fix(route): use wp api for techcrunch (#16357)

This commit is contained in:
Ethan 2024-08-08 10:09:05 -07:00 committed by GitHub
parent f61e16766b
commit d9f8019f58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 29 deletions

View File

@ -1,8 +1,12 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import parser from '@/utils/rss-parser';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
const host = 'https://techcrunch.com';
export const route: Route = {
path: '/news',
@ -29,33 +33,20 @@ export const route: Route = {
};
async function handler() {
const rssUrl = `${host}/feed/`;
const feed = await parser.parseURL(rssUrl);
const items = await Promise.all(
feed.items.map((item) =>
cache.tryGet(item.link, async () => {
const url = item.link;
const response = await got({
url,
method: 'get',
});
const html = response.data;
const $ = load(html);
const description = $('#root');
description.find('.article__title').remove();
description.find('.article__byline__meta').remove();
description.find('.mobile-header-nav').remove();
description.find('.desktop-nav').remove();
return {
title: item.title,
pubDate: item.pubDate,
link: item.link,
category: item.categories,
description: description.html(),
};
})
)
);
const { data } = await got(`${host}/wp-json/wp/v2/posts`);
const items = data.map((item) => {
const head = item.yoast_head_json;
const $ = load(item.content.rendered, null, false);
return {
title: item.title.rendered,
description: art(path.join(__dirname, 'templates/description.art'), {
head,
rendered: $.html(),
}),
link: item.link,
pubDate: parseDate(item.date_gmt),
};
});
return {
title: 'TechCrunch',

View File

@ -0,0 +1,7 @@
{{ if head.og_image }}
{{ each head.og_image img }}
<img src="{{ img.url.split('?')[0] }}">
{{ /each }}
<br>
{{ /if }}
{{@ rendered }}