fix(route): fix invalid region and refactor to v2 (#11777)

* fix(route): fix invalid region and refactor to v2

* refactor: change route path

---------
This commit is contained in:
Fatpandac
2023-02-06 01:48:09 +08:00
committed by GitHub
parent fd40d03841
commit 6ace94fcd9
7 changed files with 29 additions and 12 deletions
+1 -1
View File
@@ -680,7 +680,7 @@ Provide full article RSS for WSJ topics.
### News
<RouteEn author="KeiLongW" example="/yahoo-news/hk/world" path="/yahoo-news/:region/:category?" :paramsDesc="['Region','Category']">
<RouteEn author="KeiLongW" example="/yahoo/news/hk/world" path="/yahoo/news/:region/:category?" :paramsDesc="['Region','Category']">
`Region`
| Hong Kong | Taiwan | US |
+1 -1
View File
@@ -530,7 +530,7 @@ Solidot 提供的 feed:
### 新聞
<Route author="KeiLongW" example="/yahoo-news/hk/world" path="/yahoo-news/:region/:category?" :paramsDesc="['地区','类别']">
<Route author="KeiLongW" example="/yahoo/news/hk/world" path="/yahoo/news/:region/:category?" :paramsDesc="['地区','类别']" radar="1">
`地区`
+1 -1
View File
@@ -2315,7 +2315,7 @@ router.get('/chrome/webstore/extensions/:id', lazyloadRouteHandler('./routes/chr
router.get('/rthk-news/:lang/:category', lazyloadRouteHandler('./routes/rthk-news/index'));
// yahoo
router.get('/yahoo-news/:region/:category?', lazyloadRouteHandler('./routes/yahoo-news/index'));
// router.get('/yahoo-news/:region/:category?', lazyloadRouteHandler('./routes/yahoo-news/index'));
// Yahoo!テレビ
router.get('/yahoo-jp-tv/:query', lazyloadRouteHandler('./routes/yahoo-jp-tv/index'));
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/news/:region/:category?': ['KeiLongW'],
};
@@ -4,17 +4,17 @@ const cheerio = require('cheerio');
const { isValidHost } = require('@/utils/valid-host');
module.exports = async (ctx) => {
const region = ctx.params.region === 'en' ? '' : ctx.params.region.toLowerCase() + '.';
if (!isValidHost(region)) {
const region = ctx.params.region === 'en' ? '' : ctx.params.region.toLowerCase();
if (!isValidHost(region) && region !== '') {
throw Error('Invalid region');
}
const category = ctx.params.category ? ctx.params.category.toLowerCase() : '';
const rssUrl = `https://${region}news.yahoo.com/rss/${category}`;
const rssUrl = `https://${region ? 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 filteredItems = feed.items.filter((item) => !item.link.includes('promotions') && new URL(item.link).hostname.match(/.*\.yahoo\.com$/));
const items = await Promise.all(
filteredItems.map(async (item) => {
const description = await ctx.cache.tryGet(item.link, async () => {
@@ -24,10 +24,8 @@ module.exports = async (ctx) => {
});
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:not(header img)'));
$descriptionRoot('div.content-root').append($('article p').removeAttr('content'));
return $descriptionRoot.html();
$('.caas-content-byline-wrapper, .caas-xray-wrapper, .caas-header, .caas-readmore').remove();
return $('.caas-content-wrapper').html();
});
const single = {
title: item.title,
@@ -35,7 +33,7 @@ module.exports = async (ctx) => {
pubDate: item.pubDate,
link: item.link,
};
return Promise.resolve(single);
return single;
})
);
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
'yahoo.com': {
_name: 'Yahoo',
'.': [
{
title: '新闻',
docs: 'https://docs.rsshub.app/traditional-media.html#yahoo',
source: ['/'],
target: '/yahoo/news/:region/:category?',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/:region/:category?', require('./news/index'));
};