batch.ts 4.3 KB

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