diff --git a/lib/routes/news/whxw.js b/lib/routes/news/whxw.js index cf6bb7f59..30532efa9 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; }) ) );