Files
RSSHub/lib/v2/aeon/utils.js
T
2024-02-28 19:40:21 +08:00

52 lines
2.1 KiB
JavaScript

import cache from '@/utils/cache';
import { load } from 'cheerio';
import got from '@/utils/got';
import { art } from '@/utils/render';
import * as path from 'node:path';
const getData = async (ctx, list) => {
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = 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 = load(article.body);
const banner = article.thumbnail?.urls?.header;
capture('p.pullquote').remove();
const authorsBio = article.authors.map((author) => '<p>' + author.displayName + author.authorBio.replaceAll(/^<p>/g, ' ')).join('');
item.description = art(path.join(__dirname, 'templates/essay.art'), { banner, authorsBio, content: capture.html() });
}
return item;
})
)
);
return items;
};
module.exports = {
getData,
};