UNPKG

1.38 kBJavaScriptView Raw
1const { l10n, print, Database, downloadThreadWithDownloader } = require('../..');
2
3exports.command = 'download <THID...>';
4
5exports.aliases = ['dl'];
6
7exports.desc = l10n('CMD_DL_DESC');
8
9exports.builder = (yargs) => {
10 yargs
11 .usage(l10n('CMD_DL_USAGE'))
12 .example('dmhy dl AAA', l10n('CMD_DL_EXAMPLE1_DESC'))
13 .example('dmhy dl AAA-1,4,9', l10n('CMD_DL_EXAMPLE2_DESC'))
14 .example('dmhy dl AAA-OVA1..3', l10n('CMD_DL_EXAMPLE3_DESC'));
15};
16
17exports.handler = (argv) => {
18 const db = new Database();
19
20 const allTasks = argv.THID.map((thid) => {
21 const [sid, epstr] = thid.split('-');
22 const found = db.find({ sid });
23
24 if (found) {
25 const threads = found.getThreads(epstr);
26 print.debug('threads', threads);
27 const allThreadTasks = Promise.all(threads.map((th) => {
28 const downloader = db.config.get('downloader').value;
29 return downloadThreadWithDownloader(downloader, th, db.config.parameters);
30 }));
31
32 // flatten promise for outer Pormise.all
33 return new Promise((resolve, reject) => {
34 allThreadTasks.then(resolve).catch(reject);
35 });
36 } else {
37 print.error(l10n('CMD_DL_SID_NOT_FOUND', { sid }));
38 }
39 });
40
41 Promise.all(allTasks)
42 .then(() => {
43 process.exit(0);
44 })
45 .catch((_) => {
46 // Error will print by downloaders, keep quiet
47 process.exit(1);
48 });
49};