batch.ts 4.3 KB

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