Skip to content

Commit

Permalink
fix: fix dataguidance news feed (#17695)
Browse files Browse the repository at this point in the history
* update dataguidance feed

* fix link

* Apply suggestions from code review

---------
  • Loading branch information
harveyqiu authored Nov 24, 2024
1 parent a9f4b38 commit 334a197
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions lib/routes/dataguidance/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -10,57 +10,46 @@ 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', '<br>');
delete item.url;
return item;
})
)
);

return {
title: 'Data Guidance News',
link: currentUrl,
link: 'https://www.dataguidance.com/info?article_type=news_post',
item: items,
};
}

0 comments on commit 334a197

Please sign in to comment.