batch.ts 8.2 KB

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