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(),