From 4208e87e2473b41a4e2a724edbc083af4bdbab83 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:40:34 +0800 Subject: [PATCH] fix(route/apnews): Add throttle to topics. (#18106) * fix(route/apnews): Add throttle to topics. * Update topics.ts --- lib/routes/apnews/topics.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/routes/apnews/topics.ts b/lib/routes/apnews/topics.ts index bd89c1ba353a7b..a4f65f319d7c4c 100644 --- a/lib/routes/apnews/topics.ts +++ b/lib/routes/apnews/topics.ts @@ -1,7 +1,7 @@ import { Route, ViewType } from '@/types'; import got from '@/utils/got'; import { load } from 'cheerio'; -import { fetchArticle, removeDuplicateByKey } from './utils'; +import { asyncPoolAll, fetchArticle, removeDuplicateByKey } from './utils'; const HOME_PAGE = 'https://apnews.com'; export const route: Route = { @@ -41,17 +41,16 @@ async function handler(ctx) { const response = await got(url); const $ = load(response.data); - const items = await Promise.all( - $(':is(.PagePromo-content, .PageListStandardE-leadPromo-info) bsp-custom-headline') - .toArray() - .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : Infinity) - .map((e) => ({ - title: $(e).find('span.PagePromoContentIcons-text').text(), - link: $(e).find('a').attr('href'), - })) - .filter((e) => typeof e.link === 'string') - .map((item) => (ctx.req.query('fulltext') === 'true' ? fetchArticle(item) : item)) - ); + const list = $(':is(.PagePromo-content, .PageListStandardE-leadPromo-info) bsp-custom-headline') + .toArray() + .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : Infinity) + .map((e) => ({ + title: $(e).find('span.PagePromoContentIcons-text').text(), + link: $(e).find('a').attr('href'), + })) + .filter((e) => typeof e.link === 'string'); + + const items = ctx.req.query('fulltext') === 'true' ? await asyncPoolAll(10, list, (item) => fetchArticle(item)) : list; return { title: $('title').text(),