<commit_before>var argv = require('minimist')(process.argv.slice(2));
var Yts = require('./lib/yts.js');
var chalk = require('chalk');

var showRecentMovies = function (options) {
  options = options || {};
  options.sort_by = options.sort_by || 'year';
  options.order_by = options.order_by || 'desc';

  Yts.listMovies(options, function (err, res) {
    if (err) { console.log(err); }

    res.data.movies.forEach(function (movie) {
      console.log(chalk.underline.yellow(movie.id) + ': ' + movie.title_long);
    });
  });
};

var searchMovie = function (str) {
  Yts.listMovies({ query_term: str }, function (err, res) {
    if (err) { console.log(err); }

    if (res.data.movies.length < 1) {
      console.log('No movies found');
    } else {
      res.data.movies.forEach(function (movie) {
        console.log(chalk.underline.yellow(movie.id) + ': ' + movie.title_long);
      });
    }
  });
};

var getInfo = function (movieID) {
  var open = require('open');
  Yts.movieDetails({ movie_id: movieID }, function (err, res) {
    if (err) { return console.log(err); }
    console.log('Opening browser with movie info...');
    open(res.data.url);
  });
}

var getMovie = function (movieID, quality, subs) {
  quality = quality || '720p';
  subs = subs || 'english';
  var YSubs = require('./lib/ysubs.js');

  var spawn = require('child_process').spawn;

  Yts.movieDetails({ movie_id: movieID }, function (err, res) {
    if (res.status === 'error') {
      return console.log(chalk.red(res.status_message));
    }

    var torrInfo = res.data.torrents.filter(function (torr) {
      return torr.quality === quality;
    }).pop();
    var parseTorrent = require('parse-torrent');
    parseTorrent.remote(torrInfo.url, function (err, tinfo) {
      var magnetURI = parseTorrent.toMagnetURI(tinfo);

      var peerflix = spawn('peerflix', [
        '-t',
        res.data.slug + '.srt',
        magnetURI,
        '--vlc'
      ]);

      peerflix.stdout.on('data', function (data) {
        process.stdout.write(data);
      });

      peerflix.stderr.on('data', function (data) {
        process.stdout.write(data);
      });
    });

    YSubs.getSubtitles(res.data.imdb_code, function (err, data) {
      if (!data[subs]) {
        return console.log('No subtitles available in %s', subs);
      }
      var zipPath = data[subs].shift().url;
      YSubs.fetchSubtitle(zipPath, res.data.slug + '.srt', function (err, data) {
        if (err) { console.log(err); }
      });
    });
  })
};


var showHelp = function () {
  console.log('movees [options]');
  console.log('Options:');
  console.log('--search <search term>  search for a movie');
  console.log('--watch <movie id> [--quality <720p|1080p|3d> [--subs <subtitle language>]   watch a movie');
};

var showVersion = function () {
  var version = require('./package.json').version;
  console.log('Movees (version %s)', version);
};

/**
 * CLI arguments handling
 */

if (argv.search) {
  searchMovie(argv.search);
} else if (argv.watch) {
  getMovie(argv.watch, argv.quality, argv.subs);
} else if (argv.info) {
  getInfo(argv.info);
} else if (argv.version) {
  showVersion();
} else if (argv.help) {
  showHelp();
} else {
  var opts = {};
  opts.page = argv.page || 1;
  opts.limit = argv.limit || '20';
  showRecentMovies(opts);
}
<commit_msg>Fix error when movie has no subtitles available<commit_after>var argv = require('minimist')(process.argv.slice(2));
var Yts = require('./lib/yts.js');
var chalk = require('chalk');

var showRecentMovies = function (options) {
  options = options || {};
  options.sort_by = options.sort_by || 'year';
  options.order_by = options.order_by || 'desc';

  Yts.listMovies(options, function (err, res) {
    if (err) { console.log(err); }

    res.data.movies.forEach(function (movie) {
      console.log(chalk.underline.yellow(movie.id) + ': ' + movie.title_long);
    });
  });
};

var searchMovie = function (str) {
  Yts.listMovies({ query_term: str }, function (err, res) {
    if (err) { console.log(err); }

    if (res.data.movies.length < 1) {
      console.log('No movies found');
    } else {
      res.data.movies.forEach(function (movie) {
        console.log(chalk.underline.yellow(movie.id) + ': ' + movie.title_long);
      });
    }
  });
};

var getInfo = function (movieID) {
  var open = require('open');
  Yts.movieDetails({ movie_id: movieID }, function (err, res) {
    if (err) { return console.log(err); }
    console.log('Opening browser with movie info...');
    open(res.data.url);
  });
}

var getMovie = function (movieID, quality, subs) {
  quality = quality || '720p';
  subs = subs || 'english';
  var YSubs = require('./lib/ysubs.js');

  var spawn = require('child_process').spawn;

  Yts.movieDetails({ movie_id: movieID }, function (err, res) {
    if (res.status === 'error') {
      return console.log(chalk.red(res.status_message));
    }

    var torrInfo = res.data.torrents.filter(function (torr) {
      return torr.quality === quality;
    }).pop();
    var parseTorrent = require('parse-torrent');
    parseTorrent.remote(torrInfo.url, function (err, tinfo) {
      var magnetURI = parseTorrent.toMagnetURI(tinfo);

      var peerflix = spawn('peerflix', [
        '-t',
        res.data.slug + '.srt',
        magnetURI,
        '--vlc'
      ]);

      peerflix.stdout.on('data', function (data) {
        process.stdout.write(data);
      });

      peerflix.stderr.on('data', function (data) {
        process.stdout.write(data);
      });
    });

    YSubs.getSubtitles(res.data.imdb_code, function (err, data) {
      if (!data || !data[subs]) {
        return console.log('No subtitles available in %s', subs);
      }
      var zipPath = data[subs].shift().url;
      YSubs.fetchSubtitle(zipPath, res.data.slug + '.srt', function (err, data) {
        if (err) { console.log(err); }
      });
    });
  })
};


var showHelp = function () {
  console.log('movees [options]');
  console.log('Options:');
  console.log('--search <search term>  search for a movie');
  console.log('--watch <movie id> [--quality <720p|1080p|3d> [--subs <subtitle language>]   watch a movie');
};

var showVersion = function () {
  var version = require('./package.json').version;
  console.log('Movees (version %s)', version);
};

/**
 * CLI arguments handling
 */

if (argv.search) {
  searchMovie(argv.search);
} else if (argv.watch) {
  getMovie(argv.watch, argv.quality, argv.subs);
} else if (argv.info) {
  getInfo(argv.info);
} else if (argv.version) {
  showVersion();
} else if (argv.help) {
  showHelp();
} else {
  var opts = {};
  opts.page = argv.page || 1;
  opts.limit = argv.limit || '20';
  showRecentMovies(opts);
}

{'fraction_non_alphanumeric': 0.18006286484059272, 'fraction_numerical': 0.004789702140398144, 'mean_word_length': 3.064476885644769, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6751493', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2282, 'n_words': 478}