From 08d5a57c2a300539c751c6d612ced7e1e5dd1f1f Mon Sep 17 00:00:00 2001 From: nczitzk Date: Thu, 31 Dec 2020 08:08:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=9C=BE=E6=96=B0=E8=81=9E?= =?UTF-8?q?=E7=9C=BE=E8=81=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 12 ++++++++ lib/router.js | 3 ++ lib/routes/hkcnews/news.js | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 lib/routes/hkcnews/news.js diff --git a/docs/new-media.md b/docs/new-media.md index dea896d57..19a18d9fd 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2109,3 +2109,15 @@ QueryString: ### 全文 + +## 眾新聞 + +### 眾聞 + + + +| 全部 | 經濟 | 社會 | 生活 | 政治 | 國際 | 台灣 | 人物 | 中國 | +| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | +| | 13 | 15 | 14 | 12 | 16 | 20 | 21 | 19 | + + diff --git a/lib/router.js b/lib/router.js index 5bb0f656c..cf5c49c47 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3780,4 +3780,7 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog')); // Caixin Latest router.get('/caixin/latest', require('./routes/caixin/latest')); +// 眾新聞 +router.get('/hkcnews/news/:category?', require('./routes/hkcnews/news')); + module.exports = router; diff --git a/lib/routes/hkcnews/news.js b/lib/routes/hkcnews/news.js new file mode 100644 index 000000000..1c040d854 --- /dev/null +++ b/lib/routes/hkcnews/news.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const titles = { + '': '眾聞', + 13: '經濟', + 15: '社會', + 14: '生活', + 12: '政治', + 16: '國際', + 20: '台灣', + 21: '人物', + 19: '中國', +}; + +module.exports = async (ctx) => { + const category = ctx.params.category || ''; + + const rootUrl = 'https://www.hkcnews.com'; + const apiUrl = `${rootUrl}/data/newsposts${category === '' ? '' : `/${category}`}?page=1`; + const response = await got({ + method: 'get', + url: apiUrl, + }); + + const list = response.data.items.map((item) => { + const $ = cheerio.load(item); + const news = $('.article-block-body a'); + const date = $('.line').eq(1).text().split('.').reverse().join('-'); + + return { + title: news.text(), + link: `${rootUrl}/${news.attr('href')}`, + pubDate: new Date(date.length === 8 ? `${new Date().getFullYear().toString().slice(0, 2)}${date}` : date).toUTCString(), + }; + }); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('.article-content').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `眾新聞 - ${titles[category]}`, + link: `${rootUrl}/news${category === '' ? '' : `/${category}/${titles[category]}`}`, + item: items, + }; +};