batch.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var Command = require('commander').Command;
  3. var fs = require('fs');
  4. var path = require('path');
  5. var series = require('./series');
  6. /**
  7. * Streams the batch of series to disk.
  8. * @param {Array.<string>} args
  9. * @param {function(Error)} done
  10. */
  11. module.exports = function(args, done) {
  12. var config = _parse(args);
  13. var batchPath = path.join(config.output || process.cwd(), 'CrunchyRoll.txt');
  14. _tasks(config, batchPath, function(err, tasks) {
  15. if (err) return done(err);
  16. var i = 0;
  17. (function next() {
  18. if (i >= tasks.length) return done();
  19. series(tasks[i].config, tasks[i].address, function(err) {
  20. if (err) return done(err);
  21. i += 1;
  22. next();
  23. });
  24. })();
  25. });
  26. };
  27. /**
  28. * Parses the configuration or reads the batch-mode file for tasks.
  29. * @private
  30. * @param {Object} config
  31. * @param {string} batchPath
  32. * @param {function(Error, Object=} done
  33. */
  34. function _tasks(config, batchPath, done) {
  35. if (config.args.length) {
  36. return done(undefined, config.args.map(function(address) {
  37. return {address: address, config: config};
  38. }));
  39. }
  40. fs.exists(batchPath, function(exists) {
  41. if (!exists) return done(undefined, []);
  42. fs.readFile(batchPath, 'utf8', function(err, data) {
  43. if (err) return done(err);
  44. var map = [];
  45. data.split(/\r?\n/).forEach(function(line) {
  46. var lineConfig = _parse(process.argv.concat(line.split(' ')));
  47. lineConfig.args.forEach(function(address) {
  48. if (!address) return;
  49. map.push({address: address, config: lineConfig});
  50. });
  51. });
  52. done(undefined, map);
  53. });
  54. });
  55. }
  56. /**
  57. * Parses the arguments and returns a configuration.
  58. * @private
  59. * @param {Array.<string>} args
  60. * @returns {Object}
  61. */
  62. function _parse(args) {
  63. return new Command().version(require('../package').version)
  64. // Disables
  65. .option('-c, --cache', 'Disables the cache.')
  66. .option('-m, --merge', 'Disables merging subtitles and videos.')
  67. // Settings
  68. .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
  69. .option('-o, --output <s>', 'The output path.')
  70. .option('-s, --series <s>', 'The series override.')
  71. .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)')
  72. .option('-v, --volume <i>', 'The volume.')
  73. .parse(args);
  74. }