From ec1f718558014353eff967ed1fa3915901c8d378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Fri, 26 Oct 2018 00:37:47 +0800 Subject: [PATCH] =?UTF-8?q?Enhancement:=20=E5=8D=97=E6=96=B9=E5=91=A8?= =?UTF-8?q?=E6=9C=AB=E5=85=A8=E6=96=87=E8=BE=93=E5=87=BA=20(#964)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现 #933 - 南方周末 --- routes/infzm/news.js | 63 +++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/routes/infzm/news.js b/routes/infzm/news.js index 2ea2343f0..fbb56252a 100644 --- a/routes/infzm/news.js +++ b/routes/infzm/news.js @@ -17,30 +17,55 @@ module.exports = async (ctx) => { const data = response.data; const $ = cheerio.load(data); const list = $('article'); + const resultItem = await Promise.all( + list + .map(async (item, index) => { + item = $(index); + const info = item + .find('div.clearfix>div>p.articleInfo') + .text() + .trim() + .split(/\s+/); + const date = new Date(`${info.slice(-2)[0]}T${info.slice(-1)[0]}`); + const link = item.find('a').attr('href'); + const id = link.match(/content\/(\d+)/)[1]; + let description; + + if (id) { + const key = `infzm: ${link}`; + const value = await ctx.cache.get(key); + + if (value) { + description = value; + } else { + const response = await axios({ + method: 'get', + url: `http://api.infzm.com/mobile/contents/${id}`, + }); + + description = response.data.data.content.fulltext; + ctx.cache.set(key, description, 24 * 60 * 60); + } + } else { + description = `${item.find('div.clearfix>div>p.intro').text()}`; + } + + return { + title: item.find('div.articleTitle>h>a').text(), + description, + pubDate: date.toUTCString(), + link, + }; + }) + .get() + ); + ctx.state.data = { title: `${$('title').text()}-${$('#sortName') .find('a') .text()}`, link: url, description: $('meta[name="description"]').attr('content'), - item: - list && - list - .map((item, index) => { - item = $(index); - const info = item - .find('div.clearfix>div>p.articleInfo') - .text() - .trim() - .split(/\s+/); - const date = new Date(`${info.slice(-2)[0]}T${info.slice(-1)[0]}`); - return { - title: item.find('div.articleTitle>h>a').text(), - description: `${item.find('div.clearfix>div>p.intro').text()}`, - pubDate: date.toUTCString(), - link: item.find('a').attr('href'), - }; - }) - .get(), + item: resultItem, }; };