feat(route): 环球网 (#9580)

* feat(route): 环球网

* feat(route): 环球网 加入通用参数limit限制条数及格式调整

Co-authored-by: alex <yuxin2@mgtv.com>
This commit is contained in:
yuxinliu-alex
2022-04-20 22:30:11 +08:00
committed by GitHub
parent cc48b48bc8
commit 7e9af67274
5 changed files with 97 additions and 0 deletions
+13
View File
@@ -942,6 +942,19 @@ Type 栏目:
</Route>
## 环球网
### 分类
<Route author="yuxinliu-alex" example="/huanqiu/news/china" path="/huanqiu/news/:category?" :paramsDesc="['类别,可以使用二级域名作为参数,默认为:china']">
| 国内新闻 | 国际新闻 | 军事 | 台海 | 评论 |
|-------| ----------- | -------- |---------------|---------|
| china | world | mil | taiwai| opinion |
</Route>
## 极客公园
### 全球快讯
+65
View File
@@ -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,
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/news/:category?': ['yuxinliu-alex'],
};
+13
View File
@@ -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?',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/:category?', require('./index'));
};