Skip to content

Commit 416cc5d

Browse files
committed
重写解析规则
1 parent ae5f329 commit 416cc5d

11 files changed

+455
-512
lines changed

.prettierrc

-11
This file was deleted.

package.json

+54-56
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
11
{
2-
"name": "amanga",
3-
"version": "0.5.3",
4-
"description": "下载漫画咯",
5-
"author": "mino <[email protected]>",
6-
"repository": "minosss/amanga",
7-
"main": "source/amanga.js",
8-
"license": "MIT",
9-
"scripts": {
10-
"test": "prettier-check source/**/*.js"
11-
},
12-
"bin": {
13-
"amanga": "source/cli.js"
14-
},
15-
"files": [
16-
"source"
17-
],
18-
"dependencies": {
19-
"cheerio": "^1.0.0-rc.3",
20-
"download": "^7.1.0",
21-
"global-agent": "^2.1.1",
22-
"got": "^9.6.0",
23-
"listr": "^0.14.3",
24-
"log-symbols": "^2.2.0",
25-
"lz-string": "^1.4.4",
26-
"meow": "^5.0.0",
27-
"ow": "^0.12.0",
28-
"sharp": "^0.22.0"
29-
},
30-
"keywords": [
31-
"cli-app",
32-
"cli",
33-
"downloader"
34-
],
35-
"devDependencies": {
36-
"husky": "^1.3.1",
37-
"lint-staged": "^8.1.5",
38-
"prettier": "^1.16.4",
39-
"prettier-check": "^2.0.0"
40-
},
41-
"husky": {
42-
"hooks": {
43-
"pre-commit": "lint-staged"
44-
}
45-
},
46-
"lint-staged": {
47-
"*.{js,json,md}": [
48-
"prettier --write",
49-
"git add"
50-
]
51-
},
52-
"engines": {
53-
"node": ">=8"
54-
},
55-
"publishConfig": {
56-
"access": "public"
57-
}
2+
"name": "amanga",
3+
"version": "0.5.3",
4+
"description": "Manga Downloader",
5+
"author": "mino <[email protected]>",
6+
"repository": "minosss/amanga",
7+
"main": "source/amanga.js",
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "prettier-check source/**/*.js"
11+
},
12+
"bin": {
13+
"amanga": "source/cli.js"
14+
},
15+
"files": [
16+
"source"
17+
],
18+
"dependencies": {
19+
"cheerio": "^1.0.0-rc.3",
20+
"download": "^7.1.0",
21+
"global-agent": "^2.1.1",
22+
"got": "^9.6.0",
23+
"lz-string": "^1.4.4",
24+
"meow": "^5.0.0",
25+
"ora": "^3.4.0",
26+
"sharp": "^0.22.0"
27+
},
28+
"keywords": [
29+
"cli-app",
30+
"cli",
31+
"downloader"
32+
],
33+
"devDependencies": {
34+
"husky": "^1.3.1",
35+
"lint-staged": "^8.1.5",
36+
"prettier": "^1.16.4",
37+
"prettier-check": "^2.0.0"
38+
},
39+
"husky": {
40+
"hooks": {
41+
"pre-commit": "lint-staged"
42+
}
43+
},
44+
"lint-staged": {
45+
"*.{js,json,md}": [
46+
"prettier --write",
47+
"git add"
48+
]
49+
},
50+
"engines": {
51+
"node": ">=8"
52+
},
53+
"publishConfig": {
54+
"access": "public"
55+
}
5856
}

prettier.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://prettier.io/docs/en/options.html
2+
module.exports = {
3+
printWidth: 97,
4+
tabWidth: 4,
5+
useTabs: true,
6+
semi: true,
7+
singleQuote: true,
8+
jsxSingleQuote: true,
9+
jsxBracketSameLine: false,
10+
bracketSpacing: false,
11+
endOfLine: 'lf',
12+
trailingComma: 'es5',
13+
htmlWhitespaceSensitivity: 'ignore',
14+
};

source/amanga.js

+36-127
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,42 @@
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-
81
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+
};
99

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, /([^.]+)/);
9221

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+
}
9925

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+
};
12728

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+
}
13342
};

0 commit comments

Comments
 (0)