Skip to content

Commit

Permalink
fix(route/theatlantic): null image issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Dec 5, 2024
1 parent 0a5e907 commit c731e6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
11 changes: 4 additions & 7 deletions lib/routes/theatlantic/news.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -21,7 +21,7 @@ export const route: Route = {
},
],
name: 'News',
maintainers: ['EthanWng97'],
maintainers: ['EthanWng97', 'pseudoyu'],
handler,
description: `| Popular | Latest | Politics | Technology | Business |
| ------------ | ------ | -------- | ---------- | -------- |
Expand All @@ -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);
Expand Down
18 changes: 10 additions & 8 deletions lib/routes/theatlantic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,30 +16,32 @@ 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) {
item.category.push(channel.slug);
}
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,
});
Expand Down

0 comments on commit c731e6e

Please sign in to comment.