From 8a8e91f04eb76d31ee54ab98b43e9e3ea16b573d Mon Sep 17 00:00:00 2001 From: auto-bot-ty <75887908+auto-bot-ty@users.noreply.github.com> Date: Thu, 23 Sep 2021 12:48:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20fix=20pubDate=20for=20=E6=96=B0?= =?UTF-8?q?=E5=8D=8E=E7=A4=BE=E6=96=B0=E9=97=BB=20(#7982)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/news/whxw.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/routes/news/whxw.js b/lib/routes/news/whxw.js index cf6bb7f59b..30532efa9d 100644 --- a/lib/routes/news/whxw.js +++ b/lib/routes/news/whxw.js @@ -1,5 +1,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); module.exports = async (ctx) => { const rootUrl = 'http://www.news.cn'; @@ -12,7 +14,7 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); const list = $('h3 a') - .slice(0, 15) + .slice(0, 35) .map((_, item) => { item = $(item); return { @@ -25,20 +27,26 @@ module.exports = async (ctx) => { const items = await Promise.all( list.map((item) => ctx.cache.tryGet(item.link, async () => { + let detailResponse; try { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.description = content('#detail').html(); - item.pubDate = new Date(detailResponse.data.match(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)[1] + ' GMT+8').toUTCString(); - - return item; - } catch (e) { - return Promise.resolve(''); + detailResponse = await got(item.link); + } catch (error) { + item.status = 404; } + if (item.status !== 404) { + const content = cheerio.load(detailResponse.data); + const date = content('.header-time.left').text(); + const author = + content('.editor').text() || + content('.p-jc') + .text() + .replace(/[\r\n]/g, ''); + item.author = author.match(/责任编辑.(.*)/)[1].trim(); + content('#articleEdit').remove(); + item.description = content('#detail').html(); + item.pubDate = timezone(parseDate(date, 'YYYYMM/DD HH:mm:ss'), +8); + } + return item; }) ) );