batch.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. import commander = require('commander');
  3. import fs = require('fs');
  4. import path = require('path');
  5. import log = require('./log');
  6. import series from './series';
  7. /* correspondances between resolution and value CR excpect */
  8. const resol_table: { [id: string]: IResolData; } =
  9. {
  10. 360: {quality: '60', format: '106'},
  11. 480: {quality: '61', format: '106'},
  12. 720: {quality: '62', format: '106'},
  13. 1080: {quality: '80', format: '108'},
  14. };
  15. /**
  16. * Streams the batch of series to disk.
  17. */
  18. export default function(args: string[], done: (err?: Error) => void)
  19. {
  20. const config = parse(args);
  21. const batchPath = path.join(config.output || process.cwd(), config.batch);
  22. // set resolution
  23. if (config.resolution)
  24. {
  25. try
  26. {
  27. config.video_format = resol_table[config.resolution].format;
  28. config.video_quality = resol_table[config.resolution].quality;
  29. }
  30. catch (e)
  31. {
  32. log.warn('Invalid resolution ' + config.resolution + 'p. Setting to 1080p');
  33. config.video_format = resol_table['1080'].format;
  34. config.video_quality = resol_table['1080'].quality;
  35. }
  36. }
  37. else
  38. {
  39. /* 1080 by default */
  40. config.video_format = resol_table['1080'].format;
  41. config.video_quality = resol_table['1080'].quality;
  42. }
  43. if (config.debug)
  44. {
  45. /* Ugly but meh */
  46. const tmp = JSON.parse(JSON.stringify(config));
  47. tmp.pass = 'obfuscated';
  48. tmp.user = 'obfustated';
  49. tmp.rawArgs = undefined;
  50. tmp.options = undefined;
  51. log.dumpToDebug('Config', JSON.stringify(tmp), true);
  52. }
  53. tasks(config, batchPath, (err, tasksArr) =>
  54. {
  55. if (err)
  56. {
  57. return done(err);
  58. }
  59. let i = 0;
  60. (function next()
  61. {
  62. if (i >= tasksArr.length)
  63. {
  64. return done();
  65. }
  66. series(tasksArr[i].config, tasksArr[i].address, (errin) =>
  67. {
  68. if (errin)
  69. {
  70. if (tasksArr[i].retry <= 0)
  71. {
  72. log.error(errin.stack || errin);
  73. if (config.debug)
  74. {
  75. log.dumpToDebug('BatchGiveUp', errin.stack || errin);
  76. }
  77. log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
  78. /* Go to the next on the list */
  79. i += 1;
  80. }
  81. else
  82. {
  83. if (config.verbose)
  84. {
  85. log.error(errin);
  86. }
  87. if (config.debug)
  88. {
  89. log.dumpToDebug('BatchRetry', errin.stack || errin);
  90. }
  91. log.warn('Retrying to fetch episodes list from' + tasksArr[i].retry + ' / ' + config.retry);
  92. tasksArr[i].retry -= 1;
  93. }
  94. }
  95. else
  96. {
  97. i += 1;
  98. }
  99. next();
  100. });
  101. })();
  102. });
  103. }
  104. /**
  105. * Splits the value into arguments.
  106. */
  107. function split(value: string): string[]
  108. {
  109. let inQuote = false;
  110. let i: number;
  111. const pieces: string[] = [];
  112. let previous = 0;
  113. for (i = 0; i < value.length; i += 1)
  114. {
  115. if (value.charAt(i) === '"')
  116. {
  117. inQuote = !inQuote;
  118. }
  119. if (!inQuote && value.charAt(i) === ' ')
  120. {
  121. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  122. previous = i + 1;
  123. }
  124. }
  125. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  126. if (lastPiece)
  127. {
  128. pieces.push(lastPiece[1]);
  129. }
  130. return pieces;
  131. }
  132. /**
  133. * Parses the configuration or reads the batch-mode file for tasks.
  134. */
  135. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  136. {
  137. if (config.args.length)
  138. {
  139. const configIn = config;
  140. return done(null, config.args.map((addressIn) =>
  141. {
  142. return {address: addressIn, config: configIn, retry: config.retry};
  143. }));
  144. }
  145. fs.exists(batchPath, (exists) =>
  146. {
  147. if (!exists)
  148. {
  149. return done(null, []);
  150. }
  151. fs.readFile(batchPath, 'utf8', (err, data) =>
  152. {
  153. if (err)
  154. {
  155. return done(err);
  156. }
  157. const map: IConfigTask[] = [];
  158. data.split(/\r?\n/).forEach((line) =>
  159. {
  160. if (/^(\/\/|#)/.test(line))
  161. {
  162. return;
  163. }
  164. const lineConfig = parse(process.argv.concat(split(line)));
  165. lineConfig.args.forEach((addressIn) =>
  166. {
  167. if (!addressIn)
  168. {
  169. return;
  170. }
  171. map.push({address: addressIn, config: lineConfig, retry: config.retry});
  172. });
  173. });
  174. done(null, map);
  175. });
  176. });
  177. }
  178. /**
  179. * Parses the arguments and returns a configuration.
  180. */
  181. function parse(args: string[]): IConfigLine
  182. {
  183. return new commander.Command().version(require('../package').version)
  184. // Authentication
  185. .option('-p, --pass <s>', 'The password.')
  186. .option('-u, --user <s>', 'The e-mail address or username.')
  187. // Disables
  188. .option('-c, --cache', 'Disables the cache.')
  189. .option('-m, --merge', 'Disables merging subtitles and videos.')
  190. // Settings
  191. .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
  192. .option('-o, --output <s>', 'The output path.')
  193. .option('-s, --series <s>', 'The series override.')
  194. .option('-n, --filename <s>', 'The name override.')
  195. .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)', 'CrunchyRoll')
  196. .option('-r, --resolution <s>', 'The video resolution. (Default: 1080 (360, 480, 720, 1080))',
  197. '1080')
  198. .option('-g, --rebuildcrp', 'Rebuild the crpersistant file.')
  199. .option('-b, --batch <s>', 'Batch file', 'CrunchyRoll.txt')
  200. .option('--verbose', 'Make tool verbose')
  201. .option('--debug', 'Create a debug file. Use only if requested!')
  202. .option('--retry <i>', 'Number or time to retry fetching an episode. Default: 5', 5)
  203. .parse(args);
  204. }