Files
RSSHub/lib/routes/51read/article.ts
T
Tony 5527d18de9 refactor: remove unnecessary trim/first/last (#22837)
* refactor: remove unnecessary trim/first/last

* refactor(types): allow null in DataItem.description

* refactor: remove unnecessary fallback

* refactor(types): allow null in Data.description

* refactor: remove unnecessary fallback

* refactor: remove .clone()

* refactor: refine json content text

* fIx: cognition

* fix: more fixes

* fix(route/jiemian): use api

* fix(route/jandan): housekeeping

add back maintainers from #8904

* fix(route/kcna/news): update source and target mappings

* fix(route/jandan): sanitize HTML content in titles

* refactor: remove unnecessary referer

* fix(route/jinse): regex
2026-07-27 22:23:57 +08:00

88 lines
2.3 KiB
TypeScript

import { load } from 'cheerio';
import type { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
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 bookHtml = await ofetch(link);
const $book = load(bookHtml);
const chapter = `https://m.51read.org/zhangjiemulu/${id}`;
const chapterHtml = await ofetch(chapter);
const $chapter = load(chapterHtml);
const pageLength = $chapter('.ml-page select')
.find('option')
.toArray()
.map((option) => option.attribs.value).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 html = await ofetch(url);
const $latest = load(html);
const item = await Promise.all(
$latest('.kb-jp li>a')
.toArray()
.map((chapter) => buildItem(chapter.attribs.href))
.toReversed()
);
return item;
};
const buildItem = (url: string) =>
cache.tryGet(url, async () => {
const html = await ofetch(url);
const $ = load(html);
return {
title: $('h1').text(),
description: $('.kb-cot').html(),
link: url,
};
}) as Promise<DataItem>;