UNPKG

rsshub

Version:
70 lines (68 loc) 2.4 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import { t as config } from "./config-C37vj7VH.mjs"; import "./dist-BInvbO1W.mjs"; import "./logger-Czu8UMNd.mjs"; import "./ofetch-BIyrKU3Y.mjs"; import { t as cache_default } from "./cache-Bo__VnGm.mjs"; import "./helpers-DxBp0Pty.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/baidu/search.ts init_esm_shims(); const renderDescription = (description, images) => art(path.join(__dirname, "templates/description-7ea94b3d.art"), { description, images }); const route = { path: "/search/:keyword", categories: ["other"], example: "/baidu/search/rss", parameters: { keyword: "搜索关键词" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: true, supportBT: false, supportPodcast: false, supportScihub: false }, name: "搜索", maintainers: ["CaoMeiYouRen"], handler }; async function handler(ctx) { const keyword = ctx.req.param("keyword"); const url = `https://www.baidu.com/s?wd=${encodeURIComponent(keyword)}`; const key = `baidu-search:${url}`; const items = await cache_default.tryGet(key, async () => { const response = (await got_default(url)).data; const visitedLinks = /* @__PURE__ */ new Set(); const $ = load(response); return $("#content_left").find(".c-container").toArray().map((el) => { const element = $(el); const link = element.find("h3 a").first().attr("href"); if (link && !visitedLinks.has(link)) { visitedLinks.add(link); const imgs = element.find("img").toArray().map((_el) => $(_el).attr("src")); const description = element.find(".c-gap-top-small [class^=\"content-right_\"]").first().text() || element.find(".c-row").first().text() || element.find(".cos-row").first().text(); return { title: element.find("h3").first().text(), description: renderDescription(description, imgs), link: element.find("h3 a").first().attr("href"), author: element.find(".c-row .c-color-gray").first().text() || "" }; } return null; }).filter((e) => e?.link); }, config.cache.routeExpire, false); return { title: `${keyword} - 百度搜索`, description: `${keyword} - 百度搜索`, link: url, item: items }; } //#endregion export { route };