From 652f22098933570370735ea7fc0fc1a7c45ac77e Mon Sep 17 00:00:00 2001 From: Lyunvy <64697357+Lyunvy@users.noreply.github.com> Date: Fri, 4 Oct 2024 22:39:24 +0800 Subject: [PATCH] feat(route): add xbookcn (#16990) * feat(route): add xbookcn * use toArray instead. * 1. Change the categories; 2. Remove the maximum number of route customization. Use the common parameter limit instead; 3. Correct the example path; 4. Correct the name; * fix: namespace name --------- --- lib/routes/xbookcn/blog.ts | 66 +++++++++++++++++++++++++++++++++ lib/routes/xbookcn/namespace.ts | 6 +++ 2 files changed, 72 insertions(+) create mode 100644 lib/routes/xbookcn/blog.ts create mode 100644 lib/routes/xbookcn/namespace.ts diff --git a/lib/routes/xbookcn/blog.ts b/lib/routes/xbookcn/blog.ts new file mode 100644 index 000000000..98e907901 --- /dev/null +++ b/lib/routes/xbookcn/blog.ts @@ -0,0 +1,66 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/:label?', // 路由路径 + categories: ['reading'], // 分类 + example: '/xbookcn/精选作品', // 示例路径 + parameters: { label: '按名称分类,详见https://blog.xbookcn.net/p/all.html' }, // 参数 + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + name: '短篇', // 源名称 + maintainers: ['Lyunvy'], // 维护者 + handler: async (ctx) => { + const { label = '精选作品' } = ctx.req.param(); // 从请求参数中获取 label + const url = `https://blog.xbookcn.net/search/label/${label}`; // 使用反引号构建 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') + .toArray() + .map((el) => $(el).text().trim()); + item.category = categories; // 添加多个分类信息 + + return item; // 返回带有描述和分类的文章对象 + }) + ) + ); + + return { + title: 'xbookcn', // 源标题 + link: url, // 源链接 + item: items, // 源文章 + }; + }, +}; diff --git a/lib/routes/xbookcn/namespace.ts b/lib/routes/xbookcn/namespace.ts new file mode 100644 index 000000000..0df530de5 --- /dev/null +++ b/lib/routes/xbookcn/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '中文成人文學網', + url: 'www.xbookcn.net', +};