fix(route): Readhub (#8029)

* fix(route): Readhub

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
Ethan Shen
2022-02-23 23:02:17 +08:00
committed by GitHub
co-authored by TonyRL
parent 2389eb212d
commit 8aa3b1ca15
7 changed files with 123 additions and 108 deletions
+4 -4
View File
@@ -1012,11 +1012,11 @@ IPFS 网关有可能失效,那时候换成其他网关。
### 分类
<Route author="WhiteWorld" example="/readhub/category/topic" path="/readhub/category/:category" :paramsDesc="['分类名']">
<Route author="WhiteWorld nczitzk" example="/readhub" path="/readhub/:category?" :paramsDesc="['分类,见下表,默认为热门话题']">
| 热门话题 | 科技动态 | 开发者资讯 | 区块链快讯 | 每日早报 |
| ----- | ---- | -------- | ---------- | ----- |
| topic | news | technews | blockchain | daily |
| 热门话题 | 科技动态 | 技术资讯 | 区块链快讯 | 每日早报 |
| ----- | ---- | ---- | ---------- | ----- |
| topic | news | tech | blockchain | daily |
</Route>
+3 -2
View File
@@ -242,8 +242,9 @@ router.get('/v2ex/topics/:type', lazyloadRouteHandler('./routes/v2ex/topics'));
router.get('/v2ex/post/:postid', lazyloadRouteHandler('./routes/v2ex/post'));
router.get('/v2ex/tab/:tabid', lazyloadRouteHandler('./routes/v2ex/tab'));
// readhub
router.get('/readhub/category/:category', lazyloadRouteHandler('./routes/readhub/category'));
// Readhub migrated to v2
// router.get('/readhub/category/:category?', lazyloadRouteHandler('./routes/readhub/index'));
// router.get('/readhub/:category?', lazyloadRouteHandler('./routes/readhub/index'));
// f-droid
router.get('/fdroid/apprelease/:app', lazyloadRouteHandler('./routes/fdroid/apprelease'));
-102
View File
@@ -1,102 +0,0 @@
const got = require('@/utils/got');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
const category = ctx.params.category;
let title;
let link;
let path = category;
switch (category) {
case 'topic':
title = '热门话题';
link = 'https://readhub.cn/topics';
break;
case 'news':
title = '科技动态';
link = 'https://readhub.cn/news';
break;
case 'technews':
title = '开发者资讯';
link = 'https://readhub.cn/tech';
break;
case 'blockchain':
title = '区块链快讯';
link = 'https://readhub.cn/blockchain';
break;
case 'daily':
title = '每日早报';
link = 'https://readhub.cn/daily';
break;
default:
break;
}
if (path === 'daily') {
path = 'topic/daily';
}
const {
data: { data },
} = await got({
method: 'get',
url: `https://api.readhub.cn/${path}`,
});
const out = await Promise.all(
data.map(async (news) => {
const id = news.id;
let item;
if (category === 'topic' || category === 'daily') {
const link = `https://readhub.cn/topic/${id}`;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const { data } = await got.get(`https://api.readhub.cn/topic/${id}`);
let description = data.summary;
if (data.newsArray) {
description += '<br/><br/>媒体报道:';
for (const one of data.newsArray) {
description += `<br/>${dayjs(new Date(one.publishDate)).format('YY-MM-DD')} ${one.siteName}: <a href='${one.mobileUrl || one.url}'>${one.title}</a>`;
}
}
if (data.timeline && data.timeline.topics) {
let type = '相关事件';
if (data.timeline.commonEntities && data.timeline.commonEntities.length > 0) {
type = '事件追踪';
}
description += `<br/><br/>${type}:`;
for (const one of data.timeline.topics) {
description += `<br/>${dayjs(new Date(one.createdAt)).format('YY-MM-DD')} <a href='https://readhub.cn/topic/${one.id}'>${one.title.trim()}</a>`;
}
}
item = {
title: data.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: data.publishDate,
guid: id,
link,
};
ctx.cache.set(link, JSON.stringify(item));
} else {
const description = news.summaryAuto || news.summary || news.title;
item = {
title: news.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: news.publishDate,
guid: id,
link: news.url || news.mobileUrl,
};
}
return item;
})
);
ctx.state.data = {
title: `Readhub - ${title}`,
link,
item: out,
};
};
+94
View File
@@ -0,0 +1,94 @@
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,
};
};
+3
View File
@@ -0,0 +1,3 @@
module.export = {
'/:category?': ['WhiteWorld', 'nczitzk'],
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
'readhub.cn': {
_name: 'Readhub',
'.': [
{
title: '分类',
docs: 'https://docs.rsshub.app/new-media.html#readhub',
source: '/',
target: '/readhub',
},
],
},
};
+6
View File
@@ -0,0 +1,6 @@
module.exports = (router) => {
// deprecated
router.get('/category/:category?', require('./index'));
router.get('/:category?', require('./index'));
};