1 | const { l10n, print, Database, downloadThreadWithDownloader } = require('../..');
|
2 |
|
3 | exports.command = 'download <THID...>';
|
4 |
|
5 | exports.aliases = ['dl'];
|
6 |
|
7 | exports.desc = l10n('CMD_DL_DESC');
|
8 |
|
9 | exports.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 |
|
17 | exports.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 |
|
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 |
|
47 | process.exit(1);
|
48 | });
|
49 | };
|