|
1 |
| -const download = require('download'); |
2 |
| -const Listr = require('listr'); |
3 |
| -const sharp = require('sharp'); |
4 |
| -const path = require('path'); |
5 |
| -const makeDir = require('make-dir'); |
6 |
| -const {existsSync} = require('fs'); |
7 |
| - |
8 | 1 | const supportedImageTypes = ['jpeg', 'png', 'webp', 'tiff'];
|
| 2 | +const supportedSites = { |
| 3 | + ishuhui: 'ishuhui', |
| 4 | + manhuagui: 'manhuagui', |
| 5 | + qq: 'qq', |
| 6 | + nhentai: 'nhentai', |
| 7 | + yyls: 'yyls', |
| 8 | +}; |
9 | 9 |
|
10 |
| -async function delay(ms) { |
11 |
| - return new Promise(resolve => setTimeout(resolve, ms)); |
12 |
| -} |
13 |
| - |
14 |
| -module.exports = async (input, flags) => { |
15 |
| - const {type, outputDir, ext, focus} = flags; |
16 |
| - |
17 |
| - if (!supportedImageTypes.includes(ext)) { |
18 |
| - throw new TypeError( |
19 |
| - `不支持 ${ext} 图片格式,仅支持 ${supportedImageTypes.join('|')}` |
20 |
| - ); |
21 |
| - } |
22 |
| - |
23 |
| - const tasks = new Listr([ |
24 |
| - { |
25 |
| - title: '检查类型是否支持', |
26 |
| - task: () => { |
27 |
| - if (!existsSync(`${__dirname}/lib/${type}.js`)) { |
28 |
| - throw new Error(`${type} 类型不支持`); |
29 |
| - } |
30 |
| - } |
31 |
| - }, |
32 |
| - { |
33 |
| - title: '获取数据', |
34 |
| - task: async ctx => { |
35 |
| - const parser = require(`./lib/${type}`); |
36 |
| - const data = await parser(input, flags); |
37 |
| - ctx.data = data; |
38 |
| - } |
39 |
| - } |
40 |
| - ]); |
41 |
| - |
42 |
| - if (flags.info) { |
43 |
| - tasks.add({ |
44 |
| - title: '格式化数据', |
45 |
| - task: ctx => { |
46 |
| - const {title, images} = ctx.data; |
47 |
| - |
48 |
| - const info = ` |
49 |
| - 标题: ${title} |
50 |
| - 数量: ${images.length} |
51 |
| - 图片链接: |
52 |
| - ${ |
53 |
| - typeof images[0] === 'string' |
54 |
| - ? images.join('\n ') |
55 |
| - : images.map(img => img.url).join('\n ') |
56 |
| - } |
57 |
| - `; |
58 |
| - |
59 |
| - ctx.info = info; |
60 |
| - } |
61 |
| - }); |
62 |
| - } else { |
63 |
| - tasks.add({ |
64 |
| - title: '下载图片', |
65 |
| - skip: () => flags.info, |
66 |
| - task: async (ctx, task) => { |
67 |
| - const {title, images, options} = ctx.data; |
68 |
| - const maxLength = images.length.toString().length; |
69 |
| - |
70 |
| - let count = 0; |
71 |
| - task.title = `下载图片 [${count}/${images.length}]`; |
72 |
| - |
73 |
| - const imagesWithIndex = images.map((url, index) => { |
74 |
| - const filename = `${index + 1}`.padStart(maxLength, '0'); |
75 |
| - return [index, {filename, url}]; |
76 |
| - }); |
77 |
| - |
78 |
| - const updateTitle = () => { |
79 |
| - task.title = `下载图片 [${(count += 1)}/${images.length}]`; |
80 |
| - }; |
81 |
| - |
82 |
| - for (const [index, image] of imagesWithIndex) { |
83 |
| - const {url, filename} = image; |
84 |
| - |
85 |
| - task.output = `下载 ${url}`; |
86 |
| - |
87 |
| - const outputFilepath = path.join( |
88 |
| - outputDir || `amanga/${type}`, |
89 |
| - `${title}`, |
90 |
| - `${filename}.${ext}` |
91 |
| - ); |
| 10 | +const match1 = (text, regex) => { |
| 11 | + let r = new RegExp(regex).exec(text); |
| 12 | + if (r) { |
| 13 | + return r[1]; |
| 14 | + } |
| 15 | + return ''; |
| 16 | +}; |
| 17 | +const getModule = url => { |
| 18 | + const host = match1(url, /https?:\/\/([^\/]+)\//); |
| 19 | + const domain = match1(host, /(\.[^.]+\.[^.]+)$/) || host; |
| 20 | + const key = match1(domain, /([^.]+)/); |
92 | 21 |
|
93 |
| - if (!focus && existsSync(outputFilepath)) { |
94 |
| - task.output = `图片已存在,跳过 ${url}`; |
95 |
| - await delay(150); |
96 |
| - updateTitle(); |
97 |
| - continue; |
98 |
| - } |
| 22 | + if (key in supportedSites) { |
| 23 | + return require(`./lib/${supportedSites[key]}`); |
| 24 | + } |
99 | 25 |
|
100 |
| - await download(encodeURI(url), { |
101 |
| - // 10s |
102 |
| - timeout: 10000, |
103 |
| - ...options |
104 |
| - }) |
105 |
| - .on( |
106 |
| - 'downloadProgress', |
107 |
| - ({transferred, total, percent}) => { |
108 |
| - task.output = `下载 ${url} [${transferred}/${total}]`; |
109 |
| - if (percent === 1) { |
110 |
| - updateTitle(); |
111 |
| - } |
112 |
| - } |
113 |
| - ) |
114 |
| - .then(data => { |
115 |
| - // 确保文件夹存在 -> 用 sharp 输出图片格式 |
116 |
| - return makeDir(path.dirname(outputFilepath)).then( |
117 |
| - () => |
118 |
| - sharp(data) |
119 |
| - [ext]() |
120 |
| - .toFile(outputFilepath) |
121 |
| - ); |
122 |
| - }); |
123 |
| - } |
124 |
| - } |
125 |
| - }); |
126 |
| - } |
| 26 | + throw new Error('Site not supported ' + url); |
| 27 | +}; |
127 | 28 |
|
128 |
| - await tasks.run().then(ctx => { |
129 |
| - if (ctx.info) { |
130 |
| - console.log(ctx.info); |
131 |
| - } |
132 |
| - }); |
| 29 | +module.exports = async (input, flags) => { |
| 30 | + const {list, ext} = flags; |
| 31 | + |
| 32 | + if (!supportedImageTypes.includes(ext)) { |
| 33 | + throw new TypeError(`Supported format are ${supportedImageTypes.join('|')}`); |
| 34 | + } |
| 35 | + |
| 36 | + const m = getModule(input); |
| 37 | + if (list) { |
| 38 | + await m.downloadList(input, flags); |
| 39 | + } else { |
| 40 | + await m.download(input, flags); |
| 41 | + } |
133 | 42 | };
|
0 commit comments