From f17408d98aaab598e53bea9da793242e6aa2c11d Mon Sep 17 00:00:00 2001 From: liyaozhong Date: Sat, 2 Nov 2024 11:23:00 +0800 Subject: [PATCH] feat(route): add nielsberglund (#17398) fix(route): fix pr issue --- lib/routes/nielsberglund/index.ts | 78 +++++++++++++++++++++++++++ lib/routes/nielsberglund/namespace.ts | 7 +++ 2 files changed, 85 insertions(+) create mode 100644 lib/routes/nielsberglund/index.ts create mode 100644 lib/routes/nielsberglund/namespace.ts diff --git a/lib/routes/nielsberglund/index.ts b/lib/routes/nielsberglund/index.ts new file mode 100644 index 000000000..5f4122948 --- /dev/null +++ b/lib/routes/nielsberglund/index.ts @@ -0,0 +1,78 @@ +import { Route, DataItem } from '@/types'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/blog', + categories: ['blog'], + example: '/nielsberglund/blog', + radar: [ + { + source: ['nielsberglund.com/'], + }, + ], + url: 'nielsberglund.com/', + name: 'Blog', + maintainers: ['liyaozhong'], + handler, + description: 'Niels Berglund Blog Posts', +}; + +async function handler() { + const rootUrl = 'https://nielsberglund.com'; + const currentUrl = rootUrl; + + const response = await got(currentUrl); + const $ = load(response.data); + + let items = $('.post-preview') + .toArray() + .map((item) => { + const $item = $(item); + const $link = $item.find('a').first(); + const href = $link.attr('href'); + const title = $item.find('.post-title').first().text().trim(); + const dateStr = $item.find('.post-meta').text().trim(); + + if (!href || !title) { + return null; + } + + const link = new URL(href, rootUrl).href; + const pubDate = parseDate(dateStr); + + return { + title, + link, + pubDate, + } as DataItem; + }) + .filter((item): item is DataItem => item !== null); + + items = ( + await Promise.all( + items.map((item) => + cache.tryGet(item.link as string, async () => { + try { + const detailResponse = await got(item.link); + const $detail = load(detailResponse.data); + + item.description = $detail('.post-container').html() || ''; + + return item; + } catch { + return item; + } + }) + ) + ) + ).filter((item): item is DataItem => item !== null); + + return { + title: 'Niels Berglund Blog', + link: rootUrl, + item: items, + }; +} diff --git a/lib/routes/nielsberglund/namespace.ts b/lib/routes/nielsberglund/namespace.ts new file mode 100644 index 000000000..4f221a7f4 --- /dev/null +++ b/lib/routes/nielsberglund/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Niels Berglund Blog', + url: 'nielsberglund.com', + lang: 'en', +};