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
+3
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'));
+60
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,
};
};