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
This commit is contained in:
Enoch Ma 2022-11-23 16:21:39 +01:00 committed by GitHub
parent 9a5f9da660
commit aaf3d95a5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 86 additions and 49 deletions

View File

@ -514,9 +514,9 @@ Edition
### Topics
<RouteEn author="emdoe" example="/nautilus/topic/Art" path="/nautilus/topic/:tid" :paramsDesc="['topic']">
<RouteEn author="emdoe" example="/nautil/topic/arts" path="/nautil/topic/:tid" :paramsDesc="['topic']">
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.
</RouteEn>

View File

@ -910,7 +910,7 @@ IPFS 网关有可能失效,那时候换成其他网关。
### 话题
<Route author="emdoe" example="/nautilus/topic/Art" path="/nautilus/topic/:tid" :paramsDesc="['话题 id, 可在页面上方 TOPICS 栏目处找到']"/>
<Route author="emdoe" example="/nautil/topic/arts" path="/nautil/topic/:tid" :paramsDesc="['话题 id, 可在页面上方 TOPICS 栏目处找到']"/>
## Netflix

View File

@ -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'));

View File

@ -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,
};
};

View File

@ -0,0 +1,3 @@
module.exports = {
'/topic/:tid': ['emdoe'],
};

13
lib/v2/nautil/radar.js Normal file
View File

@ -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',
},
],
},
};

3
lib/v2/nautil/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/topic/:tid', require('./topics'));
};

View File

@ -0,0 +1,6 @@
{{ if head.og_image }}
{{ each head.og_image img }}
<img src="{{ img.url.split('?')[0] }}">
{{ /each }}
{{ /if }}
{{@ rendered }}

57
lib/v2/nautil/topics.js Normal file
View File

@ -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,
};
};