|
1 | 1 | const download = require('download'); |
2 | | -const ora = require('ora'); |
| 2 | +const Listr = require('listr'); |
| 3 | +const {existsSync} = require('fs'); |
3 | 4 |
|
4 | 5 | module.exports = async (input, flags) => { |
5 | 6 | const {type, outputDir} = flags; |
6 | | - const parser = require(`./lib/${type}`); |
7 | | - const spinner = ora(`Loading ${type}`).start(); |
8 | | - |
9 | | - const {title, images, options} = await parser(input, flags); |
10 | | - |
11 | | - spinner.info(`Title: ${title}`); |
12 | | - spinner.info(`Images: ${images.length}`); |
13 | | - |
14 | | - // 开启info的话只输出信息 |
15 | | - if (!flags.info) { |
16 | | - for (const image of images) { |
17 | | - let url = image; |
18 | | - const imgOptions = {}; |
19 | | - if (typeof image !== 'string') { |
20 | | - url = image.url; |
21 | | - if (image.name) { |
22 | | - imgOptions.filename = image.name; |
| 7 | + |
| 8 | + const tasks = new Listr([ |
| 9 | + { |
| 10 | + title: '检查类型是否支持', |
| 11 | + task: () => { |
| 12 | + if (!existsSync(`${__dirname}/lib/${type}.js`)) { |
| 13 | + throw new Error(`${type} 类型不支持`); |
23 | 14 | } |
24 | 15 | } |
25 | | - |
26 | | - try { |
27 | | - spinner.start(`Downloading ${url}`); |
28 | | - await download( |
29 | | - encodeURI(url), |
30 | | - outputDir || `amanga/${type}/${title}`, |
31 | | - { |
32 | | - // 10s超时 |
33 | | - timeout: 10000, |
34 | | - ...options, |
35 | | - ...imgOptions |
36 | | - } |
37 | | - ).on('downloadProgress', progress => { |
38 | | - spinner.text = `(${ |
39 | | - progress.transferred |
40 | | - }) Downloading ${url}`; |
41 | | - }); |
42 | | - spinner.succeed(`Done ${url}`); |
43 | | - } catch (error) { |
44 | | - spinner.fail(`${error.message} ${url}`); |
| 16 | + }, |
| 17 | + { |
| 18 | + title: '获取数据', |
| 19 | + task: async ctx => { |
| 20 | + const parser = require(`./lib/${type}`); |
| 21 | + const data = await parser(input, flags); |
| 22 | + ctx.data = data; |
45 | 23 | } |
46 | 24 | } |
| 25 | + ]); |
| 26 | + |
| 27 | + if (flags.info) { |
| 28 | + tasks.add({ |
| 29 | + title: '格式化数据', |
| 30 | + task: ctx => { |
| 31 | + const {title, images} = ctx.data; |
| 32 | + |
| 33 | + const info = ` |
| 34 | + 标题: ${title} |
| 35 | + 数量: ${images.length} |
| 36 | + 图片链接: |
| 37 | + ${ |
| 38 | + typeof images[0] === 'string' |
| 39 | + ? images.join('\n') |
| 40 | + : images.map(img => img.url).join('\n') |
| 41 | + } |
| 42 | +
|
| 43 | + `; |
| 44 | + |
| 45 | + ctx.info = info; |
| 46 | + } |
| 47 | + }); |
47 | 48 | } else { |
48 | | - console.log(); |
49 | | - console.dir({type, title, images}); |
50 | | - console.log(); |
| 49 | + tasks.add({ |
| 50 | + title: '下载图片', |
| 51 | + skip: () => flags.info, |
| 52 | + task: async (ctx, task) => { |
| 53 | + const {title, images, options} = ctx.data; |
| 54 | + |
| 55 | + let count = 0; |
| 56 | + task.title = `下载图片 [${count}/${images.length}]`; |
| 57 | + |
| 58 | + for (const image of images) { |
| 59 | + let url = image; |
| 60 | + const imgOptions = {}; |
| 61 | + if (typeof image !== 'string') { |
| 62 | + url = image.url; |
| 63 | + if (image.name) { |
| 64 | + imgOptions.filename = image.name; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + task.output = `下载 ${url}`; |
| 69 | + await download( |
| 70 | + encodeURI(url), |
| 71 | + outputDir || `amanga/${type}/${title}`, |
| 72 | + { |
| 73 | + // 10s |
| 74 | + timeout: 10000, |
| 75 | + ...options, |
| 76 | + ...imgOptions |
| 77 | + } |
| 78 | + ).on( |
| 79 | + 'downloadProgress', |
| 80 | + ({transferred, total, percent}) => { |
| 81 | + task.output = `下载 ${url} [${transferred}/${total}]`; |
| 82 | + if (percent === 1) { |
| 83 | + task.title = `下载图片 [${(count += 1)}/${ |
| 84 | + images.length |
| 85 | + }]`; |
| 86 | + } |
| 87 | + } |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
51 | 92 | } |
52 | 93 |
|
53 | | - spinner.info('All Done 🎉'); |
54 | | - spinner.stop(); |
| 94 | + await tasks.run().then(ctx => { |
| 95 | + if (ctx.info) { |
| 96 | + console.log(ctx.info); |
| 97 | + } |
| 98 | + }); |
55 | 99 | }; |
0 commit comments