From 2a164517aa928ae7e0a86cd341213d24fee29561 Mon Sep 17 00:00:00 2001 From: Hendricks Zheng <54502919+HendricksZheng@users.noreply.github.com> Date: Sun, 15 Sep 2019 12:51:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=BC=81=E9=B9=85=E5=8F=B7?= =?UTF-8?q?=E5=85=A8=E6=96=87=E8=BE=93=E5=87=BA=20(#3072)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/tencent/news/author.js | 50 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/routes/tencent/news/author.js b/lib/routes/tencent/news/author.js index a5e9b8158c..03b870a3ff 100644 --- a/lib/routes/tencent/news/author.js +++ b/lib/routes/tencent/news/author.js @@ -1,4 +1,7 @@ const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const date = require('@/utils/date'); +const iconv = require('iconv-lite'); module.exports = async (ctx) => { const mid = ctx.params.mid; @@ -12,24 +15,39 @@ module.exports = async (ctx) => { const reponse = response.data; const title = reponse.mediainfo.name; const description = reponse.mediainfo.intro; - const list = reponse.data; + const list = reponse.data.splice(0, 10); - const items = list.map((item) => { - const title = item.title; - const date = Date(item.timestamp); - const itemUrl = item.vurl; - const author = item.source; - const description = item.abstract; + const items = await Promise.all( + list.map(async (item) => { + const title = item.title; + const unixTimestamp = new Date(item.timestamp * 1000); + const pubDate = date(unixTimestamp.toLocaleString(), 8); + const itemUrl = item.vurl; + const author = item.source; + const abstract = item.abstract; + + const response = await ctx.cache.tryGet( + itemUrl, + async () => + (await got.get(itemUrl, { + responseType: 'buffer', + })).data + ); + const html = iconv.decode(response, 'gbk'); + const $ = cheerio.load(html, { decodeEntities: false }); + const article = $('div.content-article'); + + const single = { + title, + description: article.html() || abstract, + link: itemUrl, + author, + pubDate, + }; + return Promise.resolve(single); + }) + ); - const single = { - title, - description, - link: itemUrl, - author, - pubDate: date, - }; - return single; - }); ctx.state.data = { title: title, description: description,