From 7e9af672745a1793ffd21e0c2c71b4fb4796fcc0 Mon Sep 17 00:00:00 2001 From: yuxinliu-alex Date: Wed, 20 Apr 2022 22:30:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E7=8E=AF=E7=90=83=E7=BD=91=20(#?= =?UTF-8?q?9580)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 环球网 * feat(route): 环球网 加入通用参数limit限制条数及格式调整 Co-authored-by: alex --- docs/traditional-media.md | 13 ++++++++ lib/v2/huanqiu/index.js | 65 ++++++++++++++++++++++++++++++++++++ lib/v2/huanqiu/maintainer.js | 3 ++ lib/v2/huanqiu/radar.js | 13 ++++++++ lib/v2/huanqiu/router.js | 3 ++ 5 files changed, 97 insertions(+) create mode 100644 lib/v2/huanqiu/index.js create mode 100644 lib/v2/huanqiu/maintainer.js create mode 100644 lib/v2/huanqiu/radar.js create mode 100644 lib/v2/huanqiu/router.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 6171b4a189..f723a083f0 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -942,6 +942,19 @@ Type 栏目: +## 环球网 + +### 分类 + + + + +| 国内新闻 | 国际新闻 | 军事 | 台海 | 评论 | +|-------| ----------- | -------- |---------------|---------| +| china | world | mil | taiwai| opinion | + + + ## 极客公园 ### 全球快讯 diff --git a/lib/v2/huanqiu/index.js b/lib/v2/huanqiu/index.js new file mode 100644 index 0000000000..9f8fa2eb8a --- /dev/null +++ b/lib/v2/huanqiu/index.js @@ -0,0 +1,65 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +function getKeysRecursive(dic, key, attr, array) { + Object.values(dic).forEach((v) => { + if (v[key] !== undefined) { + getKeysRecursive(v[key], key, attr, array); + } else { + array.push(v[attr]); + } + }); + return array; +} + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'china'; + const host = 'https://' + category + '.huanqiu.com'; + + const resp = await got({ + method: 'get', + url: host.concat('/api/channel_pc'), + }).json(); + const name = getKeysRecursive(resp.children, 'children', 'domain_name', [])[0]; + + const nodes = getKeysRecursive(resp.children, 'children', 'node', []) + .map((x) => '"' + x + '"') + .join(); + const req = await got({ + method: 'get', + url: host.concat('/api/list?node=') + nodes.concat(`&offset=0&limit=${ctx.query.limit ?? 20}`), + }).json(); + + let items = req.list + .filter((item) => item.aid) + .map((item) => ({ + link: host.concat('/article/' + item.aid), + title: item.title, + })); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + item.description = content('article').html(); + item.author = content('span', '.source').text(); + item.pubDate = timezone(parseDate(content('p.time').text()), +8); + return item; + }) + ) + ); + + ctx.state.data = { + title: `${name} - 环球网`, + link: host, + description: '环球网', + language: 'zh-cn', + item: items, + }; +}; diff --git a/lib/v2/huanqiu/maintainer.js b/lib/v2/huanqiu/maintainer.js new file mode 100644 index 0000000000..3b0309f3b8 --- /dev/null +++ b/lib/v2/huanqiu/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/:category?': ['yuxinliu-alex'], +}; diff --git a/lib/v2/huanqiu/radar.js b/lib/v2/huanqiu/radar.js new file mode 100644 index 0000000000..c2ea99ccd5 --- /dev/null +++ b/lib/v2/huanqiu/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'huanqiu.com': { + _name: '环球网', + '.': [ + { + title: '分类', + docs: 'https://docs.rsshub.app/traditional-media.html#huan-qiu', + source: '/', + target: '/news/huanqiu/:category?', + }, + ], + }, +}; diff --git a/lib/v2/huanqiu/router.js b/lib/v2/huanqiu/router.js new file mode 100644 index 0000000000..dc3bb1cd8e --- /dev/null +++ b/lib/v2/huanqiu/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news/:category?', require('./index')); +};