UNPKG

rsshub

Version:
200 lines (199 loc) 5.3 kB
import { t as rofetch } from "./ofetch-C3ts-Hud.mjs"; import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as cache_default } from "./cache-BkqOokyU.mjs"; import { load } from "cheerio"; import sanitizeHtml from "sanitize-html"; //#region lib/routes/jandan/utils.ts /** * Extract page ID from script tags and title */ const extractPageMeta = (url) => cache_default.tryGet(`jandan:pageMeta:${url}`, async () => { const $ = load(await rofetch(url)); return { pageId: $("script:contains(\"PAGE\")").text().match(/PAGE\s*=\s*\{\s*id\s*:\s*(\d+)\s*\}/)?.[1] ?? "", title: $("title").text().trim() }; }); /** * Handle the top section (热榜) */ const handleTopSection = async (rootUrl, type) => { const response = await rofetch(`${rootUrl}/api/top/${type}`); let title = "热榜"; switch (type) { case "pic3days": title += " - 3天内无聊图"; break; case "pic7days": title += " - 7天内无聊图"; break; default: title += " - 4小时热门"; break; } if (response.code !== 0) throw new Error(`未能获取热榜数据: ${title}`); const items = response.data.map((item) => { const content = item.content.replaceAll(/img src="(.*?)"/g, (match, src) => match.replace(src, () => src.replace(/^https?:\/\/(\w+)\.moyu\.im/, "https://$1.sinaimg.cn"))); return { author: item.author, title: `${item.author}: ${sanitizeHtml(content, { allowedTags: [], allowedAttributes: {} })}`, description: content, pubDate: parseDate(item.date_gmt), link: `${rootUrl}/t/${item.id}` }; }); return { title, items }; }; /** * Handle the forum/bbs section (鱼塘) */ const handleForumSection = async (rootUrl) => { const title = "煎蛋 - 鱼塘"; const currentUrl = `${rootUrl}/new/forum`; const forumData = await rofetch(`${rootUrl}/api/forum/posts/112928?page=1`); if (forumData.code !== 0) throw new Error("未能获取鱼塘数据"); return { title, items: forumData.data.list.map((post) => ({ author: post.author_name, title: post.title, pubDate: parseDate(post.create_time), updated: parseDate(post.update_time), link: `${rootUrl}/new/forum/topic/${post.post_id}`, category: post.reply_count > 0 ? [`${post.reply_count}条回复`] : void 0 })), link: currentUrl }; }; /** * Handle other sections (问答, 树洞, 随手拍, 女装, 无聊图) */ const handleCommentSection = async (rootUrl, category) => { const currentUrl = `${rootUrl}/${category}`; const { pageId, title: pageTitle } = await extractPageMeta(currentUrl); const title = pageTitle || `煎蛋 - ${category}`; if (!pageId) throw new Error("无法从页面中获取到帖子ID,可能网站结构已变更"); const commentsData = await rofetch(`${rootUrl}/api/comment/post/${pageId}?order=desc&page=0`); if (commentsData.code !== 0) throw new Error("没有获取到内容,可能需要更新解析规则"); return { title, items: commentsData.data.list.map((comment) => { const content = comment.content.replaceAll(/img src="(.*?)"/g, (match, src) => match.replace(src, () => src.replace(/^https?:\/\/(\w+)\.moyu\.im/, "https://$1.sinaimg.cn"))); return { author: comment.author, title: `${comment.author}: ${sanitizeHtml(content, { allowedTags: [], allowedAttributes: {} })}`, description: content, pubDate: parseDate(comment.date_gmt), link: `${rootUrl}/t/${comment.id}` }; }) }; }; //#endregion //#region lib/routes/jandan/section.ts const route = { path: "/:category/:type?", example: "/jandan/top", name: "Section", maintainers: [ "kobemtl", "Xuanwo", "xyqfer", "9uanhuo", "nczitzk", "pseudoyu" ], parameters: { category: { description: "板块", options: [ { label: "热榜", value: "top" }, { label: "问答", value: "qa" }, { label: "树洞", value: "treehole" }, { label: "随手拍", value: "ooxx" }, { label: "女装", value: "beauty" }, { label: "无聊图", value: "pic" }, { label: "鱼塘", value: "bbs" } ] }, type: { description: "热榜类型,仅当 category 选择 `top` 时有效", default: "4hr", options: [ { label: "4小时热门", value: "4hr" }, { label: "3天内无聊图", value: "pic3days" }, { label: "7天内无聊图", value: "pic7days" } ] } }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["i.jandan.net/:category"], target: "/jandan/:category?" }], handler }; async function handler(ctx) { let category = ctx.req.param("category") ?? "top"; category = category.replace(/#.*$/, ""); const type = ctx.req.param("type") ?? "4hr"; const rootUrl = "https://i.jandan.net"; const currentUrl = `${rootUrl}/${category}`; let result; if (category === "top") result = await handleTopSection(rootUrl, type); else if (category === "bbs") result = await handleForumSection(rootUrl); else result = await handleCommentSection(rootUrl, category); return { title: result.title, link: result.link ?? currentUrl, item: result.items }; } //#endregion export { route };