diff --git a/lib/routes/economist/espresso.ts b/lib/routes/economist/espresso.ts index 901a78cb8..10634b838 100644 --- a/lib/routes/economist/espresso.ts +++ b/lib/routes/economist/espresso.ts @@ -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 = $('