batch.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict';
  2. import commander = require('commander');
  3. import fs = require('fs');
  4. import path = require('path');
  5. import series from './series';
  6. import log = require('./log');
  7. /* correspondances between resolution and value CR excpect */
  8. let resol_table: { [id: string]: IResolData; } = {
  9. '360': {quality:'60', format:'106'},
  10. '480': {quality:'61', format:'106'},
  11. '720': {quality:'62', format:'106'},
  12. '1080': {quality:'80', format:'108'},
  13. };
  14. /**
  15. * Streams the batch of series to disk.
  16. */
  17. export default function(args: string[], done: (err?: Error) => void)
  18. {
  19. const config = parse(args);
  20. const batchPath = path.join(config.output || process.cwd(), 'CrunchyRoll.txt');
  21. // set resolution
  22. if (config.resolution)
  23. {
  24. try
  25. {
  26. config.video_format = resol_table[config.resolution]['format'];
  27. config.video_quality = resol_table[config.resolution]['quality'];
  28. }
  29. catch(e)
  30. {
  31. log.warn("Invalid resolution " + config.resolution + "p. Setting to 1080p")
  32. config.video_format = resol_table['1080']['format'];
  33. config.video_quality = resol_table['1080']['quality'];
  34. }
  35. }
  36. else
  37. {
  38. /* 1080 by default */
  39. config.video_format = resol_table['1080']['format'];
  40. config.video_quality = resol_table['1080']['quality'];
  41. }
  42. tasks(config, batchPath, (err, tasks) =>
  43. {
  44. if (err)
  45. {
  46. return done(err);
  47. }
  48. let i = 0;
  49. (function next()
  50. {
  51. if (i >= tasks.length)
  52. {
  53. return done();
  54. }
  55. series(tasks[i].config, tasks[i].address, (errin) =>
  56. {
  57. if (errin)
  58. {
  59. return done(errin);
  60. }
  61. i += 1;
  62. next();
  63. });
  64. })();
  65. });
  66. }
  67. /**
  68. * Splits the value into arguments.
  69. */
  70. function split(value: string): string[]
  71. {
  72. let inQuote = false;
  73. let i: number;
  74. const pieces: string[] = [];
  75. let previous = 0;
  76. for (i = 0; i < value.length; i += 1)
  77. {
  78. if (value.charAt(i) === '"')
  79. {
  80. inQuote = !inQuote;
  81. }
  82. if (!inQuote && value.charAt(i) === ' ')
  83. {
  84. pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
  85. previous = i + 1;
  86. }
  87. }
  88. const lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
  89. if (lastPiece)
  90. {
  91. pieces.push(lastPiece[1]);
  92. }
  93. return pieces;
  94. }
  95. /**
  96. * Parses the configuration or reads the batch-mode file for tasks.
  97. */
  98. function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?: IConfigTask[]) => void)
  99. {
  100. if (config.args.length)
  101. {
  102. const configIn = config;
  103. return done(null, config.args.map((addressIn) =>
  104. {
  105. return {address: addressIn, config: configIn};
  106. }));
  107. }
  108. fs.exists(batchPath, (exists) =>
  109. {
  110. if (!exists)
  111. {
  112. return done(null, []);
  113. }
  114. fs.readFile(batchPath, 'utf8', (err, data) =>
  115. {
  116. if (err)
  117. {
  118. return done(err);
  119. }
  120. const map: IConfigTask[] = [];
  121. data.split(/\r?\n/).forEach((line) =>
  122. {
  123. if (/^(\/\/|#)/.test(line))
  124. {
  125. return;
  126. }
  127. const lineConfig = parse(process.argv.concat(split(line)));
  128. lineConfig.args.forEach((addressIn) =>
  129. {
  130. if (!addressIn)
  131. {
  132. return;
  133. }
  134. map.push({address: addressIn, config: lineConfig});
  135. });
  136. });
  137. done(null, map);
  138. });
  139. });
  140. }
  141. /**
  142. * Parses the arguments and returns a configuration.
  143. */
  144. function parse(args: string[]): IConfigLine
  145. {
  146. return new commander.Command().version(require('../package').version)
  147. // Authentication
  148. .option('-p, --pass <s>', 'The password.')
  149. .option('-u, --user <s>', 'The e-mail address or username.')
  150. // Disables
  151. .option('-c, --cache', 'Disables the cache.')
  152. .option('-m, --merge', 'Disables merging subtitles and videos.')
  153. // Filters
  154. .option('-e, --episode <i>', 'The episode filter.')
  155. .option('-v, --volume <i>', 'The volume filter.')
  156. // Settings
  157. .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
  158. .option('-o, --output <s>', 'The output path.')
  159. .option('-s, --series <s>', 'The series override.')
  160. .option('-n, --filename <s>', 'The name override.')
  161. .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)')
  162. .option('-r, --resolution <s>', 'The video resolution. (Default: 1080 (360, 480, 720, 1080))')
  163. .parse(args);
  164. }