UNPKG

rsshub

Version:
88 lines (86 loc) 3.85 kB
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as timezone } from "./timezone-BBvDwS_3.mjs"; import { n as getTiebaPageContent, r as normalizeUrl } from "./common-DqeLZ7uH.mjs"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { load } from "cheerio"; import { renderToString } from "hono/jsx/dom/server"; import { raw } from "hono/html"; //#region lib/routes/baidu/tieba/search.tsx const route = { path: "/tieba/search/:qw/:routeParams?", categories: ["bbs"], example: "/baidu/tieba/search/neuro", parameters: { qw: "搜索关键词", routeParams: "额外参数;请参阅以下说明和表格" }, features: { requireConfig: [{ name: "BAIDU_COOKIE", optional: false, description: "百度 cookie 值,用于需要登录的贴吧页面" }], requirePuppeteer: true, antiCrawler: true, supportBT: false, supportPodcast: false, supportScihub: false }, name: "贴吧搜索", maintainers: ["JimenezLi", "FlanChanXwO"], handler, description: `| 键 | 含义 | 接受的值 | 默认值 | | ------------ | ---------------------------------------------------------- | ------------- | ------ | | kw | 在名为 kw 的贴吧中搜索 | 任意名称 / 无 | 无 | | only\\_thread | 只看主题帖,默认为 0 关闭 | 0/1 | 0 | | rn | 返回条目的数量 | 1-20 | 20 | | sm | 排序方式,0 为按时间顺序,1 为按时间倒序,2 为按相关性顺序 | 0/1/2 | 1 | 用例:\`/baidu/tieba/search/neuro/kw=neurosama&only_thread=1&sm=2\`` }; async function handler(ctx) { const qw = ctx.req.param("qw"); const query = new URLSearchParams(ctx.req.param("routeParams")); query.set("ie", "utf-8"); query.set("qw", qw); query.set("rn", query.get("rn") || "20"); const link = `https://tieba.baidu.com/f/search/res?${query.toString()}`; const $ = load(await getTiebaPageContent(link, `tieba:search:${qw}:${query.toString()}`, { waitForSelector: ".thread-content-box", timeout: 3e3 })); const resultList = $(".thread-content-box"); if (resultList.length === 0) throw new Error("No search results found. The page structure may have changed."); return { title: `${qw} - ${query.get("kw") || "百度贴"}吧搜索`, link, item: resultList.toArray().map((element) => { const item = $(element); const title = item.find(".title-content-wrap .title-wrap span").text().trim(); const details = item.find(".abstract-wrap span").text().trim(); const linkHref = normalizeUrl(item.find(".action-bar-warp a.action-link-bg").attr("href") || ""); const author = item.find(".forum-attention.user").text().trim(); const timeMatch = item.find(".top-title").text().trim().match(/发布于\s+(\d{4}-\d{1,2}-\d{1,2})/); const time = timeMatch ? timeMatch[1] : ""; const parsedDate = time ? parseDate(time, "YYYY-M-D") : null; const validPubDate = parsedDate && !Number.isNaN(parsedDate.getTime()) ? timezone(parsedDate, 8) : void 0; const medias = item.find(".p_mediaCont img").toArray().flatMap((element) => { const src = ($(element).attr("original") || "").trim(); return src ? [`<img src="${src}">`] : []; }).join(""); const tieba = item.find(".forum-name-text").text().trim(); return { title, description: renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("p", { children: details }), /* @__PURE__ */ jsx("p", { children: raw(medias) }), /* @__PURE__ */ jsxs("p", { children: ["贴吧:", tieba] }) ] })), author, pubDate: validPubDate, link: linkHref }; }) }; } //#endregion export { route };