diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 7f8f4dd838586d..651f9495c49cb7 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -827,6 +827,16 @@ Type 栏目: +## 共同网 + +### 最新报道 + + + +`keyword` 为关键词,由于共同网有许多关键词并不在主页列出,此处不一一列举,可从关键词页的 URL 的最后一级路径中提取。如 `日中关系` 的关键词页 URL 为 `https://china.kyodonews.net/news/japan-china_relationship`, 则将 `japan-china_relationship` 填入 `keyword`。特别地,当填入 `rss` 时,将从共同网官方 RSS 中抓取文章;略去时,将从首页抓取最新报道 (注意:首页更新可能比官方 RSS 稍慢)。 + + + ## 国际金融报栏目 ### 栏目 diff --git a/lib/v2/kyodonews/index.js b/lib/v2/kyodonews/index.js new file mode 100644 index 00000000000000..2df6c220ee1978 --- /dev/null +++ b/lib/v2/kyodonews/index.js @@ -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, + }; +}; diff --git a/lib/v2/kyodonews/maintainer.js b/lib/v2/kyodonews/maintainer.js new file mode 100644 index 00000000000000..0ac170398f25f4 --- /dev/null +++ b/lib/v2/kyodonews/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:language?/:keyword?': ['Rongronggg9'], +}; diff --git a/lib/v2/kyodonews/radar.js b/lib/v2/kyodonews/radar.js new file mode 100644 index 00000000000000..875a36260dfd31 --- /dev/null +++ b/lib/v2/kyodonews/radar.js @@ -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?', + }, + ], + }, +}; diff --git a/lib/v2/kyodonews/router.js b/lib/v2/kyodonews/router.js new file mode 100644 index 00000000000000..c4f4963474d0b9 --- /dev/null +++ b/lib/v2/kyodonews/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:language?/:keyword?', require('./index')); +};