Skip to content

Commit

Permalink
feat(route): add xbookcn
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyunvy committed Oct 2, 2024
1 parent 155116d commit faf21ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/routes/xbookcn/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import cache from '@/utils/cache';

export const route: Route = {
path: '/:label?/:max?', // 路由路径
categories: ['other'], // 分类
example: '/xbookcn/:label?', // 示例路径
parameters: { label: '按名称分类,详见https://blog.xbookcn.net/p/all.html', max: '最大条数' }, // 参数
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: 'xbookcn', // 源名称
maintainers: ['Lyunvy'], // 维护者
handler: async (ctx) => {
const { label = '精选作品', max = '20' } = ctx.req.param(); // 从请求参数中获取 label
const url = `https://blog.xbookcn.net/search/label/${label}?max-results=${max}`; // 使用反引号构建 URL
const response = await ofetch(url); // 请求源链接
const $ = load(response); // 加载 HTML

const articles = $('.blog-posts.hfeed .date-outer').find('.post'); // 查找文章

const list = articles.toArray().map((elem) => {
const a = $(elem).find('.post-title a'); // 获取标题链接
return {
title: a.text().trim(), // 标题
link: a.attr('href'), // 链接
category: [], // 分类
};
});

const items = await Promise.all(
list.map(async (item) =>
// 使用缓存以避免重复请求
await cache.tryGet(item.link, async () => {
const response = await ofetch(item.link); // 请求文章链接
const $ = load(response); // 加载文章页面

// 获取文章的完整描述
item.description = $('.post-body.entry-content').html() || '无内容'; // 抓取指定内容

// 获取分类信息
const categories = $('.post-labels a')
.map((i, el) => $(el).text().trim())
.get();

Check warning

Code scanning / ESLint

Disallow specified syntax Warning

Please use toArray instead.
item.category = categories; // 添加多个分类信息

return item; // 返回带有描述和分类的文章对象
})
)
);

return {
title: 'xbookcn', // 源标题
link: url, // 源链接
item: items, // 源文章
};
},
};
6 changes: 6 additions & 0 deletions lib/routes/xbookcn/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'xbookcn',
url: 'blog.xbookcn.net',
};

0 comments on commit faf21ec

Please sign in to comment.