feat: Add Yahoo news (#3720)

This commit is contained in:
KeiLongW
2020-01-16 14:15:40 +08:00
committed by DIYgod
parent 40501a7dfd
commit e875690c07
4 changed files with 83 additions and 0 deletions

View File

@@ -81,3 +81,21 @@ Provides a better reading experience (full text articles) over the official one.
| (空) | dual | en | dual-traditionalchinese | traditionalchinese |
</RouteEn>
## Yahoo
### News
<Route author="KeiLongW" example="/yahoo-news/hk/world" path="/yahoo-news/:region/:category?" :paramsDesc="['Region','Category']">
`Region`
| Hong Kong | Taiwan | US |
| -- | -- | -- |
| hk | tw | en |
`Category`
| All | World | Business | Entertainment | Sports | Health |
| -- | -- | -- | -- | -- | -- |
| (Empty) | world | business | entertainment | sports | health |
</Route>

View File

@@ -96,6 +96,24 @@ Solidot 提供的 feed:
<Route author="xyqfer" example="/the-economist/gre-vocabulary" path="/the-economist/gre-vocabulary" />
## Yahoo
### 新聞
<Route author="KeiLongW" example="/yahoo-news/hk/world" path="/yahoo-news/:region/:category?" :paramsDesc="['地区','类别']">
`地区`
| 香港 | 台灣 | 美國 |
| -- | -- | -- |
| hk | tw | en |
`类別`
| 新聞總集 | 兩岸國際 | 財經 | 娛樂 | 體育 | 健康 |
| -- | -- | -- | -- | -- | -- |
| (空) | world | business | entertainment | sports | health |
</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'));
// yahoo
router.get('/yahoo-news/:region/:category?', require('./routes/yahoo-news/index'));
// 白鲸出海
router.get('/baijing', require('./routes/baijing'));

44
lib/routes/yahoo-news/index.js vendored Executable file
View File

@@ -0,0 +1,44 @@
const got = require('@/utils/got');
const parser = require('@/utils/rss-parser');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const region = ctx.params.region === 'en' ? '' : ctx.params.region.toLowerCase() + '.';
const category = ctx.params.category ? ctx.params.category.toLowerCase() : '';
const rssUrl = `https://${region}news.yahoo.com/rss/${category}`;
const feed = await parser.parseURL(rssUrl);
const title = feed.title;
const link = feed.link;
const description = feed.description;
const filteredItems = feed.items.filter((item) => !item.link.includes('promotions') && item.link.includes('yahoo.com'));
const items = await Promise.all(
filteredItems.map(async (item) => {
const description = await ctx.cache.tryGet(item.link, async () => {
const response = await got({
method: 'get',
url: item.link,
});
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($('article img'));
$descriptionRoot('div.content-root').append($('article p').removeAttr('content'));
return $descriptionRoot.html();
});
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title,
link,
description,
item: items,
};
};