From d9f8019f587fb2abeefdec2618818ce69ef01114 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 8 Aug 2024 10:09:05 -0700 Subject: [PATCH] fix(route): use wp api for techcrunch (#16357) --- lib/routes/techcrunch/news.ts | 49 ++++++++----------- .../techcrunch/templates/description.art | 7 +++ 2 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 lib/routes/techcrunch/templates/description.art diff --git a/lib/routes/techcrunch/news.ts b/lib/routes/techcrunch/news.ts index 09ff9ab90..604c74d61 100644 --- a/lib/routes/techcrunch/news.ts +++ b/lib/routes/techcrunch/news.ts @@ -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', diff --git a/lib/routes/techcrunch/templates/description.art b/lib/routes/techcrunch/templates/description.art new file mode 100644 index 000000000..3cb00899e --- /dev/null +++ b/lib/routes/techcrunch/templates/description.art @@ -0,0 +1,7 @@ +{{ if head.og_image }} +{{ each head.og_image img }} + +{{ /each }} +
+{{ /if }} +{{@ rendered }}