batch.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. tasks(config, batchPath, (err, tasksArr) =>
  44. {
  45. if (err)
  46. {
  47. return done(err);
  48. }
  49. let i = 0;
  50. (function next()
  51. {
  52. if (i >= tasksArr.length)
  53. {
  54. return done();
  55. }
  56. series(tasksArr[i].config, tasksArr[i].address, (errin) =>
  57. {
  58. if (errin)
  59. {
  60. if (tasksArr[i].retry <= 0)
  61. {
  62. console.error(errin);
  63. log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
  64. /* Go to the next on the list */
  65. i += 1;
  66. }
  67. else
  68. {
  69. if (config.verbose)
  70. {
  71. console.error(errin);
  72. }
  73. log.warn('Retrying to fetch episodes list from' + tasksArr[i].retry + ' / ' + config.retry);
  74. tasksArr[i].retry -= 1;
  75. }
  76. }
  77. else
  78. {
  79. i += 1;
  80. }
  81. next();
  82. });
  83. })();
  84. });
  85. }
  86. /**
  87. * Splits the value into arguments.
  88. */
  89. function split(value: string): string[]
  90. {
  91. let inQuote = false;
  92. let i: number;
  93. const pieces: string[] = [];
  94. let previous = 0;
  95. for (i = 0; i < value.length; i += 1)
  96. {
  97. if (value.charAt(i) === '"')
  98. {
  99. inQuote = !inQuote;
  100. }
  101. if (!inQuote && value.charAt(i) === ' ')
  102. {
  103. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  104. previous = i + 1;
  105. }
  106. }
  107. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  108. if (lastPiece)
  109. {
  110. pieces.push(lastPiece[1]);
  111. }
  112. return pieces;
  113. }
  114. /**
  115. * Parses the configuration or reads the batch-mode file for tasks.
  116. */
  117. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  118. {
  119. if (config.args.length)
  120. {
  121. const configIn = config;
  122. return done(null, config.args.map((addressIn) =>
  123. {
  124. return {address: addressIn, config: configIn, retry: config.retry};
  125. }));
  126. }
  127. fs.exists(batchPath, (exists) =>
  128. {
  129. if (!exists)
  130. {
  131. return done(null, []);
  132. }
  133. fs.readFile(batchPath, 'utf8', (err, data) =>
  134. {
  135. if (err)
  136. {
  137. return done(err);
  138. }
  139. const map: IConfigTask[] = [];
  140. data.split(/\r?\n/).forEach((line) =>
  141. {
  142. if (/^(\/\/|#)/.test(line))
  143. {
  144. return;
  145. }
  146. const lineConfig = parse(process.argv.concat(split(line)));
  147. lineConfig.args.forEach((addressIn) =>
  148. {
  149. if (!addressIn)
  150. {
  151. return;
  152. }
  153. map.push({address: addressIn, config: lineConfig, retry: config.retry});
  154. });
  155. });
  156. done(null, map);
  157. });
  158. });
  159. }
  160. /**
  161. * Parses the arguments and returns a configuration.
  162. */
  163. function parse(args: string[]): IConfigLine
  164. {
  165. return new commander.Command().version(require('../package').version)
  166. // Authentication
  167. .option('-p, --pass <s>', 'The password.')
  168. .option('-u, --user <s>', 'The e-mail address or username.')
  169. // Disables
  170. .option('-c, --cache', 'Disables the cache.')
  171. .option('-m, --merge', 'Disables merging subtitles and videos.')
  172. // Settings
  173. .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
  174. .option('-o, --output <s>', 'The output path.')
  175. .option('-s, --series <s>', 'The series override.')
  176. .option('-n, --filename <s>', 'The name override.')
  177. .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)', 'CrunchyRoll')
  178. .option('-r, --resolution <s>', 'The video resolution. (Default: 1080 (360, 480, 720, 1080))',
  179. '1080')
  180. .option('-g, --rebuildcrp', 'Rebuild the crpersistant file.')
  181. .option('-b, --batch <s>', 'Batch file', 'CrunchyRoll.txt')
  182. .option('--verbose', 'Make tool verbose')
  183. .option('--retry <i>', 'Number or time to retry fetching an episode. Default: 5', 5)
  184. .parse(args);
  185. }