mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-24 02:08:55 +08:00
fix(route/theatlantic): null image issue
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user