fix(route): aeon - support for other categories (#12614)

* aeon: hotfix & migrate to v2

* rewrite & focus on essays

* fix banner

* better support for aeon

* update the doc

* credits before content

* using templates & fix doc
This commit is contained in:
Enoch Ma
2023-06-05 23:50:00 +08:00
committed by GitHub
parent b53d343bc1
commit 051a3efcbb
11 changed files with 156 additions and 57 deletions
+27
View File
@@ -0,0 +1,27 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { getData } = require('./utils');
module.exports = async (ctx) => {
const url = `https://aeon.co/${ctx.params.category}`;
const { data: response } = await got(url);
const $ = cheerio.load(response);
const data = JSON.parse($('script#__NEXT_DATA__').text());
const list = data.props.pageProps.section.articles.edges.map((item) => ({
title: item.node.title,
author: item.node.authors.map((author) => author.displayName).join(', '),
link: `https://aeon.co/${item.node.type.toLowerCase()}s/${item.node.slug}`,
pubDate: item.node.createdAt,
}));
const items = await getData(ctx, list);
ctx.state.data = {
title: `AEON | ${data.props.pageProps.section.title}`,
link: url,
description: data.props.pageProps.section.metaDescription,
item: items,
};
};
-46
View File
@@ -1,46 +0,0 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const url = `https://aeon.co/essays`;
const { data: response } = await got(url);
const $ = cheerio.load(response);
const data = JSON.parse($('script#__NEXT_DATA__').text());
const list = data.props.pageProps.articles.map((item) => ({
title: item.title,
author: item.authors.map((author) => author.displayName).join(', '),
link: `https://aeon.co/essays/${item.slug}`,
pubDate: item.createdAt,
}));
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
// It seems that the method based on __NEXT_DATA__
// does not include the information of the two-column
// images in the article body,
// e.g. https://aeon.co/essays/how-to-mourn-a-forest-a-lesson-from-west-papua .
// But that's very rare.
const data = JSON.parse($('script#__NEXT_DATA__').text());
const banner = `<img src="${data.props.pageProps.article.thumbnail.urls.header}">`;
const authorsBio = data.props.pageProps.article.authors.map((author) => '<p>' + author.displayName + author.authorBio.replace(/^<p>/g, ' ')).join('');
const capture = cheerio.load(data.props.pageProps.article.body);
capture('p.pullquote').remove();
item.description = banner + authorsBio + capture.html();
return item;
})
)
);
ctx.state.data = {
title: `AEON | Essays`,
link: url,
item: items,
};
};
+2 -1
View File
@@ -1,3 +1,4 @@
module.exports = {
'/essays': ['emdoe'],
'/category/:category': ['emdoe'],
'/:type': ['emdoe'],
};
+9 -3
View File
@@ -3,10 +3,16 @@ module.exports = {
_name: 'AEON',
aeon: [
{
title: 'Essays',
title: 'Types (Essays, Videos, or Audio)',
docs: 'https://docs.rsshub.app/new-media.html##aeon',
source: ['/'],
target: '/aeon/essays',
source: ['/:type'],
target: '/aeon/:type',
},
{
title: 'Category',
docs: 'https://docs.rsshub.app/new-media.html##aeon',
source: ['/:category'],
target: '/aeon/category/:category',
},
],
},
+2 -1
View File
@@ -1,3 +1,4 @@
module.exports = function (router) {
router.get('/essays', require('./essays'));
router.get('/category/:category', require('./category'));
router.get('/:type', require('./type'));
};
+3
View File
@@ -0,0 +1,3 @@
<img src="{{ banner }}" alt="">
{{@ authorsBio }}
{{@ content}}
+10
View File
@@ -0,0 +1,10 @@
{{ set video = article.hosterId }}
{{ if article.hoster === 'vimeo' }}
{{ set video = "https://player.vimeo.com/video/" + video + "?dnt=1"}}
{{ else if article.hoster == 'youtube' }}
{{ set video = "https://www.youtube-nocookie.com/embed/" + video }}
{{ /if }}
<iframe width="672" height="377" src="{{ video }}" frameborder="0" allowfullscreen></iframe>
{{@ article.credits}}
{{@ article.description}}
+29
View File
@@ -0,0 +1,29 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { getData } = require('./utils');
module.exports = async (ctx) => {
const type = ctx.params.type;
const binaryType = type === 'videos' ? 'videos' : 'essays';
const capitalizedType = type.charAt(0).toUpperCase() + type.slice(1);
const url = `https://aeon.co/${type}`;
const { data: response } = await got(url);
const $ = cheerio.load(response);
const data = JSON.parse($('script#__NEXT_DATA__').text());
const list = data.props.pageProps.articles.map((item) => ({
title: item.title,
link: `https://aeon.co/${binaryType}/${item.slug}`,
pubDate: item.createdAt,
}));
const items = await getData(ctx, list);
ctx.state.data = {
title: `AEON | ${capitalizedType}`,
link: url,
item: items,
};
};
+50
View File
@@ -0,0 +1,50 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { art } = require('@/utils/render');
const path = require('path');
const getData = async (ctx, list) => {
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
const data = JSON.parse($('script#__NEXT_DATA__').text());
const type = data.props.pageProps.article.type.toLowerCase();
if (type === 'video') {
item.description = art(path.join(__dirname, 'templates/video.art'), { article: data.props.pageProps.article });
} else {
// Essay or Audio
// But unfortunately, the method based on __NEXT_DATA__
// does not include the information of the audio link.
// Besides, it seems that the method based on __NEXT_DATA__
// does not include the information of the two-column
// images in the article body,
// e.g. https://aeon.co/essays/how-to-mourn-a-forest-a-lesson-from-west-papua .
// But that's very rare.
item.author = data.props.pageProps.article.authors.map((author) => author.displayName).join(', ');
const article = data.props.pageProps.article;
const capture = cheerio.load(article.body);
const banner = article.thumbnail.urls.header;
capture('p.pullquote').remove();
const authorsBio = article.authors.map((author) => '<p>' + author.displayName + author.authorBio.replace(/^<p>/g, ' ')).join('');
item.description = art(path.join(__dirname, 'templates/essay.art'), { banner, authorsBio, content: capture.html() });
}
return item;
})
)
);
return items;
};
module.exports = {
getData,
};