batch.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (tasksArr[0].address === '')
  60. {
  61. return done();
  62. }
  63. let i = 0;
  64. (function next()
  65. {
  66. if (i >= tasksArr.length)
  67. {
  68. return done();
  69. }
  70. if (config.debug)
  71. {
  72. log.dumpToDebug('Task ' + i, JSON.stringify(tasksArr[i]));
  73. }
  74. series(config, tasksArr[i], (errin) =>
  75. {
  76. if (errin)
  77. {
  78. if (errin.error)
  79. {
  80. /* Error from the request, so ignore it */
  81. tasksArr[i].retry = 0;
  82. }
  83. if (tasksArr[i].retry <= 0)
  84. {
  85. log.error(JSON.stringify(errin));
  86. if (config.debug)
  87. {
  88. log.dumpToDebug('BatchGiveUp', JSON.stringify(errin));
  89. }
  90. log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
  91. /* Go to the next on the list */
  92. i += 1;
  93. }
  94. else
  95. {
  96. if (config.verbose)
  97. {
  98. log.error(JSON.stringify(errin));
  99. }
  100. if (config.debug)
  101. {
  102. log.dumpToDebug('BatchRetry', JSON.stringify(errin));
  103. }
  104. log.warn('Retrying to fetch episodes list from' + tasksArr[i].retry + ' / ' + config.retry);
  105. tasksArr[i].retry -= 1;
  106. }
  107. }
  108. else
  109. {
  110. i += 1;
  111. }
  112. next();
  113. });
  114. })();
  115. });
  116. }
  117. /**
  118. * Splits the value into arguments.
  119. */
  120. function split(value: string): string[]
  121. {
  122. let inQuote = false;
  123. let i: number;
  124. const pieces: string[] = [];
  125. let previous = 0;
  126. for (i = 0; i < value.length; i += 1)
  127. {
  128. if (value.charAt(i) === '"')
  129. {
  130. inQuote = !inQuote;
  131. }
  132. if (!inQuote && value.charAt(i) === ' ')
  133. {
  134. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  135. previous = i + 1;
  136. }
  137. }
  138. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  139. if (lastPiece)
  140. {
  141. pieces.push(lastPiece[1]);
  142. }
  143. return pieces;
  144. }
  145. function get_min_filter(filter: string): number
  146. {
  147. if (filter !== undefined)
  148. {
  149. const tok = filter.split('-');
  150. if (tok.length > 2)
  151. {
  152. log.error('Invalid episode filter \'' + filter + '\'');
  153. process.exit(-1);
  154. }
  155. if (tok[0] !== '')
  156. {
  157. return parseInt(tok[0], 10);
  158. }
  159. }
  160. return 0;
  161. }
  162. function get_max_filter(filter: string): number
  163. {
  164. if (filter !== undefined)
  165. {
  166. const tok = filter.split('-');
  167. if (tok.length > 2)
  168. {
  169. log.error('Invalid episode filter \'' + filter + '\'');
  170. process.exit(-1);
  171. }
  172. if ((tok.length > 1) && (tok[1] !== ''))
  173. {
  174. /* We have a max value */
  175. return parseInt(tok[1], 10);
  176. }
  177. else if ((tok.length === 1) && (tok[0] !== ''))
  178. {
  179. /* A single episode has been requested */
  180. return parseInt(tok[0], 10);
  181. }
  182. }
  183. return +Infinity;
  184. }
  185. /**
  186. * Check that URL start with http:// or https://
  187. * As for some reason request just return an error but a useless one when that happen so check it
  188. * soon enough.
  189. */
  190. function checkURL(address: string): boolean
  191. {
  192. if (address.startsWith('http:\/\/'))
  193. {
  194. return true;
  195. }
  196. if (address.startsWith('http:\/\/'))
  197. {
  198. return true;
  199. }
  200. log.error('URL ' + address + ' miss \'http:\/\/\' or \'https:\/\/\' => will be ignored');
  201. return false;
  202. }
  203. /**
  204. * Parses the configuration or reads the batch-mode file for tasks.
  205. */
  206. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  207. {
  208. if (config.args.length)
  209. {
  210. return done(null, config.args.map((addressIn) =>
  211. {
  212. if (checkURL(addressIn))
  213. {
  214. return {address: addressIn, retry: config.retry,
  215. episode_min: get_min_filter(config.episodes), episode_max: get_max_filter(config.episodes)};
  216. }
  217. return {address: '', retry: 0, episode_min: 0, episode_max: 0};
  218. }));
  219. }
  220. fs.exists(batchPath, (exists) =>
  221. {
  222. if (!exists)
  223. {
  224. return done(null, []);
  225. }
  226. fs.readFile(batchPath, 'utf8', (err, data) =>
  227. {
  228. if (err)
  229. {
  230. return done(err);
  231. }
  232. const map: IConfigTask[] = [];
  233. data.split(/\r?\n/).forEach((line) =>
  234. {
  235. if (/^(\/\/|#)/.test(line))
  236. {
  237. return;
  238. }
  239. const lineConfig = parse(process.argv.concat(split(line)));
  240. lineConfig.args.forEach((addressIn) =>
  241. {
  242. if (!addressIn)
  243. {
  244. return;
  245. }
  246. if (checkURL(addressIn))
  247. {
  248. map.push({address: addressIn, retry: lineConfig.retry,
  249. episode_min: get_min_filter(lineConfig.episodes), episode_max: get_max_filter(lineConfig.episodes)});
  250. }
  251. });
  252. });
  253. done(null, map);
  254. });
  255. });
  256. }
  257. /**
  258. * Parses the arguments and returns a configuration.
  259. */
  260. function parse(args: string[]): IConfigLine
  261. {
  262. return new commander.Command().version(require('../package').version)
  263. // Authentication
  264. .option('-p, --pass <s>', 'The password.')
  265. .option('-u, --user <s>', 'The e-mail address or username.')
  266. // Disables
  267. .option('-c, --cache', 'Disables the cache.')
  268. .option('-m, --merge', 'Disables merging subtitles and videos.')
  269. // Episode filter
  270. .option('-e, --episodes <s>', 'Episode list. Read documentation on how to use')
  271. // Settings
  272. .option('-f, --format <s>', 'The subtitle format.', 'ass')
  273. .option('-o, --output <s>', 'The output path.')
  274. .option('-s, --series <s>', 'The series name override.')
  275. .option('-n, --nametmpl <s>', 'Output name template', '{SERIES_TITLE} - s{SEASON_NUMBER}e{EPISODE_NUMBER} - {EPISODE_TITLE} - [{TAG}]')
  276. .option('-t, --tag <s>', 'The subgroup.', 'CrunchyRoll')
  277. .option('-r, --resolution <s>', 'The video resolution. (valid: 360, 480, 720, 1080)', '1080')
  278. .option('-b, --batch <s>', 'Batch file', 'CrunchyRoll.txt')
  279. .option('--verbose', 'Make tool verbose')
  280. .option('--debug', 'Create a debug file. Use only if requested!')
  281. .option('--rebuildcrp', 'Rebuild the crpersistant file.')
  282. .option('--retry <i>', 'Number or time to retry fetching an episode.', 5)
  283. .parse(args);
  284. }