From 334a197179e777f8f11d542c8bfc6c90fa78e710 Mon Sep 17 00:00:00 2001 From: Harvey Qiu Date: Sun, 24 Nov 2024 22:42:53 +0800 Subject: [PATCH] fix: fix dataguidance news feed (#17695) * update dataguidance feed * fix link * Apply suggestions from code review --------- --- lib/routes/dataguidance/index.ts | 49 +++++++++++++------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/lib/routes/dataguidance/index.ts b/lib/routes/dataguidance/index.ts index 3529706f465d88..74a40c31f5d1cd 100644 --- a/lib/routes/dataguidance/index.ts +++ b/lib/routes/dataguidance/index.ts @@ -1,7 +1,7 @@ import { Route } from '@/types'; import cache from '@/utils/cache'; -import got from '@/utils/got'; -import { load } from 'cheerio'; +import ofetch from '@/utils/ofetch'; + import { parseDate } from '@/utils/parse-date'; export const route: Route = { @@ -10,49 +10,38 @@ export const route: Route = { path: '/news', radar: [ { - source: ['dataguidance.com/search/news'], + source: ['www.dataguidance.com/info'], }, ], maintainers: ['harveyqiu'], handler, - url: 'dataguidance.com/news', + url: 'https://www.dataguidance.com/info?article_type=news_post', }; async function handler() { const rootUrl = 'https://www.dataguidance.com'; - const currentUrl = `${rootUrl}/search/news/`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const $ = load(response.data); + const url = 'https://dgcb20-ca-northeurope-dglive.yellowground-c1f17366.northeurope.azurecontainerapps.io/api/v1/content/articles?order=DESC_publishedOn&limit=25&article_types=news_post'; - let items = $('.field-name-title') - .toArray() - .map((item) => { - item = $(item); - const a = item.find('a').first(); + const response = await ofetch(url); - return { - title: a.text(), - link: `${rootUrl}${a.attr('href')}`, - }; - }); + const data = response.data; + let items = data.map((item) => ({ + title: item.title.en, + link: `${rootUrl}${item.url}`, + url: item.url, + pubDate: parseDate(item.publishedOn), + })); + const baseUrl = 'https://dgcb20-ca-northeurope-dglive.yellowground-c1f17366.northeurope.azurecontainerapps.io/api/v1/content/articles/by_path?path='; items = await Promise.all( items.map((item) => cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + const detailUrl = `${baseUrl}${item.url}`; - const content = load(detailResponse.data); - item.pubDate = parseDate(content('.field-name-post-date').text()); - item.description = content('.field-name-body').html(); + const detailResponse = await ofetch(detailUrl); + item.description = detailResponse.contentBody?.html.en.replaceAll('\n', '
'); + delete item.url; return item; }) ) @@ -60,7 +49,7 @@ async function handler() { return { title: 'Data Guidance News', - link: currentUrl, + link: 'https://www.dataguidance.com/info?article_type=news_post', item: items, }; }