Merge pull request #6578 from nczitzk/feature/hkcnews

feat: add 眾新聞眾聞
This commit is contained in:
NeverBehave 2021-01-10 03:58:01 -05:00 committed by GitHub
commit 37bcaba8b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 0 deletions

View File

@ -2116,3 +2116,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

@ -3789,6 +3789,9 @@ 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'));
// AnyTXT
router.get('/anytxt/release-notes', require('./routes/anytxt/release-notes'));
@ -3826,4 +3829,5 @@ router.get('/rfa/:language?/:channel?/:subChannel?', require('./routes/rfa/index
// booth.pm
router.get('/booth.pm/shop/:subdomain', require('./routes/booth-pm/shop'));
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,
};
};