Skip to content

Commit

Permalink
feat(route): add kyodonews (DIYgod#9435)
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
  • Loading branch information
Rongronggg9 authored and RikkaBlue committed Apr 9, 2022
1 parent 9ca9ada commit 40f6401
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
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'));
};

0 comments on commit 40f6401

Please sign in to comment.