batch.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. import commander = require('commander');
  3. import fs = require('fs');
  4. import path = require('path');
  5. import series from './series';
  6. /**
  7. * Streams the batch of series to disk.
  8. */
  9. export default function(args: string[], done: (err?: Error) => void)
  10. {
  11. const config = parse(args);
  12. const batchPath = path.join(config.output || process.cwd(), 'CrunchyRoll.txt');
  13. tasks(config, batchPath, (err, tasks) =>
  14. {
  15. if (err)
  16. {
  17. return done(err);
  18. }
  19. let i = 0;
  20. (function next()
  21. {
  22. if (i >= tasks.length)
  23. {
  24. return done();
  25. }
  26. series(tasks[i].config, tasks[i].address, (errin) =>
  27. {
  28. if (errin)
  29. {
  30. return done(errin);
  31. }
  32. i += 1;
  33. next();
  34. });
  35. })();
  36. });
  37. }
  38. /**
  39. * Splits the value into arguments.
  40. */
  41. function split(value: string): string[]
  42. {
  43. let inQuote = false;
  44. let i: number;
  45. const pieces: string[] = [];
  46. let previous = 0;
  47. for (i = 0; i < value.length; i += 1)
  48. {
  49. if (value.charAt(i) === '"')
  50. {
  51. inQuote = !inQuote;
  52. }
  53. if (!inQuote && value.charAt(i) === ' ')
  54. {
  55. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  56. previous = i + 1;
  57. }
  58. }
  59. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  60. if (lastPiece)
  61. {
  62. pieces.push(lastPiece[1]);
  63. }
  64. return pieces;
  65. }
  66. /**
  67. * Parses the configuration or reads the batch-mode file for tasks.
  68. */
  69. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  70. {
  71. if (config.args.length)
  72. {
  73. const configIn = config;
  74. return done(null, config.args.map((addressIn) =>
  75. {
  76. return {address: addressIn, config: configIn};
  77. }));
  78. }
  79. fs.exists(batchPath, (exists) =>
  80. {
  81. if (!exists)
  82. {
  83. return done(null, []);
  84. }
  85. fs.readFile(batchPath, 'utf8', (err, data) =>
  86. {
  87. if (err)
  88. {
  89. return done(err);
  90. }
  91. const map: IConfigTask[] = [];
  92. data.split(/\r?\n/).forEach((line) =>
  93. {
  94. if (/^(\/\/|#)/.test(line))
  95. {
  96. return;
  97. }
  98. const lineConfig = parse(process.argv.concat(split(line)));
  99. lineConfig.args.forEach((addressIn) =>
  100. {
  101. if (!addressIn)
  102. {
  103. return;
  104. }
  105. map.push({address: addressIn, config: lineConfig});
  106. });
  107. });
  108. done(null, map);
  109. });
  110. });
  111. }
  112. /**
  113. * Parses the arguments and returns a configuration.
  114. */
  115. function parse(args: string[]): IConfigLine
  116. {
  117. return new commander.Command().version(require('../package').version)
  118. // Authentication
  119. .option('-p, --pass <s>', 'The password.')
  120. .option('-u, --user <s>', 'The e-mail address or username.')
  121. // Disables
  122. .option('-c, --cache', 'Disables the cache.')
  123. .option('-m, --merge', 'Disables merging subtitles and videos.')
  124. // Filters
  125. .option('-e, --episode <i>', 'The episode filter.')
  126. .option('-v, --volume <i>', 'The volume filter.')
  127. // Settings
  128. .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
  129. .option('-o, --output <s>', 'The output path.')
  130. .option('-s, --series <s>', 'The series override.')
  131. .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)')
  132. .parse(args);
  133. }