From 5f4cf8a2f02ec940f1cb7a1872dfed5ae1d8dee1 Mon Sep 17 00:00:00 2001 From: yuxinliu-alex Date: Tue, 19 Apr 2022 19:17:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=8F=82=E8=80=83=E6=B6=88?= =?UTF-8?q?=E6=81=AF=20(#9564)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 参考消息 * fixed the router's & ref bugs * docs: fix example * fix: prefer https * fix: radar Co-authored-by: alex --- docs/traditional-media.md | 10 ++++ lib/v2/cankaoxiaoxi/index.js | 81 +++++++++++++++++++++++++++++++ lib/v2/cankaoxiaoxi/maintainer.js | 3 ++ lib/v2/cankaoxiaoxi/radar.js | 61 +++++++++++++++++++++++ lib/v2/cankaoxiaoxi/router.js | 3 ++ 5 files changed, 158 insertions(+) create mode 100644 lib/v2/cankaoxiaoxi/index.js create mode 100644 lib/v2/cankaoxiaoxi/maintainer.js create mode 100644 lib/v2/cankaoxiaoxi/radar.js create mode 100644 lib/v2/cankaoxiaoxi/router.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index d72e09276..a51ecd5a8 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -521,6 +521,16 @@ Category 列表: +## 参考消息 + +### 新闻分类 + + + +| 中国 | 国际 | 军事 | 台海 | 财经 | 科技 | 文化 | +| ---------- | ---------- | ------------- | ----------- | ------------ | --------------- | ------------ | +| china_news | world_news | military_news | taiwan_news | finance_news | technology_news | culture_news | + ## 朝日新聞中文網(繁體中文版) ::: tip 提示 diff --git a/lib/v2/cankaoxiaoxi/index.js b/lib/v2/cankaoxiaoxi/index.js new file mode 100644 index 000000000..ffe22e966 --- /dev/null +++ b/lib/v2/cankaoxiaoxi/index.js @@ -0,0 +1,81 @@ +const got = require('@/utils/got'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); +const cheerio = require('cheerio'); + +const nodes = { + china_news: { + title: '中国', + url: 'https://china.cankaoxiaoxi.com/', + }, + world_news: { + title: '国际', + url: 'https://world.cankaoxiaoxi.com/', + }, + military_news: { + title: '军事', + url: 'https://mil.cankaoxiaoxi.com/', + }, + taiwan_news: { + title: '台海', + url: 'https://tw.cankaoxiaoxi.com/', + }, + finance_news: { + title: '财经', + url: 'https://finance.cankaoxiaoxi.com/', + }, + technology_news: { + title: '科技', + url: 'https://science.cankaoxiaoxi.com/', + }, + culture_news: { + title: '文化', + url: 'https://culture.cankaoxiaoxi.com/', + }, +}; + +module.exports = async (ctx) => { + const category = ctx.params.category; + + const currentUrl = nodes[category].url; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('a', '.toutiao') + .filter(function () { + return $(this).text() !== ''; + }) + .map((_, item) => ({ + link: $(item).attr('href'), + })) + .get(); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + item.title = content('h1.articleHead').text(); + item.pubDate = timezone(parseDate(content('span#pubtime_baidu').text()), +8); + item.author = content('span#source_baidu').text(); + item.description = content('div.articleText').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `${nodes[category].title} 参考消息`, + link: currentUrl, + description: '参考消息', + language: 'zh-cn', + item: items, + }; +}; diff --git a/lib/v2/cankaoxiaoxi/maintainer.js b/lib/v2/cankaoxiaoxi/maintainer.js new file mode 100644 index 000000000..3dcd5ba71 --- /dev/null +++ b/lib/v2/cankaoxiaoxi/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/:category': ['yuxinliu-alex'], +}; diff --git a/lib/v2/cankaoxiaoxi/radar.js b/lib/v2/cankaoxiaoxi/radar.js new file mode 100644 index 000000000..96912d3e0 --- /dev/null +++ b/lib/v2/cankaoxiaoxi/radar.js @@ -0,0 +1,61 @@ +module.exports = { + 'cankaoxiaoxi.com': { + _name: '参考消息', + china: [ + { + title: '中国新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/china_news', + }, + ], + culture: [ + { + title: '文化新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/culture_news', + }, + ], + finance: [ + { + title: '财经新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/finance_news', + }, + ], + mil: [ + { + title: '军事新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/military_news', + }, + ], + science: [ + { + title: '科技新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/technology_news', + }, + ], + tw: [ + { + title: '台海新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/taiwan_news', + }, + ], + world: [ + { + title: '国际新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi', + source: ['/'], + target: '/cankaoxiaoxi/news/world_news', + }, + ], + }, +}; diff --git a/lib/v2/cankaoxiaoxi/router.js b/lib/v2/cankaoxiaoxi/router.js new file mode 100644 index 000000000..a9517720c --- /dev/null +++ b/lib/v2/cankaoxiaoxi/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news/:category', require('./index')); +};