* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
124 lines
4.1 KiB
JavaScript
124 lines
4.1 KiB
JavaScript
const cheerio = require('cheerio');
|
|
const got = require('@/utils/got');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
const { art } = require('@/utils/render');
|
|
const path = require('path');
|
|
const config = require('@/config').value;
|
|
const asyncPool = require('tiny-async-pool');
|
|
|
|
module.exports = async (ctx) => {
|
|
const { id } = ctx.params;
|
|
// 用于控制返回的章节数量
|
|
const chapterCnt = Number(ctx.params.chapterCnt || 10);
|
|
// 直接调用拷贝漫画的接口
|
|
const host = 'copymanga.site';
|
|
const baseUrl = `https://${host}`;
|
|
const apiBaseUrl = `https://api.${host}`;
|
|
const strBaseUrl = `${apiBaseUrl}/api/v3/comic/${id}/group/default/chapters`;
|
|
const iReqLimit = 500;
|
|
// 获取漫画列表
|
|
const chapterArray = await ctx.cache.tryGet(
|
|
strBaseUrl,
|
|
async () => {
|
|
let bHasNextPage = false;
|
|
let chapters = [];
|
|
let iReqOffSet = 0;
|
|
|
|
do {
|
|
bHasNextPage = false;
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const { data } = await got(strBaseUrl, {
|
|
headers: {
|
|
platform: 1,
|
|
},
|
|
searchParams: {
|
|
limit: iReqLimit,
|
|
offset: iReqOffSet,
|
|
},
|
|
});
|
|
const { code, results } = data;
|
|
|
|
if (code !== 200) {
|
|
break;
|
|
}
|
|
|
|
if (results.limit + results.offset < results.total) {
|
|
bHasNextPage = true;
|
|
}
|
|
iReqOffSet += iReqLimit;
|
|
|
|
chapters = [...chapters, ...results.list];
|
|
} while (bHasNextPage);
|
|
|
|
chapters = chapters
|
|
.map(({ comic_path_word, uuid, name, size, datetime_created, ordered /* , index*/ }) => ({
|
|
link: `${baseUrl}/comic/${comic_path_word}/chapter/${uuid}`,
|
|
uuid,
|
|
title: name,
|
|
size,
|
|
pubDate: parseDate(datetime_created, 'YYYY-MM-DD'),
|
|
ordered,
|
|
// index,
|
|
}))
|
|
.sort((a, b) => b.ordered - a.ordered);
|
|
|
|
return chapters;
|
|
},
|
|
config.cache.routeExpire,
|
|
false
|
|
);
|
|
|
|
// 获取漫画标题、介绍
|
|
const { bookTitle, bookIntro } = await ctx.cache.tryGet(`${baseUrl}/comic/${id}`, async () => {
|
|
const { data } = await got(`${baseUrl}/comic/${id}`);
|
|
const $ = cheerio.load(data);
|
|
return {
|
|
bookTitle: $('.comicParticulars-title-right > ul > li > h6').text(),
|
|
bookIntro: $('.intro').text(),
|
|
};
|
|
});
|
|
|
|
const genResult = async (chapter) => {
|
|
const {
|
|
data: { code, results },
|
|
} = await got(`${apiBaseUrl}/api/v3/comic/${id}/chapter/${chapter.uuid}`, {
|
|
headers: {
|
|
webp: 1,
|
|
},
|
|
});
|
|
|
|
const contents =
|
|
code === 210
|
|
? [] // Request was throttled. Expected available in x seconds.
|
|
: results.chapter.contents.map((content) => ({ url: content.url.replace('.c800x.', '.c1500x.') }));
|
|
|
|
return {
|
|
link: chapter.link,
|
|
title: chapter.title,
|
|
description: art(path.join(__dirname, './templates/comic.art'), {
|
|
size: chapter.size,
|
|
contents,
|
|
}),
|
|
pubDate: chapter.pubDate,
|
|
};
|
|
};
|
|
|
|
const asyncPoolAll = async (...args) => {
|
|
const results = [];
|
|
for await (const result of asyncPool(...args)) {
|
|
results.push(result);
|
|
}
|
|
return results;
|
|
};
|
|
|
|
const result = await asyncPoolAll(3, chapterArray.slice(0, chapterCnt), (chapter) => ctx.cache.tryGet(chapter.link, () => genResult(chapter)));
|
|
const items = [...result, ...chapterArray.slice(chapterCnt)];
|
|
|
|
ctx.state.data = {
|
|
title: `拷贝漫画 - ${bookTitle}`,
|
|
link: `${baseUrl}/comic/${id}`,
|
|
description: bookIntro,
|
|
item: items,
|
|
};
|
|
};
|