From 8bee770594950dd57137f3ae7cc3bcde7cdac605 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 7 Jul 2020 17:53:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=B8=87=E7=BB=B4=E8=AF=BB?= =?UTF-8?q?=E8=80=85=20(#5123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 6 +++++ lib/router.js | 3 +++ lib/routes/creaders/headline.js | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/routes/creaders/headline.js diff --git a/docs/bbs.md b/docs/bbs.md index fa0deaa51..a80134400 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -337,6 +337,12 @@ pageClass: routes +## 万维读者 + +### 焦点新闻 + + + ## 小米社区 ### 圈子 diff --git a/lib/router.js b/lib/router.js index a462b163c..684c9457b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2932,6 +2932,9 @@ router.get('/gelonghui/keyword/:keyword', require('./routes/gelonghui/keyword')) // 光谷社区 router.get('/guanggoo/:caty', require('./routes/guanggoo/index')); +// 万维读者 +router.get('/creaders/headline', require('./routes/creaders/headline')); + // 金山词霸 router.get('/iciba/:days?/:img_type?', require('./routes/iciba/index')); diff --git a/lib/routes/creaders/headline.js b/lib/routes/creaders/headline.js new file mode 100644 index 000000000..c56cd591a --- /dev/null +++ b/lib/routes/creaders/headline.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const currentUrl = 'http://news.creaders.net/headline/'; + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + const list = $('ul.newslist li') + .slice(0, 10) + .map((_, item) => { + item = $(item); + const a = item.find('a').eq(1); + return { + title: a.text(), + link: a.attr('href'), + pubDate: new Date(item.find('time').text() + ' GMT+8').toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(iconv.decode(res.data, 'gbk')); + + item.description = content('#newsContent').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: '万维读者 - 焦点新闻', + link: currentUrl, + item: items, + }; +};