mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-02 01:58:11 +08:00
feat(route): DevolverDigital Blog: Parse per page instead of full page, and add cache support. (#12560)
* get each post instead of the whole page * add cache support * Update blog.js * Update blog.js
This commit is contained in:
@@ -3,39 +3,47 @@ const cheerio = require('cheerio');
|
|||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const baseUrl = 'https://www.devolverdigital.com/blog';
|
const baseUrl = 'https://www.devolverdigital.com/blog';
|
||||||
|
|
||||||
const { data: response } = await got(baseUrl);
|
const { data: response } = await got(baseUrl);
|
||||||
|
|
||||||
const $ = cheerio.load(response);
|
const $ = cheerio.load(response);
|
||||||
$('noscript').remove();
|
|
||||||
|
|
||||||
const nextData = JSON.parse($('#__NEXT_DATA__').text());
|
const nextData = JSON.parse($('#__NEXT_DATA__').text());
|
||||||
const allBlogContents = $('div.flex > div > div > div > div:not([class])');
|
|
||||||
|
|
||||||
const items = nextData.props.pageProps.posts.map((post, postIndex) => {
|
const items = await Promise.all(
|
||||||
// img resource redirection and
|
nextData.props.pageProps.posts.map((postData) => {
|
||||||
// clean up absolute layouts for img and span
|
const postUrl = `${baseUrl}/post/${postData.id}`;
|
||||||
|
return ctx.cache.tryGet(postUrl, async () => {
|
||||||
|
const { data: postPage } = await got(postUrl);
|
||||||
|
|
||||||
const imageUrls = post.body.filter((item) => item.type === 'upload' && item.value.cloudinary.resource_type === 'image').map((item) => item.value.cloudinary.secure_url);
|
const $page = cheerio.load(postPage);
|
||||||
|
$page('noscript').remove();
|
||||||
|
const postContent = $page('div.flex > div > div > div > div:not([class])');
|
||||||
|
|
||||||
const allImageSpans = $(allBlogContents[postIndex]).find('span > img').parent();
|
// img resource redirection and
|
||||||
|
// clean up absolute layouts for img and span
|
||||||
|
const imageUrls = postData.body.filter((item) => item.type === 'upload' && item.value.cloudinary.resource_type === 'image').map((item) => item.value.cloudinary.secure_url);
|
||||||
|
const allImageSpans = postContent.find('span > img').parent();
|
||||||
|
allImageSpans.each((spanIndex, span) => {
|
||||||
|
$(span).attr('style', $(span).attr('style').replace('position:absolute', ''));
|
||||||
|
const img = $(span).find('img');
|
||||||
|
img.attr('src', imageUrls[spanIndex]);
|
||||||
|
img.attr('style', img.attr('style').replace('position:absolute', '').replace('width:0', '').replace('height:0', ''));
|
||||||
|
});
|
||||||
|
|
||||||
allImageSpans.each((spanIndex, span) => {
|
return {
|
||||||
$(span).attr('style', $(span).attr('style').replace('position:absolute', ''));
|
title: postData.title,
|
||||||
const img = $(span).find('img');
|
link: postUrl,
|
||||||
img.attr('src', imageUrls[spanIndex]);
|
author: postData.author,
|
||||||
img.attr('style', img.attr('style').replace('position:absolute', '').replace('width:0', '').replace('height:0', ''));
|
pubDate: Date.parse(postData.createdAt),
|
||||||
});
|
updated: Date.parse(postData.updatedAt),
|
||||||
|
description: postContent.html(),
|
||||||
return {
|
};
|
||||||
title: post.title,
|
});
|
||||||
author: post.author,
|
})
|
||||||
pubDate: Date.parse(post.createdAt),
|
);
|
||||||
description: $(allBlogContents[postIndex]).html(),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.state.data = {
|
ctx.state.data = {
|
||||||
title: 'DevolverDigital Blog',
|
title: 'DevolverDigital Blog',
|
||||||
|
language: 'en-us',
|
||||||
link: 'https://www.devolverdigital.com/blog',
|
link: 'https://www.devolverdigital.com/blog',
|
||||||
item: items,
|
item: items,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user