From de2870bbdea540afa0945d555a38f4b5fbb46cbf Mon Sep 17 00:00:00 2001 From: Harvey Qiu Date: Sat, 1 Jun 2024 22:21:56 +0800 Subject: [PATCH] feat(route): add dataguidance news (#15711) * add dataguidanec news * 1. add date 2. fix / * fix: example --------- --- lib/routes/dataguidance/index.ts | 66 ++++++++++++++++++++++++++++ lib/routes/dataguidance/namespace.ts | 7 +++ 2 files changed, 73 insertions(+) create mode 100644 lib/routes/dataguidance/index.ts create mode 100644 lib/routes/dataguidance/namespace.ts diff --git a/lib/routes/dataguidance/index.ts b/lib/routes/dataguidance/index.ts new file mode 100644 index 000000000..3529706f4 --- /dev/null +++ b/lib/routes/dataguidance/index.ts @@ -0,0 +1,66 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + name: 'News', + example: '/dataguidance/news', + path: '/news', + radar: [ + { + source: ['dataguidance.com/search/news'], + }, + ], + maintainers: ['harveyqiu'], + handler, + url: 'dataguidance.com/news', +}; + +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); + + let items = $('.field-name-title') + .toArray() + .map((item) => { + item = $(item); + const a = item.find('a').first(); + + return { + title: a.text(), + link: `${rootUrl}${a.attr('href')}`, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = load(detailResponse.data); + item.pubDate = parseDate(content('.field-name-post-date').text()); + item.description = content('.field-name-body').html(); + + return item; + }) + ) + ); + + return { + title: 'Data Guidance News', + link: currentUrl, + item: items, + }; +} diff --git a/lib/routes/dataguidance/namespace.ts b/lib/routes/dataguidance/namespace.ts new file mode 100644 index 000000000..e063fb973 --- /dev/null +++ b/lib/routes/dataguidance/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'DataGuidance', + url: 'dataguidance.com', + categories: ['other'], +};