From 7ef0be848f522861dbffd62dbf8b09708450dda4 Mon Sep 17 00:00:00 2001 From: liyaozhong Date: Fri, 1 Nov 2024 01:07:28 +0800 Subject: [PATCH] feat(route): add enterprisecraftsmanship (#17371) feat(route): fix issues --- lib/routes/enterprisecraftsmanship/index.ts | 74 +++++++++++++++++++ .../enterprisecraftsmanship/namespace.ts | 6 ++ 2 files changed, 80 insertions(+) create mode 100644 lib/routes/enterprisecraftsmanship/index.ts create mode 100644 lib/routes/enterprisecraftsmanship/namespace.ts diff --git a/lib/routes/enterprisecraftsmanship/index.ts b/lib/routes/enterprisecraftsmanship/index.ts new file mode 100644 index 000000000..044571c25 --- /dev/null +++ b/lib/routes/enterprisecraftsmanship/index.ts @@ -0,0 +1,74 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import logger from '@/utils/logger'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/archives', + categories: ['blog'], + example: '/enterprisecraftsmanship/archives', + radar: [ + { + source: ['enterprisecraftsmanship.com/archives/'], + }, + ], + url: 'enterprisecraftsmanship.com/', + name: 'Archives', + maintainers: ['liyaozhong'], + handler, + description: 'Enterprise Craftsmanship blog archives', +}; + +async function handler() { + const rootUrl = 'https://enterprisecraftsmanship.com'; + const currentUrl = `${rootUrl}/archives`; + + const response = await got(currentUrl); + + const $ = load(response.data); + + let items = $('.postIndexItem') + .toArray() + .map((item) => { + const $item = $(item); + const title = $item.find('.title a').text().trim(); + const link = new URL($item.find('.title a').attr('href'), currentUrl).href; + const dateStr = $item.find('.date').text().trim(); + const pubDate = parseDate(dateStr); + + return { + title, + link, + pubDate, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + try { + const detailResponse = await got(item.link); + const $detail = load(detailResponse.data); + + // 获取 .post 内容,但排除 .post-info + const $post = $detail('.post'); + $post.find('.post-info').remove(); + item.description = $post.html(); + + return item; + } catch (error) { + logger.error(`处理文章 ${item.link} 时发生错误: ${error}`); + return item; + } + }) + ) + ); + + return { + title: 'Enterprise Craftsmanship - Archives', + link: currentUrl, + item: items, + }; +} diff --git a/lib/routes/enterprisecraftsmanship/namespace.ts b/lib/routes/enterprisecraftsmanship/namespace.ts new file mode 100644 index 000000000..3bf4dfa48 --- /dev/null +++ b/lib/routes/enterprisecraftsmanship/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Enterprise Craftsmanship', + url: 'enterprisecraftsmanship.com', +};