batch.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 my_request = require('./my_request');
  7. import cfg = require('./config');
  8. import series from './series';
  9. /* correspondances between resolution and value CR excpect */
  10. const resol_table: { [id: string]: IResolData; } =
  11. {
  12. 360: {quality: '60', format: '106'},
  13. 480: {quality: '61', format: '106'},
  14. 720: {quality: '62', format: '106'},
  15. 1080: {quality: '80', format: '108'},
  16. };
  17. /**
  18. * Streams the batch of series to disk.
  19. */
  20. export default function(args: string[], done: (err?: Error) => void)
  21. {
  22. const config = Object.assign(cfg.load(), parse(args));
  23. let batchPath;
  24. if (path.isAbsolute(config.batch))
  25. {
  26. batchPath = path.normalize(config.batch);
  27. }
  28. else
  29. {
  30. batchPath = path.normalize(path.join(process.cwd(), config.batch));
  31. }
  32. // Update the config file with new parameters
  33. cfg.save(config);
  34. if (config.unlog)
  35. {
  36. config.crDeviceId = undefined;
  37. config.user = undefined;
  38. config.pass = undefined;
  39. my_request.eatCookies(config);
  40. cfg.save(config);
  41. log.info('Unlogged!');
  42. process.exit(0);
  43. }
  44. // set resolution
  45. if (config.resolution)
  46. {
  47. try
  48. {
  49. config.video_format = resol_table[config.resolution].format;
  50. config.video_quality = resol_table[config.resolution].quality;
  51. }
  52. catch (e)
  53. {
  54. log.warn('Invalid resolution ' + config.resolution + 'p. Setting to 1080p');
  55. config.video_format = resol_table['1080'].format;
  56. config.video_quality = resol_table['1080'].quality;
  57. }
  58. }
  59. else
  60. {
  61. /* 1080 by default */
  62. config.video_format = resol_table['1080'].format;
  63. config.video_quality = resol_table['1080'].quality;
  64. }
  65. if (config.debug)
  66. {
  67. /* Ugly but meh */
  68. const tmp = JSON.parse(JSON.stringify(config));
  69. tmp.pass = 'obfuscated';
  70. tmp.user = 'obfustated';
  71. tmp.rawArgs = undefined;
  72. tmp.options = undefined;
  73. log.dumpToDebug('Config', JSON.stringify(tmp), true);
  74. }
  75. tasks(config, batchPath, (err, tasksArr) =>
  76. {
  77. if (err)
  78. {
  79. return done(err);
  80. }
  81. if (!tasksArr || !tasksArr[0] || (tasksArr[0].address === ''))
  82. {
  83. return done();
  84. }
  85. let i = 0;
  86. (function next()
  87. {
  88. if (i >= tasksArr.length)
  89. {
  90. // Save configuration before leaving (should store info like session & other)
  91. cfg.save(config);
  92. return done();
  93. }
  94. if (config.debug)
  95. {
  96. log.dumpToDebug('Task ' + i, JSON.stringify(tasksArr[i]));
  97. }
  98. series(config, tasksArr[i], (errin) =>
  99. {
  100. if (errin)
  101. {
  102. if (errin.error)
  103. {
  104. /* Error from the request, so ignore it */
  105. tasksArr[i].retry = 0;
  106. }
  107. if (errin.authError)
  108. {
  109. /* Force a graceful exit */
  110. log.error(errin.message);
  111. i = tasksArr.length;
  112. }
  113. else if (tasksArr[i].retry <= 0)
  114. {
  115. log.error(JSON.stringify(errin));
  116. if (config.debug)
  117. {
  118. log.dumpToDebug('BatchGiveUp', JSON.stringify(errin));
  119. }
  120. log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
  121. /* Go to the next on the list */
  122. i += 1;
  123. }
  124. else
  125. {
  126. if (config.verbose)
  127. {
  128. log.error(JSON.stringify(errin));
  129. }
  130. if (config.debug)
  131. {
  132. log.dumpToDebug('BatchRetry', JSON.stringify(errin));
  133. }
  134. log.warn('Retrying to fetch episodes list from' + tasksArr[i].retry + ' / ' + config.retry);
  135. tasksArr[i].retry -= 1;
  136. }
  137. }
  138. else
  139. {
  140. i += 1;
  141. }
  142. next();
  143. });
  144. })();
  145. });
  146. }
  147. /**
  148. * Splits the value into arguments.
  149. */
  150. function split(value: string): string[]
  151. {
  152. let inQuote = false;
  153. let i: number;
  154. const pieces: string[] = [];
  155. let previous = 0;
  156. for (i = 0; i < value.length; i += 1)
  157. {
  158. if (value.charAt(i) === '"')
  159. {
  160. inQuote = !inQuote;
  161. }
  162. if (!inQuote && value.charAt(i) === ' ')
  163. {
  164. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  165. previous = i + 1;
  166. }
  167. }
  168. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  169. if (lastPiece)
  170. {
  171. pieces.push(lastPiece[1]);
  172. }
  173. return pieces;
  174. }
  175. function get_min_filter(filter: string): number
  176. {
  177. if (filter !== undefined)
  178. {
  179. const tok = filter.split('-');
  180. if (tok.length > 2)
  181. {
  182. log.error('Invalid episode filter \'' + filter + '\'');
  183. process.exit(-1);
  184. }
  185. if (tok[0] !== '')
  186. {
  187. return parseInt(tok[0], 10);
  188. }
  189. }
  190. return 0;
  191. }
  192. function get_max_filter(filter: string): number
  193. {
  194. if (filter !== undefined)
  195. {
  196. const tok = filter.split('-');
  197. if (tok.length > 2)
  198. {
  199. log.error('Invalid episode filter \'' + filter + '\'');
  200. process.exit(-1);
  201. }
  202. if ((tok.length > 1) && (tok[1] !== ''))
  203. {
  204. /* We have a max value */
  205. return parseInt(tok[1], 10);
  206. }
  207. else if ((tok.length === 1) && (tok[0] !== ''))
  208. {
  209. /* A single episode has been requested */
  210. return parseInt(tok[0], 10);
  211. }
  212. }
  213. return +Infinity;
  214. }
  215. /**
  216. * Check that URL start with http:// or https://
  217. * As for some reason request just return an error but a useless one when that happen so check it
  218. * soon enough.
  219. */
  220. function checkURL(address: string): boolean
  221. {
  222. if (address.startsWith('http:\/\/'))
  223. {
  224. return true;
  225. }
  226. if (address.startsWith('https:\/\/'))
  227. {
  228. return true;
  229. }
  230. if (address.startsWith('@http:\/\/'))
  231. {
  232. return true;
  233. }
  234. if (address.startsWith('@https:\/\/'))
  235. {
  236. return true;
  237. }
  238. log.error('URL ' + address + ' miss \'http:\/\/\' or \'https:\/\/\' => will be ignored');
  239. return false;
  240. }
  241. /**
  242. * Parses the configuration or reads the batch-mode file for tasks.
  243. */
  244. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  245. {
  246. if (config.args.length)
  247. {
  248. return done(null, config.args.map((addressIn) =>
  249. {
  250. if (checkURL(addressIn))
  251. {
  252. return {address: addressIn, retry: config.retry,
  253. episode_min: get_min_filter(config.episodes), episode_max: get_max_filter(config.episodes)};
  254. }
  255. return {address: '', retry: 0, episode_min: 0, episode_max: 0};
  256. }));
  257. }
  258. fs.exists(batchPath, (exists) =>
  259. {
  260. if (!exists)
  261. {
  262. return done(null, []);
  263. }
  264. fs.readFile(batchPath, 'utf8', (err, data) =>
  265. {
  266. if (err)
  267. {
  268. return done(err);
  269. }
  270. const map: IConfigTask[] = [];
  271. data.split(/\r?\n/).forEach((line) =>
  272. {
  273. if (/^(\/\/|#)/.test(line))
  274. {
  275. return;
  276. }
  277. const lineConfig = parse(process.argv.concat(split(line)));
  278. lineConfig.args.forEach((addressIn) =>
  279. {
  280. if (!addressIn)
  281. {
  282. return;
  283. }
  284. if (checkURL(addressIn))
  285. {
  286. map.push({address: addressIn, retry: lineConfig.retry,
  287. episode_min: get_min_filter(lineConfig.episodes), episode_max: get_max_filter(lineConfig.episodes)});
  288. }
  289. });
  290. });
  291. done(null, map);
  292. });
  293. });
  294. }
  295. /**
  296. * Parses the arguments and returns a configuration.
  297. */
  298. function parse(args: string[]): IConfigLine
  299. {
  300. return new commander.Command().version(require('../package').version)
  301. // Authentication
  302. .option('-p, --pass <s>', 'The password.')
  303. .option('-u, --user <s>', 'The e-mail address or username.')
  304. .option('-d, --unlog', 'Unlog')
  305. // Disables
  306. .option('-c, --cache', 'Disables the cache.')
  307. .option('-m, --merge', 'Disables merging subtitles and videos.')
  308. // Episode filter
  309. .option('-e, --episodes <s>', 'Episode list. Read documentation on how to use')
  310. // Settings
  311. .option('-l, --crlang <s>', 'CR page language (valid: en, fr, es, it, pt, de, ru).')
  312. .option('-f, --format <s>', 'The subtitle format.', 'ass')
  313. .option('-o, --output <s>', 'The output path.')
  314. .option('-s, --series <s>', 'The series name override.')
  315. .option('--ignoredub', 'Experimental: Ignore all seasons where the title end with \'Dub)\'')
  316. .option('-n, --nametmpl <s>', 'Output name template', '{SERIES_TITLE} - s{SEASON_NUMBER}e{EPISODE_NUMBER} - {EPISODE_TITLE} - [{TAG}]')
  317. .option('-t, --tag <s>', 'The subgroup.', 'CrunchyRoll')
  318. .option('-r, --resolution <s>', 'The video resolution. (valid: 360, 480, 720, 1080)', '1080')
  319. .option('-b, --batch <s>', 'Batch file', 'CrunchyRoll.txt')
  320. .option('--verbose', 'Make tool verbose')
  321. .option('--debug', 'Create a debug file. Use only if requested!')
  322. .option('--rebuildcrp', 'Rebuild the crpersistant file.')
  323. .option('--retry <i>', 'Number or time to retry fetching an episode.', 5)
  324. .parse(args);
  325. }