From 1edc3b7076128d97a0f02dc9bc2cea90f6b3c953 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Mar 2021 05:52:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=96=B0=E5=8D=8E?= =?UTF-8?q?=E7=A4=BE=E6=96=B0=E9=97=BB=20(#7183)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: NeverBehave --- docs/new-media.md | 6 +++++ lib/router.js | 3 +++ lib/routes/news/whxw.js | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/routes/news/whxw.js diff --git a/docs/new-media.md b/docs/new-media.md index 5991f6eff..517115a45 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2162,6 +2162,12 @@ column 为 third 时可选的 category: +## 新华网 + +### 新华社新闻 + + + ## 新浪专栏 ### 创事记 diff --git a/lib/router.js b/lib/router.js index 0fbf1925c..bb4070595 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3941,6 +3941,9 @@ router.get('/tianyancha/hot', require('./routes/tianyancha/hot')); // King Arthur router.get('/kingarthur/:type', require('./routes/kingarthur/index')); +// 新华网 +router.get('/news/whxw', require('./routes/news/whxw')); + // 游讯网 router.get('/yxdown/recommend', require('./routes/yxdown/recommend')); diff --git a/lib/routes/news/whxw.js b/lib/routes/news/whxw.js new file mode 100644 index 000000000..3498b5e61 --- /dev/null +++ b/lib/routes/news/whxw.js @@ -0,0 +1,52 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'http://www.news.cn'; + const currentUrl = `${rootUrl}/whxw.htm`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('h3 a') + .slice(0, 15) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: item.attr('href'), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + 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(''); + } + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};