rsshub
Version:
Make RSS Great Again!
76 lines (75 loc) • 2.86 kB
JavaScript
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";
//#region lib/routes/baidu/tieba/user.tsx
const route = {
path: "/tieba/user/:uid",
categories: ["bbs"],
example: "/baidu/tieba/user/斗鱼游戏君",
parameters: { uid: "用户 ID" },
features: {
requireConfig: [{
name: "BAIDU_COOKIE",
optional: false,
description: "百度 cookie 值,用于需要登录的贴吧页面"
}],
requirePuppeteer: true,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
name: "用户帖子",
maintainers: [
"igxlin",
"nczitzk",
"FlanChanXwO"
],
handler,
description: "用户 ID 可以通过打开用户的主页后查看地址栏的 `un` 字段来获取。"
};
async function handler(ctx) {
const uid = ctx.req.param("uid");
const encodedUid = encodeURIComponent(uid);
const $ = load(await getTiebaPageContent(`https://tieba.baidu.com/home/main?un=${encodedUid}`, `tieba:user:${uid}`, {
waitForSelector: ".thread-card",
timeout: 3e3
}));
const name = $("span.userinfo_username").text() || uid;
const list = $(".thread-card");
if (list.length === 0) throw new Error("No user posts found. The page structure may have changed or the user does not exist.");
return {
title: `${name} 的贴吧`,
link: `https://tieba.baidu.com/home/main?un=${encodedUid}`,
item: list.toArray().map((element) => {
const item = $(element);
const authorName = item.find(".head-name").text().trim() || name;
const title = item.find(".title-text").text().trim();
const content = item.find(".tb-richtext .text").text().trim();
const images = item.find(".image-list-item img").toArray().map((img) => $(img).attr("src") || $(img).attr("data-src") || "").filter(Boolean);
const timeText = item.find(".post-num").text().trim();
const parsedDate = timeText ? parseDate(timeText, ["YYYY-MM-DD"]) : null;
const validPubDate = parsedDate && !Number.isNaN(parsedDate.getTime()) ? timezone(parsedDate, 8) : void 0;
const link = normalizeUrl(item.find("a.thread-card-content").attr("href") || "");
return {
title,
pubDate: validPubDate,
author: authorName,
description: renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [content ? /* @__PURE__ */ jsx("p", { children: content }) : null, images.length > 0 ? /* @__PURE__ */ jsx("div", { children: images.map((img) => /* @__PURE__ */ jsx("img", {
src: img,
alt: "",
style: {
maxWidth: "100%",
margin: "5px 0"
}
})) }) : null] })),
link
};
})
};
}
//#endregion
export { route };