From aaf3d95a5d6602f3cd2cd34b552464a65be0330a Mon Sep 17 00:00:00 2001 From: Enoch Ma Date: Wed, 23 Nov 2022 16:21:39 +0100 Subject: [PATCH] fix(route): nautilus (#11242) * Update topics.js * update doc(cn) for nautilus * update doc(en) for nautilus * refactor: use cache.tryGet * refactor: migrate to v2 * refactor: use wordpress api --- docs/en/new-media.md | 4 +- docs/new-media.md | 2 +- lib/router.js | 2 +- lib/routes/nautilus/topics.js | 45 ------------------- lib/v2/nautil/maintainer.js | 3 ++ lib/v2/nautil/radar.js | 13 ++++++ lib/v2/nautil/router.js | 3 ++ lib/v2/nautil/templates/description.art | 6 +++ lib/v2/nautil/topics.js | 57 +++++++++++++++++++++++++ 9 files changed, 86 insertions(+), 49 deletions(-) delete mode 100644 lib/routes/nautilus/topics.js create mode 100644 lib/v2/nautil/maintainer.js create mode 100644 lib/v2/nautil/radar.js create mode 100644 lib/v2/nautil/router.js create mode 100644 lib/v2/nautil/templates/description.art create mode 100644 lib/v2/nautil/topics.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 88c0747f2..73dcb4548 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -514,9 +514,9 @@ Edition ### Topics - + -This route provides a flexible plan with full text content to subscribe specific topic(s) on the Nautilus. Please visit [nautil.us](http://nautil.us) and click `Topics` to acquire whole topic list. +This route provides a flexible plan with full text content to subscribe specific topic(s) on the Nautilus. Please visit [nautil.us](https://nautil.us) and click `Topics` to acquire whole topic list. diff --git a/docs/new-media.md b/docs/new-media.md index b5963728f..a5140ffbe 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -910,7 +910,7 @@ IPFS 网关有可能失效,那时候换成其他网关。 ### 话题 - + ## Netflix diff --git a/lib/router.js b/lib/router.js index 175ff55c9..7c3594fb2 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1065,7 +1065,7 @@ router.get('/miniflux/:feeds/:parameters?', lazyloadRouteHandler('./routes/minif // router.get('/nga/post/:tid', lazyloadRouteHandler('./routes/nga/post')); // Nautilus -router.get('/nautilus/topic/:tid', lazyloadRouteHandler('./routes/nautilus/topics')); +// router.get('/nautilus/topic/:tid', lazyloadRouteHandler('./routes/nautilus/topics')); // JavBus migrated to v2 // router.get('/javbus/home', lazyloadRouteHandler('./routes/javbus/home')); diff --git a/lib/routes/nautilus/topics.js b/lib/routes/nautilus/topics.js deleted file mode 100644 index f641773a5..000000000 --- a/lib/routes/nautilus/topics.js +++ /dev/null @@ -1,45 +0,0 @@ -const cheerio = require('cheerio'); -const got = require('@/utils/got'); - -module.exports = async (ctx) => { - const url = `http://nautil.us/term/f/${ctx.params.tid}`; - - const res = await got.get(url); - const $ = cheerio.load(res.data); - const list = $('article.search-result').get(); - - const out = await Promise.all( - list.map(async (item) => { - const $ = cheerio.load(item); - const title = $('h3 > a').text(); - const partial = $('h3 > a').attr('href'); - const address = `http://nautil.us${partial}`; - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - const res = await got.get(address); - const capture = cheerio.load(res.data); - capture('div.reco').remove(); - const banner = capture('div.banner').html(); - const textWithTime = capture('div.byline').text(); - const author = capture('div.byline > span:nth-child(2)').text(); - const contents = banner + capture('div.page-content').html(); - const single = { - title, - author, - description: contents, - link: address, - guid: address, - pubDate: new Date(textWithTime).toUTCString(), - }; - ctx.cache.set(address, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - ctx.state.data = { - title: 'Nautilus | ' + $('title').text(), - link: url, - item: out, - }; -}; diff --git a/lib/v2/nautil/maintainer.js b/lib/v2/nautil/maintainer.js new file mode 100644 index 000000000..86a64ffe1 --- /dev/null +++ b/lib/v2/nautil/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/topic/:tid': ['emdoe'], +}; diff --git a/lib/v2/nautil/radar.js b/lib/v2/nautil/radar.js new file mode 100644 index 000000000..ccca2bafb --- /dev/null +++ b/lib/v2/nautil/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'nautil.us': { + _name: 'Nautilus', + '.': [ + { + title: 'Topics', + docs: 'https://docs.rsshub.app/en/new-media.html#nautilus', + source: ['/topics/:tid'], + target: '/nautil/topic/:tid', + }, + ], + }, +}; diff --git a/lib/v2/nautil/router.js b/lib/v2/nautil/router.js new file mode 100644 index 000000000..d9fec6531 --- /dev/null +++ b/lib/v2/nautil/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/topic/:tid', require('./topics')); +}; diff --git a/lib/v2/nautil/templates/description.art b/lib/v2/nautil/templates/description.art new file mode 100644 index 000000000..8f54b3c7a --- /dev/null +++ b/lib/v2/nautil/templates/description.art @@ -0,0 +1,6 @@ +{{ if head.og_image }} +{{ each head.og_image img }} + +{{ /each }} +{{ /if }} +{{@ rendered }} diff --git a/lib/v2/nautil/topics.js b/lib/v2/nautil/topics.js new file mode 100644 index 000000000..764db0917 --- /dev/null +++ b/lib/v2/nautil/topics.js @@ -0,0 +1,57 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); +const baseUrl = 'https://nautil.us'; + +module.exports = async (ctx) => { + const categoryIdMap = await ctx.cache.tryGet('nautil:categories', async () => { + const { data } = await got(`${baseUrl}/wp-json/wp/v2/categories`, { + searchParams: { + per_page: 100, + }, + }); + return data.map((item) => ({ + id: item.id, + name: item.name, + slug: item.slug, + })); + }); + + const { data: list } = await got(`${baseUrl}/wp-json/wp/v2/posts`, { + searchParams: { + categories: categoryIdMap.find((item) => item.slug === ctx.params.tid.toLowerCase()).id, + per_page: ctx.query.limit ? parseInt(ctx.query.limit) : 20, + }, + }); + + const out = list.map((item) => { + const head = item.yoast_head_json; + const $ = cheerio.load(item.content.rendered, null, false); + // lazyload images + $('img').each((_, e) => { + e = $(e); + e.attr('src', e.attr('data-src') ?? e.attr('srcset')); + e.attr('src', e.attr('src').split('?')[0]); + e.removeAttr('data-src'); + e.removeAttr('srcset'); + }); + return { + title: item.title.rendered, + author: item.yoast_head_json.author, + description: art(path.join(__dirname, 'templates/description.art'), { + head, + rendered: $.html(), + }), + link: item.link, + pubDate: parseDate(item.date_gmt), + }; + }); + + ctx.state.data = { + title: 'Nautilus | ' + categoryIdMap.find((item) => item.slug === ctx.params.tid.toLowerCase()).name, + link: `${baseUrl}/topics/${ctx.params.tid}/`, + item: out, + }; +};