From 0e6090151a4715447efb5f6068182238997bbb4f Mon Sep 17 00:00:00 2001 From: ZhW <1065423410@qq.com> Date: Wed, 7 Aug 2024 10:38:02 +0800 Subject: [PATCH] feat(route): add 51read.org (#16382) * feat(route): add 51read.org * fix(router): only povid last page and rm `UTF-8` encoding * refactor(route): refactor code * fix(router): Reverse the chapter array, so new chapter at the top --- lib/routes/51read/article.ts | 82 ++++++++++++++++++++++++++++++++++ lib/routes/51read/namespace.ts | 6 +++ 2 files changed, 88 insertions(+) create mode 100644 lib/routes/51read/article.ts create mode 100644 lib/routes/51read/namespace.ts diff --git a/lib/routes/51read/article.ts b/lib/routes/51read/article.ts new file mode 100644 index 000000000..a2e50b941 --- /dev/null +++ b/lib/routes/51read/article.ts @@ -0,0 +1,82 @@ +import { load } from 'cheerio'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import type { Route, DataItem } from '@/types'; + +export const route: Route = { + path: '/article/:id', + name: '章节', + url: 'm.51read.org', + maintainers: ['lazwa34'], + example: '/51read/article/152685', + parameters: { id: '小说 id, 可在对应小说页 URL 中找到' }, + categories: ['reading'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['m.51read.org/xiaoshuo/:id'], + target: '/article/:id', + }, + { + source: ['51read.org/xiaoshuo/:id'], + target: '/article/:id', + }, + ], + handler, +}; + +async function handler(ctx) { + const { id } = ctx.req.param(); + const link = `https://m.51read.org/xiaoshuo/${id}`; + const $book = load(await ofetch(link)); + + const chapter = `https://m.51read.org/zhangjiemulu/${id}`; + const $chapter = load(await ofetch(chapter)); + + const pageLength = $chapter('.ml-page select') + .find('option') + .map((_, option) => option.attribs.value) + .toArray().length; + + const item = await createItem(chapter, pageLength); + + return { + title: $book('h1').text(), + description: $book('.bi-cot p').text(), + link, + item, + image: $book('.bi-img img').attr('src'), + author: $book('.bi-wt a').text(), + language: 'zh-cn', + }; +} + +const createItem = async (baseUrl: string, page: number) => { + const url = `${baseUrl}/${page}`; + const $latest = load(await ofetch(url)); + const item = await Promise.all( + $latest('.kb-jp li>a') + .map((_, chapter) => buildItem(chapter.attribs.href)) + .toArray() + .toReversed() + ); + return item; +}; + +const buildItem = (url: string) => + cache.tryGet(url, async () => { + const $ = load(await ofetch(url)); + + return { + title: $('h1').text(), + description: $('.kb-cot').html() || '', + link: url, + }; + }) as Promise; diff --git a/lib/routes/51read/namespace.ts b/lib/routes/51read/namespace.ts new file mode 100644 index 000000000..1c50a9ed2 --- /dev/null +++ b/lib/routes/51read/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '51Read', + url: 'm.51read.org', +};