batch.ts 5.1 KB

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