feat: add 眾新聞眾聞

This commit is contained in:
nczitzk 2020-12-31 08:08:50 +08:00
parent b1ae777b87
commit 08d5a57c2a
3 changed files with 75 additions and 0 deletions

View File

@ -2109,3 +2109,15 @@ QueryString:
### 全文
<Route author="HenryQW" example="/zzz" path="/zzz/index"/>
## 眾新聞
### 眾聞
<Route author="nczitzk" example="/hkcnews/news" path="/hkcnews/news/:category?" :paramsDesc="['分类,见下表,默认为全部']">
| 全部 | 經濟 | 社會 | 生活 | 政治 | 國際 | 台灣 | 人物 | 中國 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| | 13 | 15 | 14 | 12 | 16 | 20 | 21 | 19 |
</Route>

View File

@ -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;

View File

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