Files
RSSHub/lib/v2/readhub/index.js
T
Ethan ShenandTonyRL 8aa3b1ca15 fix(route): Readhub (#8029)
* fix(route): Readhub

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2022-02-23 23:02:17 +08:00

95 lines
3.5 KiB
JavaScript

const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const titles = {
topic: '热门话题',
news: '科技动态',
tech: '技术资讯',
blockchain: '区块链快讯',
daily: '每日早报',
};
module.exports = async (ctx) => {
let category = ctx.params.category ?? 'topic';
category = category === 'technews' ? 'tech' : category;
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 response = await got({
method: 'get',
url: apiUrl,
});
const list = response.data.data.map((item) => ({
id: item.id,
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,
}));
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.id, async () => {
if (item.hasInstantView || category === 'daily') {
const { data } = await got({
method: 'get',
url: `${apiRootUrl}/topic/instantview?topicId=${item.id}`,
});
item.description = `<a href="${data.url}">访问原网址</a><br>${data.content}`;
}
if (isNaN(item.id)) {
const { data } = await got({
method: 'get',
url: `${apiRootUrl}/topic/${item.id}`,
});
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;
})
)
);
ctx.state.data = {
title: `Readhub - ${titles[category]}`,
link: currentUrl,
item: items,
};
};