diff --git a/lib/routes/wenku8/volume.ts b/lib/routes/wenku8/volume.ts index 23c750010..0054d78fd 100644 --- a/lib/routes/wenku8/volume.ts +++ b/lib/routes/wenku8/volume.ts @@ -1,53 +1,41 @@ import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; -import iconv from 'iconv-lite'; +import { decode } from 'iconv-lite'; export default async (ctx) => { - const aid = ctx.req.param('id'); - const novel_url = `https://www.wenku8.net/novel/${Number.parseInt(aid / 1000)}/${aid}/index.htm`; - const $ = load(await get(novel_url)); - - const novel_name = $('#title').text(); - - const volume_list = $('.vcss'); - const last_volume = $(volume_list.at(-1)); - const vid = last_volume.parent().next().find('a')[0].attribs.href.replace('.htm', ''); - - const volume_url = `https://dl.wenku8.com/packtxt.php?aid=${aid}&vid=${vid}&charset=gbk`; - const item_list = await cache.tryGet(volume_url, async () => { - const result = await get(volume_url); - const chapter_list = [...result.matchAll(/ {2}(\S.*)\r\n([\S\s]+?)\r\n\r\n/g)]; - return chapter_list - .filter((chapter) => chapter[2].trim()) - .reverse() - .map((chapter) => ({ - title: chapter[1], - description: format_description(chapter[2]), - guid: Buffer.from(`${vid}${chapter[1]}`).toString('base64'), - })); - }); + const aid = Number.parseInt(ctx.req.param('id')); + const link = `https://www.wenku8.net/novel/${Math.floor(aid / 1000)}/${aid}/index.htm`; + const $ = load(await get(link)); + const vid = $('.vcss').last().parent().next().find('a')[0].attribs.href.replace('.htm', ''); + const volumeUrl = `https://dl.wenku8.com/packtxt.php?aid=${aid}&vid=${vid}&charset=gbk`; + const lastestChapters = $('.vcss') + .last() + .parent() + .nextAll() + .find('a') + .toArray() + .map((a) => ({ link: a.attribs.href })); ctx.set('data', { - title: `轻小说文库 ${novel_name} 最新卷`, - link: novel_url, - item: item_list, + title: `轻小说文库 ${$('#title').text()} 最新卷`, + link, + item: await cache.tryGet(volumeUrl, async () => + [...(await get(volumeUrl)).matchAll(/\s{2}(\S.*)\r?\n([\S\s]+?)\r?\n\r?\n/g)] + .map((chapter, index) => ({ + title: chapter[1], + description: chapter[2] + .split('\r\n') + .filter((line) => line.trim()) + .map((line) => `
${line.trim()}
`) + .join(''), + guid: Buffer.from(`${vid}${chapter[1]}`).toString('base64'), + link: lastestChapters[index]?.link, + })) + .filter((chapter) => chapter.description) + .toReversed() + ), }); }; -const get = async (url) => { - const response = await got({ - method: 'get', - url, - responseType: 'buffer', - }); - - return iconv.decode(response.data, 'gbk'); -}; - -const format_description = (str) => - str - .split('\r\n') - .filter((line) => line.trim()) - .map((line) => `${line.trim()}
`) - .join(''); +const get = async (url: string) => decode(await got(url).buffer(), 'gbk'); diff --git a/website/docs/routes/reading.mdx b/website/docs/routes/reading.mdx index 1a7130d20..0a9257d71 100644 --- a/website/docs/routes/reading.mdx +++ b/website/docs/routes/reading.mdx @@ -367,7 +367,7 @@ ### 最新卷 {#qing-xiao-shuo-wen-ku-zui-xin-juan} -