From e47c9c1d8c7f13cd7d950882cdc266bf0cedbc4a Mon Sep 17 00:00:00 2001 From: miles Date: Mon, 5 Sep 2022 08:05:09 +0000 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E5=A3=B9=E8=98=8B=E6=96=B0?= =?UTF-8?q?=E8=81=9E=E7=B6=B2=E7=8D=B2=E5=8F=96=E5=A4=B1=E6=95=97=20(#1069?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 壹蘋新聞網獲取失敗 * fix(route): rate limit --- lib/v2/nextapple/realtime.js | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/v2/nextapple/realtime.js b/lib/v2/nextapple/realtime.js index 70712d3ecb..dd0836f32d 100644 --- a/lib/v2/nextapple/realtime.js +++ b/lib/v2/nextapple/realtime.js @@ -1,40 +1,40 @@ const got = require('@/utils/got'); const { parseDate } = require('@/utils/parse-date'); const cheerio = require('cheerio'); +const asyncPool = require('tiny-async-pool'); module.exports = async (ctx) => { const category = ctx.params.category ?? 'new'; const currentUrl = `https://tw.nextapple.com/realtime/${category}`; const response = await got(currentUrl); const $ = cheerio.load(response.data); - const items = await Promise.all( - $('article') - .get() - .map((item) => { - const link = $(item).find('.post-title').attr('href'); - return ctx.cache.tryGet(link, async () => { - const response = await got(link); - const $ = cheerio.load(response.data); - const mainContent = $('#main-content'); - const titleElement = mainContent.find('header h1'); - const title = titleElement.text(); - titleElement.remove(); - const postMetaElement = mainContent.find('.post-meta'); - const category = postMetaElement.find('.category').text(); - const pubDate = parseDate(postMetaElement.find('time').attr('datetime')); - postMetaElement.remove(); - $('.post-comments').remove(); + const items = []; + for await (const item of asyncPool(5, $('article.infScroll'), (item) => { + const link = $(item).find('.post-title').attr('href'); + return ctx.cache.tryGet(link, async () => { + const response = await got(link); + const $ = cheerio.load(response.data); + const mainContent = $('#main-content'); + const titleElement = mainContent.find('header h1'); + const title = titleElement.text(); + titleElement.remove(); + const postMetaElement = mainContent.find('.post-meta'); + const category = postMetaElement.find('.category').text(); + const pubDate = parseDate(postMetaElement.find('time').attr('datetime')); + postMetaElement.remove(); + $('.post-comments').remove(); - return { - title, - description: mainContent.html(), - category, - pubDate, - link, - }; - }); - }) - ); + return { + title, + description: mainContent.html(), + category, + pubDate, + link, + }; + }); + })) { + items.push(item); + } ctx.state.data = { title: $('title').text(),