diff --git a/lib/v2/wxkol/show.js b/lib/v2/wxkol/show.js index cda8aeabad..f8c91d2aaa 100644 --- a/lib/v2/wxkol/show.js +++ b/lib/v2/wxkol/show.js @@ -1,13 +1,22 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { finishArticleItem } = require('@/utils/wechat-mp'); +const { fetchArticle } = require('@/utils/wechat-mp'); const config = require('@/config').value; +const asyncPool = require('tiny-async-pool'); module.exports = async (ctx) => { const baseUrl = 'https://www.wxkol.com'; const { id } = ctx.params; const url = `${baseUrl}/show/${id}.html`; + const asyncPoolAll = async (...args) => { + const results = []; + for await (const result of asyncPool(...args)) { + results.push(result); + } + return results; + }; + const feedData = await ctx.cache.tryGet( url, async () => { @@ -21,7 +30,7 @@ module.exports = async (ctx) => { const a = item.find('.title a'); return { title: a.attr('title'), - link: a.attr('href'), + link: `${baseUrl}${a.attr('href')}`, }; }); @@ -38,7 +47,28 @@ module.exports = async (ctx) => { false ); - const items = await Promise.all(feedData.feedItem.map((item) => finishArticleItem(ctx, item))); + const urlList = await asyncPoolAll(5, feedData.feedItem, (item) => + ctx.cache.tryGet(item.link, async () => { + const { data: response } = await got(item.link); + const $ = cheerio.load(response); + item.link = `${baseUrl}${$('.source a').attr('href')}`; + return item; + }) + ); + + const items = await asyncPoolAll(5, urlList, async (item) => { + const { title, author, description, summary, pubDate, mpName, link: itemLink } = await fetchArticle(ctx, item.link, true); + + item.title = title; + item.author = author; + item.description = description; + item.summary = summary; + item.pubDate = pubDate; + item.author = mpName; + item.link = itemLink; + + return item; + }); ctx.state.data = { title: feedData.feedTitle,