feat: Add rthk (#3718)

This commit is contained in:
KeiLongW 2020-01-16 14:19:55 +08:00 committed by DIYgod
parent e875690c07
commit 8a4e83aa2f
4 changed files with 95 additions and 0 deletions

View File

@ -44,6 +44,22 @@ Support major channels, refer to [BBC RSS feeds](https://www.bbc.co.uk/news/1062
<RouteEn author="Andiedie" example="/nhk/news_web_easy" path="/nhk/news_web_easy"/>
## RTHK
### News
RTHK offical provides full text RSS, check the offical website for detail information: <https://news.rthk.hk/rthk/en/rss.htm>
This route adds the missing photo and Link element. (Offical RSS doesn't have Link element may cause issue on some RSS client)
<Route author="KeiLongW" example="/rthk-news/hk/international" path="/rthk-news/:lang/:category" :paramsDesc="['LanguageTraditional Chinese`hk`English`en`','Category']">
| local | greaterchina | international | finance | sport |
| ---------- | ------------------ | ------------- | ------------ | ---------- |
| Local News | Greater China News | World News | Finance News | Sport News |
</Route>
## The Economist
### Category

View File

@ -360,6 +360,22 @@ category 对应的关键词有
<Route author="hoilc" example="/hk01/tag/2787" path="/hk01/tag/:id" :paramsDesc="['标签id, 可在URL中找到']"/>
## 香港電台
### 新聞
香港電台官方已有提供全文 RSS詳細可前往官方網站 <https://news.rthk.hk/rthk/ch/rss.htm>
此路由主要補回官方 RSS 缺少的圖片以及 Link 元素。(官方 RSS 沒有 Link 元素可能導致某些 RSS 客戶端出現問題)
<Route author="KeiLongW" example="/rthk-news/hk/international" path="/rthk-news/:lang/:category" :paramsDesc="['语言,繁体`hk`,英文`en`','类别']">
| local | greaterchina | international | finance | sport |
| -------- | ------------ | ------------- | -------- | -------- |
| 本地新聞 | 大中華新聞 | 國際新聞 | 財經新聞 | 體育新聞 |
</Route>
## 新京报
### 栏目

View File

@ -2101,6 +2101,9 @@ router.get('/mlog-club/projects', require('./routes/mlog-club/projects'));
// Chrome 网上应用店
router.get('/chrome/webstore/extensions/:id', require('./routes/chrome/extensions'));
// RTHK
router.get('/rthk-news/:lang/:category', require('./routes/rthk-news/index'));
// yahoo
router.get('/yahoo-news/:region/:category?', require('./routes/yahoo-news/index'));

View File

@ -0,0 +1,60 @@
const got = require('@/utils/got');
const parser = require('@/utils/rss-parser');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
let category = ctx.params.category.toLowerCase();
const language = ctx.params.lang.toLowerCase();
let languageUrlKey;
switch (language) {
case 'hk':
languageUrlKey = 'c';
break;
case 'en':
languageUrlKey = 'e';
break;
default:
languageUrlKey = 'c';
break;
}
if (languageUrlKey !== 'c' || category !== 'greaterchina') {
category = languageUrlKey + category;
}
const rssUrl = `https://rthk.hk/rthk/news/rss/${languageUrlKey}_expressnews_${category}.xml`;
const feed = await parser.parseURL(rssUrl);
const title = feed.title;
const link = feed.link;
const description = feed.description;
const items = await Promise.all(
feed.items.map(async (item) => {
const description = await ctx.cache.tryGet(item.guid, async () => {
const response = await got({
method: 'get',
url: item.guid,
});
const $ = cheerio.load(response.data);
const $descriptionRoot = cheerio.load('<div><div class="img-root"></div><div class="content-root"></div></div>');
$descriptionRoot('div.img-root').append($('div.itemImageContainer img.imgPhotoAfterLoad'));
$descriptionRoot('div.content-root').append(item.content.replace(/\r\n/g, '<br/>'));
return $descriptionRoot.html();
});
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.guid,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title,
link,
description,
item: items,
};
};