fix(route): 参考消息栏目 (#12168)

* fix(route): 参考消息栏目

* apply suggestions from code review
---------
This commit is contained in:
Ethan Shen 2023-03-26 00:45:19 +08:00 committed by GitHub
parent ea2c526679
commit f014aa755b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 116 deletions

View File

@ -632,13 +632,31 @@ Category 列表:
## 参考消息
### 新闻分类
### 栏目
<Route author="yuxinliu-alex" example="/cankaoxiaoxi/news/military_news" path="/cankaoxiaoxi/news/:category">
<Route author="yuxinliu-alex nczitzk" example="/cankaoxiaoxi/column/diyi" path="/cankaoxiaoxi/column/:id?" :paramsDesc="['栏目 id默认为 diyi即第一关注']">
| 中国 | 国际 | 军事 | 台海 | 财经 | 科技 | 文化 |
| ---------- | ---------- | ------------- | ----------- | ------------ | --------------- | ------------ |
| china_news | world_news | military_news | taiwan_news | finance_news | technology_news | culture_news |
| 栏目 | id |
| -------------- | -------- |
| 第一关注 | diyi |
| 中国 | zhongguo |
| 国际 | gj |
| 观点 | guandian |
| 锐参考 | ruick |
| 体育健康 | tiyujk |
| 科技应用 | kejiyy |
| 文化旅游 | wenhualy |
| 参考漫谈 | cankaomt |
| 研究动态 | yjdt |
| 海外智库 | hwzk |
| 业界信息・观点 | yjxx |
| 海外看中国城市 | hwkzgcs |
| 译名趣谈 | ymymqt |
| 译名发布 | ymymfb |
| 双语汇 | ymsyh |
| 参考视频 | video |
| 军事 | junshi |
| 参考人物 | cankaorw |
</Route>

View File

@ -1,78 +1,65 @@
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/',
},
};
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const category = ctx.params.category;
const id = ctx.params.id ?? 'diyi';
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50;
const currentUrl = nodes[category].url;
const response = await got({
const rootUrl = 'https://china.cankaoxiaoxi.com';
const listApiUrl = `${rootUrl}/json/channel/${id}/list.json`;
const channelApiUrl = `${rootUrl}/json/channel/${id}.channeljson`;
const currentUrl = `${rootUrl}/#/generalColumns/${id}`;
const listResponse = await got({
method: 'get',
url: currentUrl,
url: listApiUrl,
});
const $ = cheerio.load(response.data);
const channelResponse = await got({
method: 'get',
url: channelApiUrl,
});
const list = $('a', '.toutiao')
.filter(function () {
return $(this).text() !== '';
})
.map((_, item) => ({
link: $(item).attr('href'),
}))
.get();
let items = listResponse.data.list.slice(0, limit).map((item) => ({
title: item.data.title,
author: item.data.userName,
category: item.data.channelName,
pubDate: timezone(parseDate(item.data.publishTime), +8),
link: item.data.moVideoPath ? item.data.sourceUrl : `${rootUrl}/json/content/${item.data.url.match(/\/pages\/(.*?)\.html/)[1]}.detailjson`,
video: item.data.moVideoPath,
cover: item.data.mCoverImg,
}));
const items = await Promise.all(
list.map((item) =>
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.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();
if (item.video) {
item.description = art(path.join(__dirname, 'templates/description.art'), {
video: item.video,
cover: item.cover,
});
} else {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const data = detailResponse.data;
item.link = `${rootUrl}/#/detailsPage/${id}/${data.id}/1/${data.publishTime.split(' ')[0]}`;
item.description = data.txt;
}
return item;
})
)
);
ctx.state.data = {
title: `${nodes[category].title} 参考消息`,
title: `参考消息 - ${channelResponse.data.name}`,
link: currentUrl,
description: '参考消息',
language: 'zh-cn',

View File

@ -1,3 +1,4 @@
module.exports = {
'/news/:category': ['yuxinliu-alex'],
'/column/:id?': ['yuxinliu-alex', 'nczitzk'],
'/:id?': ['yuxinliu-alex', 'nczitzk'],
};

View File

@ -1,60 +1,15 @@
module.exports = {
'cankaoxiaoxi.com': {
_name: '参考消息',
china: [
'.': [
{
title: '中国新闻',
docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi',
title: '栏目',
docs: 'https://docs.rsshub.app/traditional-media.html#can-kao-xiao-xi-lan-mu',
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',
target: (params, url) => {
const urlStr = new URL(url).toString();
return `/cankaoxiaoxi/column${/\/#\//.test(urlStr) ? `/${urlStr.split('/').pop()}` : ''}`;
},
},
],
},

View File

@ -1,3 +1,4 @@
module.exports = function (router) {
router.get('/news/:category', require('./index'));
router.get('/column/:id?', require('./'));
router.get('/:id?', require('./'));
};

View File

@ -0,0 +1,5 @@
{{ if video }}
<video controls poster="{{ cover }}">
<source src="{{ video }}" type="video/mp4">
</video>
{{ /if }}