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, + }; +};