feat(route): add kyodonews (#9435)

Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
This commit is contained in:
Rongrong
2022-04-01 13:02:17 +08:00
committed by GitHub
parent c6fbc9ecf2
commit df846c497c
5 changed files with 137 additions and 0 deletions
+10
View File
@@ -827,6 +827,16 @@ Type 栏目:
<Route author="nczitzk" example="/pts/dailynews" path="/pts/dailynews"/>
## 共同网
### 最新报道
<Route author="Rongronggg9" example="/kyodonews" path="/kyodonews/:language?/:keyword?" :paramsDesc="['语言: `china` = 简体中文 (默认), `tchina` = 繁體中文', '关键词']">
`keyword` 为关键词,由于共同网有许多关键词并不在主页列出,此处不一一列举,可从关键词页的 URL 的最后一级路径中提取。如 `日中关系` 的关键词页 URL 为 `https://china.kyodonews.net/news/japan-china_relationship`, 则将 `japan-china_relationship` 填入 `keyword`。特别地,当填入 `rss` 时,将从共同网官方 RSS 中抓取文章;略去时,将从首页抓取最新报道 (注意:首页更新可能比官方 RSS 稍慢)。
</Route>
## 国际金融报栏目
### 栏目
+88
View File
@@ -0,0 +1,88 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const language = ctx.params.language ?? 'china';
const keyword = ctx.params.keyword === 'RSS' ? 'rss' : ctx.params.keyword ?? '';
// raise error for invalid languages
if (!['china', 'tchina'].includes(language)) {
throw Error('Invalid language');
}
const rootUrl = `https://${language}.kyodonews.net`;
const currentUrl = `${rootUrl}/${keyword ? (keyword === 'rss' ? 'rss/news.xml' : `news/${keyword}`) : ''}`;
let response;
try {
response = await got(currentUrl);
} catch (e) {
throw e.response && e.response.statusCode === 404 ? new Error('Invalid keyword') : e;
}
const $ = cheerio.load(response.data, { xmlMode: keyword === 'rss' });
let title, description, items;
if (keyword === 'rss') {
title = $('channel > title').text();
description = $('channel > description').text();
items = $('item')
.map((_, item) => {
const $item = $(item);
const link = $item.find('link').text();
// const pubDate = $item.find('pubDate').text();
return {
link,
// pubDate, // no need to normalize because it's from a valid RSS feed
};
})
.get();
} else {
title = $('head > title').text();
description = $('meta[name="description"]').attr('content');
items = $('div.sec-latest > ul > li')
.map((_, item) => {
item = $(item);
const link = item.find('a').attr('href');
return {
link: `${link.startsWith('http') ? '' : rootUrl}${link}`,
};
})
.get();
}
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const $ = cheerio.load(detailResponse.data);
item.title = $('head > title').text();
item.author = $('meta[name="author"]').attr('content');
item.description = $('div.article-body')
.html()
.replace(/(完)(?=<\/p>\s*$)/m, '');
const pubDate_match = $('script[type="application/ld+json"]')
.html()
.match(/"datePublished":"([\d\s-:]*?)"/);
if (pubDate_match) {
item.pubDate = timezone(parseDate(pubDate_match[1]), 9);
}
return item;
})
)
);
ctx.state.data = {
title,
description,
link: currentUrl,
item: items,
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/:language?/:keyword?': ['Rongronggg9'],
};
+33
View File
@@ -0,0 +1,33 @@
module.exports = {
'kyodonews.net': {
_name: '共同网',
china: [
{
title: '最新报道',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao',
source: '/',
target: '/kyodonews/china',
},
{
title: '关键词',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao',
source: '/news/:keyword',
target: '/kyodonews/china/:keyword?',
},
],
tchina: [
{
title: '最新報道',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao',
source: '/',
target: '/kyodonews/tchina/:keyword?',
},
{
title: '關鍵詞',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao',
source: '/news/:keyword',
target: '/kyodonews/tchina/:keyword?',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:language?/:keyword?', require('./index'));
};