Skip to content

Commit

Permalink
fix(route): devolverdigital no longer uses embedded json (#16440)
Browse files Browse the repository at this point in the history
* fix(route): devolverdigital no longer uses embedded json

* Update blog.ts

* Fetch only the first page

* Update blog.ts
  • Loading branch information
XXY233 authored Aug 15, 2024
1 parent adce405 commit f917bdc
Showing 1 changed file with 59 additions and 42 deletions.
101 changes: 59 additions & 42 deletions lib/routes/devolverdigital/blog.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { DataItem, Route } from '@/types';
import { load } from 'cheerio';
import { ofetch } from 'ofetch';

export const route: Route = {
path: '/blog',
categories: ['blog'],
categories: ['game'],
example: '/devolverdigital/blog',
parameters: {},
features: {
Expand All @@ -28,44 +27,7 @@ export const route: Route = {
};

async function handler() {
const baseUrl = 'https://www.devolverdigital.com/blog';
const { data: response } = await got(baseUrl);

const $ = load(response);
const nextData = JSON.parse($('#__NEXT_DATA__').text());

const items = await Promise.all(
nextData.props.pageProps.posts.map((postData) => {
const postUrl = `${baseUrl}/post/${postData.id}`;
return cache.tryGet(postUrl, async () => {
const { data: postPage } = await got(postUrl);

const $page = load(postPage);
$page('noscript').remove();
const postContent = $page('div.flex > div > div > div > div:not([class])');

// 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', ''));
});

return {
title: postData.title,
link: postUrl,
author: postData.author,
pubDate: Date.parse(postData.createdAt),
updated: Date.parse(postData.updatedAt),
description: postContent.html(),
};
});
})
);
const items = await fetchPage();

return {
title: 'DevolverDigital Blog',
Expand All @@ -74,3 +36,58 @@ async function handler() {
item: items,
};
}

async function fetchPage() {
const baseUrl = 'https://www.devolverdigital.com/blog';
const response = await ofetch(baseUrl);
const $ = load(response, { scriptingEnabled: false });

// Extract all posts of this page
const $titleDivs = $('div.w-full.flex.justify-center.py-4.bg-red-400.undefined');
const $contentDivs = $('div.bg-gray-800.flex.justify-center.font-sm.py-4');
const items: DataItem[] = $titleDivs.toArray().map((titleDiv, index) => {
const content = $contentDivs[index];
const postAuthor = parsePostAuthor($, titleDiv);
const postDate = parsePostDate($, titleDiv);
const postTitle = $(titleDiv).find('h1').text();
const postLink = $(content).find('div.ml-auto.flex.items-center a').attr('href');
// Modify the src attribute of the image
parsePostImages($, content);
const postContent = $.html($(content).find('div.cms-content'));
return {
title: postTitle,
link: postLink,
author: postAuthor,
pubDate: postDate,
description: postContent,
};
});

return items;
}

function parsePostAuthor($, titleDiv) {
const $postAuthorElement = $(titleDiv).find('div.font-xs.leading-none.mb-1');
return $postAuthorElement.text().replace('By ', '') || 'Devolver Digital';
}

function parsePostDate($, titleDiv) {
const dateStr = $(titleDiv).find('div.font-2xs.leading-none.mb-1').text();
const cleanedDateStr = dateStr.replace(/(\d+)(st|nd|rd|th)/, '$1');
return new Date(cleanedDateStr);
}

function parsePostImages($, content) {
$(content)
.find('img')
.each((index, img) => {
const $img = $(img);
const src = $img.attr('src') || '';
if (src.startsWith('/_next/image')) {
const srcSet = $img.attr('srcset') || '';
const actualSrc = srcSet.split(',').pop()?.split(' ')[0] || src;
$img.attr('src', actualSrc);
}
$img.removeAttr('loading').removeAttr('decoding').removeAttr('data-nimg').removeAttr('style').removeAttr('sizes').removeAttr('srcset').removeAttr('referrerpolicy');
});
}

0 comments on commit f917bdc

Please sign in to comment.