feat(route): add enterprisecraftsmanship (#17371)

feat(route): fix issues
This commit is contained in:
liyaozhong
2024-11-01 01:07:28 +08:00
committed by GitHub
parent 4d518cc398
commit 7ef0be848f
2 changed files with 80 additions and 0 deletions
@@ -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,
};
}
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Enterprise Craftsmanship',
url: 'enterprisecraftsmanship.com',
};