Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add kyodonews #9435

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

## 国际金融报栏目

### 栏目
Expand Down
88 changes: 88 additions & 0 deletions lib/v2/kyodonews/index.js
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions lib/v2/kyodonews/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/:language?/:keyword?': ['Rongronggg9'],
};
33 changes: 33 additions & 0 deletions lib/v2/kyodonews/radar.js
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions lib/v2/kyodonews/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:language?/:keyword?', require('./index'));
};