fix(route): Readhub (#13567)

* fix(route): Readhub

* fix docs
This commit is contained in:
Ethan Shen 2023-10-20 04:51:34 +08:00 committed by GitHub
parent 9ef53d597d
commit b1f206fcdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 147 additions and 97 deletions

View File

@ -1,106 +1,115 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const titles = {
topic: '热门话题',
news: '科技动态',
tech: '技术资讯',
technews: '技术资讯',
blockchain: '区块链快讯',
daily: '每日早报',
};
const { art } = require('@/utils/render');
const path = require('path');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
let category = 'topic';
let overview = false;
if (titles[ctx.params.category]) {
category = ctx.params.category;
overview = !!ctx.params.overview;
category = category === 'tech' ? 'technews' : category;
} else if (ctx.params.category) {
overview = true;
}
const { category } = ctx.params;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 20;
const rootUrl = 'https://readhub.cn';
const apiRootUrl = 'https://api.readhub.cn';
const currentUrl = `${rootUrl}/topic/${category}`;
const apiUrl = `${apiRootUrl}/${category === 'daily' ? 'topic/daily' : category}`;
const domain = 'readhub.cn';
const rootUrl = `https://${domain}`;
const apiRootUrl = `https://api.${domain}`;
const currentUrl = new URL(category ?? '', rootUrl).href;
const response = await got({
method: 'get',
url: apiUrl,
const isCategory = !category || category === 'daily';
const formatDate = (date, format) => dayjs(date).format(format);
const toTopicUrl = (id) => new URL(`topic/${id}`, rootUrl).href;
art.defaults.imports = {
...art.defaults.imports,
...{
formatDate,
toTopicUrl,
},
};
const { data: currentResponse } = await got(currentUrl);
const buildId = currentResponse.match(/"buildId":"(\w+)"/)[1];
const apiUrl = new URL(category ? (category === 'daily' ? `_next/data/${buildId}/daily.json` : 'news/list') : 'topic/list', category === 'daily' ? rootUrl : apiRootUrl).href;
const { data: response } = await got(apiUrl, {
searchParams: {
...(isCategory
? {}
: {
type: currentResponse.match(/"query":\{"type":"(\d+)"\}/)[1],
}),
...{
page: 1,
size: limit,
},
},
});
const list = response.data.data.map((item) => ({
id: item.id,
let items = (response.data?.items ?? response.pageProps.daily).slice(0, limit).map((item) => ({
title: item.title,
pubDate: parseDate(item.publishDate),
description: item.summaryAuto || item.summary || '',
link: item.url || item.mobileUrl || `${rootUrl}/topic/${item.id}`,
author: item.authorName || item.siteName || '',
hasInstantView: item.hasInstantView || false,
link: item.url ?? new URL(`topic/${item.uid}`, rootUrl).href,
description: art(path.join(__dirname, 'templates/description.art'), {
description: item.summary,
news: item.newsAggList,
timeline: item.timeline,
}),
author: item.siteNameDisplay,
category: item.tagList?.map((c) => c.name) ?? [],
guid: isCategory ? item.uid : `readhub-${item.guid}`,
pubDate: timezone(parseDate(item.publishDate), +8),
}));
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.id + overview, async () => {
if (item.hasInstantView || category === 'daily') {
const { data } = await got({
method: 'get',
url: `${apiRootUrl}/topic/instantview?topicId=${item.id}`,
if (isCategory) {
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(`readhub-${item.guid}`, async () => {
const { data: detailResponse } = await got(new URL(`_next/data/${buildId}/topic/${item.guid}.json`, rootUrl).href);
const data = detailResponse.pageProps.topic;
item.title = data.title;
item.link = data.url ?? new URL(`topic/${data.uid}`, rootUrl).href;
item.description = art(path.join(__dirname, 'templates/description.art'), {
description: data.summary,
news: data.newsAggList,
timeline: data.timeline,
});
item.author = data.entityList.map((a) => a.name).join('/');
item.category = data.tagList.map((c) => c.name);
item.guid = `readhub-${data.uid}`;
item.pubDate = timezone(parseDate(data.publishDate), +8);
item.description = `<a href="${data.url}">访问原网址</a><br>${!overview ? data.content : ''}`;
}
return item;
})
)
);
}
if (isNaN(item.id)) {
const { data } = await got({
method: 'get',
url: `${apiRootUrl}/topic/${item.id}`,
});
const $ = cheerio.load(currentResponse);
if (data.summary && overview) {
item.description += `${data.summary}<hr>`;
}
if (data.newsArray) {
item.description += '<br><b>媒体报道</b><ul>';
for (const news of data.newsArray) {
item.description +=
`<li><a href="${news.url || news.mobileUrl}">${news.title}</a>` +
(news.siteName ? `&nbsp;&nbsp;<small><b>${news.siteName}</b></small>` : '') +
`${news.publishDate ? `&nbsp;&nbsp;<small>${news.publishDate.split('T')[0]}</small>` : ''}</li>`;
}
item.description += '</ul>';
}
if (data.timeline && data.timeline.topics) {
const hasTimeline = data.timeline.commonEntities && data.timeline.commonEntities.length > 0;
item.description += `<br><b>${hasTimeline ? '事件追踪' : '相关事件'}</b><ul>`;
for (const news of data.timeline.topics) {
const createdAt = news.createdAt ? `<small>${news.createdAt.split('T')[0]}</small>` : '';
item.description += `<li>${hasTimeline ? `${createdAt}&nbsp;&nbsp;` : ''}<a href="${rootUrl}/topic/${news.id}">${news.title}</a>${hasTimeline ? '' : `&nbsp;&nbsp;${createdAt}`}</li>`;
}
item.description += '</ul>';
}
}
delete item.id;
delete item.hasInstantView;
return item;
})
)
);
const author = $('meta[property="og:site_name"]').prop('content');
const subtitle = $(
$('nav div a')
.toArray()
.filter((a) => /style_active.*/.test($(a).prop('class')))
.pop()
).text();
const icon = new URL($('link[rel="apple-touch-icon"]').prop('href'), rootUrl).href;
ctx.state.data = {
title: `Readhub - ${titles[category]}`,
link: currentUrl,
item: items,
title: `${author} - ${subtitle}`,
link: currentUrl,
description: $('meta[property="og:description"]').prop('content'),
language: $('html').prop('lang'),
image: $('meta[property="og:image"]').prop('content'),
icon,
logo: icon,
subtitle,
author,
allowEmpty: true,
};
};

View File

@ -1,3 +1,3 @@
module.exports = {
'/:category?/:overview?': ['WhiteWorld', 'nczitzk', 'Fatpandac'],
'/:category?': ['WhiteWorld', 'nczitzk', 'Fatpandac'],
};

View File

@ -4,9 +4,13 @@ module.exports = {
'.': [
{
title: '分类',
docs: 'https://docs.rsshub.app/routes/new-media#readhub',
docs: 'https://docs.rsshub.app/routes/new-media#readhub-fen-lei',
source: ['/', '/:category'],
target: (params) => `/readhub/${params.category ? params.category : ''}`,
target: (params) => {
const category = params.category;
return `/readhub${category ? `/${category}` : ''}`;
},
},
],
},

View File

@ -1,6 +1,3 @@
module.exports = (router) => {
// deprecated
router.get('/category/:category?/:overview?', require('./index'));
router.get('/:category?/:overview?', require('./index'));
router.get('/:category?', require('./'));
};

View File

@ -0,0 +1,40 @@
{{ if description }}
<p>{{ description }}</p>
{{ /if }}
{{ if news }}
<h3>媒体报道</h3>
<table cellspacing="8">
<tbody>
{{ each news n }}
<tr>
<td>
<a href="{{ n.url }}">{{ n.title }}</a>
</td>
<th align="left">
<small>{{ n.siteNameDisplay }}</small>
</th>
</tr>
{{ /each }}
</tbody>
</table>
{{ /if }}
{{ if timeline }}
<h3>事件追踪</h3>
<table cellspacing="10">
<tbody>
{{ set topics = timeline.topics }}
{{ each topics t }}
<tr>
<th align="left">
<small>{{ t.publishDate | formatDate 'YYYY-MM-DD HH:mm:ss' }}</small>
</th>
<td>
<a href="{{ t.uid | toTopicUrl }}">{{ t.title }}</a>
</td>
</tr>
{{ /each }}
</tbody>
</table>
{{ /if }}

View File

@ -1708,11 +1708,11 @@ Compared to the official one, this feed:
### 分类 {#readhub-fen-lei}
<Route author="WhiteWorld nczitzk Fatpandac" example="/readhub" path="/readhub/:category?/:overview?" paramsDesc={['分类,见下表,默认为热门话题', '获取概述任意值获取概述默认为不获取']}>
<Route author="WhiteWorld nczitzk Fatpandac" example="/readhub" path="/readhub/:category?" paramsDesc={['分类,见下表,默认为热门话题']} radar="1" rssbud="1">
| 热门话题 | 科技动态 | 技术资讯 | 区块链快讯 | 每日早报 |
| -------- | -------- | -------- | ---------- | -------- |
| topic | news | tech | blockchain | daily |
| 热门话题 | 每日早报 | 科技动态 | 医疗产业 |
| -------- | -------- | -------- | -------- |
| | daily | news | medical |
</Route>