batch.ts 9.9 KB

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