Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/routes/theatlantic/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@ async function handler(ctx) {
const host = 'https://www.theatlantic.com';
const category = ctx.req.param('category');
const url = `${host}/${category}/`;
const response = await ofetch(url);
const init = await ofetch.raw(url, { minVersion: 'TLSv1.3', ignoreResponseError: true });
const headers = {
cookie: init.headers
.getSetCookie()
.find((c) => c.startsWith('__cf_bm='))!
.split(';', 1)[0],
};
const response = init.ok ? init._data : await ofetch(url, { minVersion: 'TLSv1.3', headers });
const $ = load(response);
const contents = JSON.parse($('script#__NEXT_DATA__').text()).props.pageProps.urqlState;
const keyWithContent = Object.keys(contents).find((key) => contents[key].data.includes(category))!;
const data = JSON.parse(contents[keyWithContent].data);
let list = Object.values<any>(data)[0].river.edges;
list = list.filter((item) => !item.node.url.startsWith('https://www.theatlantic.com/photo'));
list = list.map((item) => {
const data = { link: item.node.url, pubDate: item.node.datePublished };
return data;
});
const items = await getArticleDetails(list);
const list = Object.values<any>(contents)
.map((content) => Object.values<any>(JSON.parse(content.data))[0])
.find((data) => data?.river)
.river.edges.filter((item) => !item.node.url.startsWith('https://www.theatlantic.com/photo/'))
.map((item) => {
const data = { link: item.node.url, pubDate: item.node.datePublished };
return data;
});
const items = await getArticleDetails(list, headers);

return {
title: `The Atlantic - ${category.toUpperCase()}`,
Expand Down
37 changes: 24 additions & 13 deletions lib/routes/theatlantic/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { PRESETS } from '@/utils/header-generator';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

const getArticleDetails = async (items) => {
const getArticleDetails = async (items, headers) => {
const list = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const url = item.link;
const html = await ofetch(url, {
headerGeneratorOptions: PRESETS.MODERN_ANDROID,
minVersion: 'TLSv1.3',
headers,
});
const $ = load(html);
let data = JSON.parse($('script#__NEXT_DATA__').text());
Expand All @@ -27,7 +29,7 @@ const getArticleDetails = async (items) => {
for (const channel of data.channels) {
item.category.push(channel.slug);
}
item.content = data.content.filter((item) => item.innerHtml !== undefined && item.innerHtml !== '');
item.content = data.content.filter((item) => item.innerHtml || item.url);
item.caption = data.dek;

if (data.leadArt) {
Expand All @@ -46,17 +48,26 @@ const getArticleDetails = async (items) => {
<figcaption>{item.imgCaption}</figcaption>
</figure>
) : null}
{item.content.map((contentItem) => (
<>
{raw(contentItem.innerHtml)}
{contentItem.tagName ? null : (
<>
<br />
<br />
</>
)}
</>
))}
{item.content.map((contentItem) =>
contentItem.url ? (
<figure>
<img src={contentItem.url} alt={contentItem.altText ?? ''} />
<figcaption>
{raw(contentItem.captionText ?? '')} {contentItem.attributionText}
</figcaption>
</figure>
) : (
<>
{raw(contentItem.innerHtml)}
{contentItem.tagName ? null : (
<>
<br />
<br />
</>
)}
</>
)
)}
</div>
);
return {
Expand Down
Loading