diff --git a/docs/new-media.md b/docs/new-media.md index a863739de..229262b93 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1012,11 +1012,11 @@ IPFS 网关有可能失效,那时候换成其他网关。 ### 分类 - + -| 热门话题 | 科技动态 | 开发者资讯 | 区块链快讯 | 每日早报 | -| ----- | ---- | -------- | ---------- | ----- | -| topic | news | technews | blockchain | daily | +| 热门话题 | 科技动态 | 技术资讯 | 区块链快讯 | 每日早报 | +| ----- | ---- | ---- | ---------- | ----- | +| topic | news | tech | blockchain | daily | diff --git a/lib/router.js b/lib/router.js index efb34e347..c1ed56056 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/readhub/category.js b/lib/routes/readhub/category.js deleted file mode 100644 index 87de00158..000000000 --- a/lib/routes/readhub/category.js +++ /dev/null @@ -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 += '

媒体报道:'; - for (const one of data.newsArray) { - description += `
${dayjs(new Date(one.publishDate)).format('YY-MM-DD')} ${one.siteName}: ${one.title}`; - } - } - if (data.timeline && data.timeline.topics) { - let type = '相关事件'; - if (data.timeline.commonEntities && data.timeline.commonEntities.length > 0) { - type = '事件追踪'; - } - description += `

${type}:`; - for (const one of data.timeline.topics) { - description += `
${dayjs(new Date(one.createdAt)).format('YY-MM-DD')} ${one.title.trim()}`; - } - } - item = { - title: data.title, - description: description.replace(new RegExp('\n', 'g'), '
'), - 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'), '
'), - pubDate: news.publishDate, - guid: id, - link: news.url || news.mobileUrl, - }; - } - return item; - }) - ); - - ctx.state.data = { - title: `Readhub - ${title}`, - link, - item: out, - }; -}; diff --git a/lib/v2/readhub/index.js b/lib/v2/readhub/index.js new file mode 100644 index 000000000..652e503a7 --- /dev/null +++ b/lib/v2/readhub/index.js @@ -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 = `访问原网址
${data.content}`; + } + + if (isNaN(item.id)) { + const { data } = await got({ + method: 'get', + url: `${apiRootUrl}/topic/${item.id}`, + }); + + if (data.newsArray) { + item.description += '
媒体报道'; + } + + if (data.timeline && data.timeline.topics) { + const hasTimeline = data.timeline.commonEntities && data.timeline.commonEntities.length > 0; + + item.description += `
${hasTimeline ? '事件追踪' : '相关事件'}'; + } + } + delete item.id; + delete item.hasInstantView; + + return item; + }) + ) + ); + + ctx.state.data = { + title: `Readhub - ${titles[category]}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/readhub/maintainer.js b/lib/v2/readhub/maintainer.js new file mode 100644 index 000000000..56e8afdd9 --- /dev/null +++ b/lib/v2/readhub/maintainer.js @@ -0,0 +1,3 @@ +module.export = { + '/:category?': ['WhiteWorld', 'nczitzk'], +}; diff --git a/lib/v2/readhub/radar.js b/lib/v2/readhub/radar.js new file mode 100644 index 000000000..952821594 --- /dev/null +++ b/lib/v2/readhub/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'readhub.cn': { + _name: 'Readhub', + '.': [ + { + title: '分类', + docs: 'https://docs.rsshub.app/new-media.html#readhub', + source: '/', + target: '/readhub', + }, + ], + }, +}; diff --git a/lib/v2/readhub/router.js b/lib/v2/readhub/router.js new file mode 100644 index 000000000..aee28921f --- /dev/null +++ b/lib/v2/readhub/router.js @@ -0,0 +1,6 @@ +module.exports = (router) => { + // deprecated + router.get('/category/:category?', require('./index')); + + router.get('/:category?', require('./index')); +};