From c731e6e301d070206a4f60dbb90d97e5f8f3ea10 Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Thu, 5 Dec 2024 10:19:52 +0800 Subject: [PATCH] fix(route/theatlantic): null image issue --- lib/routes/theatlantic/news.ts | 11 ++++------- lib/routes/theatlantic/utils.ts | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/routes/theatlantic/news.ts b/lib/routes/theatlantic/news.ts index eab56646aeccd4..d6b056d8174516 100644 --- a/lib/routes/theatlantic/news.ts +++ b/lib/routes/theatlantic/news.ts @@ -1,5 +1,5 @@ import { Route } from '@/types'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { getArticleDetails } from './utils'; export const route: Route = { @@ -21,7 +21,7 @@ export const route: Route = { }, ], name: 'News', - maintainers: ['EthanWng97'], + maintainers: ['EthanWng97', 'pseudoyu'], handler, description: `| Popular | Latest | Politics | Technology | Business | | ------------ | ------ | -------- | ---------- | -------- | @@ -34,11 +34,8 @@ async function handler(ctx) { const host = 'https://www.theatlantic.com'; const category = ctx.req.param('category'); const url = `${host}/${category}/`; - const response = await got({ - method: 'get', - url, - }); - const $ = load(response.data); + const response = await ofetch(url); + const $ = load(response); const contents = JSON.parse($('script#__NEXT_DATA__').text()).props.pageProps.urqlState; const keyWithContent = Object.keys(contents).filter((key) => contents[key].data.includes(category)); const data = JSON.parse(contents[keyWithContent].data); diff --git a/lib/routes/theatlantic/utils.ts b/lib/routes/theatlantic/utils.ts index f0dc4c2da9b079..cba3daa8d28f4a 100644 --- a/lib/routes/theatlantic/utils.ts +++ b/lib/routes/theatlantic/utils.ts @@ -3,7 +3,7 @@ const __dirname = getCurrentPath(import.meta.url); import cache from '@/utils/cache'; import { load } from 'cheerio'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; import { art } from '@/utils/render'; import path from 'node:path'; @@ -16,20 +16,18 @@ const getArticleDetails = async (items) => { items.map((item) => cache.tryGet(item.link, async () => { const url = item.link; - const response = await got({ - url, - method: 'get', + const html = await ofetch(url, { headers: { 'User-Agent': UA, }, }); - const html = response.data; const $ = load(html); let data = JSON.parse($('script#__NEXT_DATA__').text()); const list = data.props.pageProps.urqlState; const keyWithContent = Object.keys(list).filter((key) => list[key].data.includes('content')); data = JSON.parse(list[keyWithContent].data).article; + item.title = data.shareTitle; item.category = data.categories.map((category) => category.slug); for (const channel of data.channels) { @@ -37,9 +35,13 @@ const getArticleDetails = async (items) => { } item.content = data.content.filter((item) => item.innerHtml !== undefined && item.innerHtml !== ''); item.caption = data.dek; - item.imgUrl = data.leadArt.image?.url; - item.imgAlt = data.leadArt.image?.altText; - item.imgCaption = data.leadArt.image?.attributionText; + + if (data.leadArt) { + item.imgUrl = data.leadArt.image?.url; + item.imgAlt = data.leadArt.image?.altText; + item.imgCaption = data.leadArt.image?.attributionText; + } + item.description = art(path.join(__dirname, 'templates/article-description.art'), { item, });