fix(route): economist espresso (#17227)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { Route, ViewType } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { config } from '@/config';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import xxhash from 'xxhash-wasm';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
|
||||
const link = 'https://www.economist.com/the-world-in-brief';
|
||||
|
||||
@@ -32,76 +35,33 @@ export const route: Route = {
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const $ = await cache.tryGet(
|
||||
const { h64ToString } = await xxhash();
|
||||
const nextData = await cache.tryGet(
|
||||
link,
|
||||
async () => {
|
||||
const response = await got(link);
|
||||
return load(response.data);
|
||||
const response = await ofetch(link);
|
||||
const $ = cheerio.load(response);
|
||||
return JSON.parse($('script#__NEXT_DATA__').text());
|
||||
},
|
||||
config.cache.routeExpire,
|
||||
false
|
||||
);
|
||||
|
||||
const nextData = JSON.parse($('script#__NEXT_DATA__').text());
|
||||
const parts = nextData.props.pageProps.content.hasPart.parts[0].hasPart.parts.filter((part) => part.type.includes('Article') && part.headline !== '');
|
||||
|
||||
const renderHTML = (node) => {
|
||||
let el;
|
||||
switch (node.type) {
|
||||
case 'tag':
|
||||
el = $(`<${node.name}>`);
|
||||
for (const name in node.attribs) {
|
||||
el.attr(name, node.attribs[name]);
|
||||
}
|
||||
if (node.children) {
|
||||
el.append(node.children.map((childNode) => renderHTML(childNode)));
|
||||
}
|
||||
return el;
|
||||
case 'text':
|
||||
return node.data;
|
||||
default:
|
||||
el = $('<div>');
|
||||
el.append(node.map((n) => renderHTML(n)));
|
||||
return el;
|
||||
}
|
||||
};
|
||||
|
||||
const gobbetPart = parts.shift();
|
||||
const gobbets = gobbetPart.text.map((node, index) => {
|
||||
const html = renderHTML(node).html();
|
||||
return {
|
||||
guid: `${gobbetPart.id}/${index}`,
|
||||
link,
|
||||
title: gobbetPart.headline,
|
||||
pubDate: gobbetPart.datePublished,
|
||||
description: html,
|
||||
};
|
||||
});
|
||||
|
||||
const articles = parts.map((part) => {
|
||||
const image = part.image.main;
|
||||
const imgNode = {
|
||||
type: 'tag',
|
||||
name: 'img',
|
||||
attribs: { width: image.width, height: image.height, src: image.url.canonical },
|
||||
};
|
||||
|
||||
const html = renderHTML([imgNode, ...part.text]).html();
|
||||
|
||||
return {
|
||||
link,
|
||||
guid: part.id,
|
||||
title: part.headline,
|
||||
pubDate: part.datePublished,
|
||||
description: html,
|
||||
};
|
||||
});
|
||||
const { content, metadata } = nextData.props.pageProps;
|
||||
const items = content.gobbets.map((item) => ({
|
||||
link,
|
||||
title: sanitizeHtml(item, { allowedTags: [], allowedAttributes: {} }),
|
||||
pubDate: parseDate(content.datePublished),
|
||||
description: item,
|
||||
guid: `${link}#${h64ToString(item)}`,
|
||||
}));
|
||||
|
||||
return {
|
||||
title: $('head title').text(),
|
||||
title: metadata.title,
|
||||
link,
|
||||
description: $('meta[property="og:description"]').attr('content'),
|
||||
description: metadata.description,
|
||||
language: 'en-gb',
|
||||
item: [...gobbets, ...articles],
|
||||
image: metadata.imageUrl,
|
||||
item: items,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user