From b9c832125e68e1353c0a0da577228ce05f767148 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 16 Jul 2023 04:31:55 +0900 Subject: [PATCH] fix(route): copymanga with rate limit and cache (#12805) --- docs/anime.md | 2 +- lib/v2/copymanga/comic.js | 132 +++++++++++++++------------ lib/v2/copymanga/templates/comic.art | 2 +- 3 files changed, 76 insertions(+), 60 deletions(-) diff --git a/docs/anime.md b/docs/anime.md index c17f48c87..a344f927a 100644 --- a/docs/anime.md +++ b/docs/anime.md @@ -758,7 +758,7 @@ Sources ### 漫画更新 - + ## 漫画 DB diff --git a/lib/v2/copymanga/comic.js b/lib/v2/copymanga/comic.js index 843da8116..95a1c9c58 100644 --- a/lib/v2/copymanga/comic.js +++ b/lib/v2/copymanga/comic.js @@ -3,59 +3,70 @@ 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'); -const host = 'copymanga.site'; -const baseUrl = `https://${host}`; -const apiBaseUrl = `https://api.${host}`; - -// 直接调用拷贝漫画的接口 module.exports = async (ctx) => { const { id } = ctx.params; // 用于控制返回的章节数量 - const chapterCnt = Number(ctx.params.chapterCnt || 0); - // 获取漫画列表 - let bHasNextPage = false; - const iReqLimit = (ctx.queries && ctx.queries.limit) || 100; - let iReqOffSet = 0; + 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`; - let chapterArray = []; + 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 === 0 ? undefined : iReqOffSet, - }, - }); - const { code, results } = data; + 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 (code !== 200) { + break; + } - if (results.limit + results.offset < results.total) { - bHasNextPage = true; - } - iReqOffSet += iReqLimit; + if (results.limit + results.offset < results.total) { + bHasNextPage = true; + } + iReqOffSet += iReqLimit; - chapterArray = [...chapterArray, ...results.list]; - } while (bHasNextPage); + chapters = [...chapters, ...results.list]; + } while (bHasNextPage); - chapterArray = chapterArray - .map(({ comic_path_word, uuid, name, size, datetime_created /* , index*/ }) => ({ - link: `${baseUrl}/comic/${comic_path_word}/chapter/${uuid}`, - uuid, - title: name, - size, - pubDate: datetime_created, - // index, - })) - .reverse(); + 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 () => { @@ -69,20 +80,20 @@ module.exports = async (ctx) => { const genResult = async (chapter) => { const { - data: { results }, - } = await got(`${apiBaseUrl}/api/v3/comic/${id}/chapter2/${chapter.uuid}`, { + data: { code, results }, + } = await got(`${apiBaseUrl}/api/v3/comic/${id}/chapter/${chapter.uuid}`, { headers: { webp: 1, }, }); - const pageNumbers = Object.values(results.chapter.words); - const contents = results.chapter.contents - .map((content, index) => ({ - page: pageNumbers[index], - url: content.url, - })) - .sort((a, b) => a.page - b.page); + let contents; + if (code === 210) { + // Request was throttled. Expected available in x seconds. + contents = []; + } else { + contents = results.chapter.contents.map((content) => ({ url: content.url.replace('.c800x.', '.c1500x.') })); + } return { link: chapter.link, @@ -91,20 +102,25 @@ module.exports = async (ctx) => { size: chapter.size, contents, }), - pubDate: parseDate(chapter.pubDate, 'YYYY-MM-DD'), + pubDate: chapter.pubDate, }; }; - let itemsLen = chapterArray.length; + const asyncPoolAll = async (...args) => { + const results = []; + for await (const result of asyncPool(...args)) { + results.push(result); + } + return results; + }; - if (chapterCnt > 0) { - itemsLen = chapterCnt; - } + 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: await Promise.all(chapterArray.slice(0, itemsLen).map((chapter) => ctx.cache.tryGet(chapter.link, () => genResult(chapter)))), + item: items, }; }; diff --git a/lib/v2/copymanga/templates/comic.art b/lib/v2/copymanga/templates/comic.art index e2f092d60..b137f7c71 100644 --- a/lib/v2/copymanga/templates/comic.art +++ b/lib/v2/copymanga/templates/comic.art @@ -1,4 +1,4 @@

{{ size }}p


{{ each contents img }} -{{ img.page }} + {{ /each }}