From 2a2c371b3ef6be19763d2c22f02b40a60c2d62ce Mon Sep 17 00:00:00 2001 From: Haodong DU Date: Mon, 25 Jan 2021 15:53:23 -0500 Subject: [PATCH] feat: caixin: full text of article route (#6773) Co-authored-by: duhd1993 --- lib/routes/caixin/article.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/routes/caixin/article.js b/lib/routes/caixin/article.js index 29fd6d70a..bff813032 100644 --- a/lib/routes/caixin/article.js +++ b/lib/routes/caixin/article.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const cheerio = require('cheerio'); module.exports = async (ctx) => { const response = await got({ @@ -12,15 +13,34 @@ module.exports = async (ctx) => { const data = response.data.data.list; + const items = await Promise.all( + data.map(async (item) => { + const link = item.web_url; + const summary = `

${item.summary}

`; + const key = 'caixin_article_' + link; + + const fullText = await ctx.cache.tryGet(key, async () => { + const result = await got.get(link); + + const $ = cheerio.load(result.data); + + return $('div#Main_Content_Val.text').html(); + }); + + return { + title: item.title, + description: fullText ? summary + fullText : summary, + link: link, + pubDate: new Date(item.time * 1000), + author: item.author_name, + }; + }) + ); + ctx.state.data = { title: `财新网 - 首页`, link: `http://www.caixin.com/`, description: '财新网 - 首页', - item: data.map((item) => ({ - title: item.title, - description: `

${item.summary}

`, - link: item.web_url, - pubDate: new Date(item.time * 1000), - })), + item: items, }; };