UNPKG

rsshub

Version:
114 lines (112 loc) 3.84 kB
import "./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 parseDate } from "./parse-date-BrP7mxXf.mjs"; import "./md5-C8GRvctM.mjs"; import "./cache-Bo__VnGm.mjs"; import "./helpers-DxBp0Pty.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import { t as config_not_found_default } from "./config-not-found-Dyp3RlZZ.mjs"; import { r as processImage } from "./utils-Vxf_K345.mjs"; //#region lib/routes/zhihu/timeline.ts const route = { path: "/timeline", categories: ["social-media"], example: "/zhihu/timeline", parameters: {}, features: { requireConfig: [{ name: "ZHIHU_COOKIES", description: "" }], requirePuppeteer: false, antiCrawler: true, supportBT: false, supportPodcast: false, supportScihub: false }, name: "用户关注时间线", maintainers: ["SeanChao"], handler, description: `::: warning 用户关注动态需要登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。 :::` }; async function handler(ctx) { const cookie = config.zhihu.cookies; if (cookie === void 0) throw new config_not_found_default("缺少知乎用户登录后的 Cookie 值"); const feeds = (await got_default({ method: "get", url: `https://www.zhihu.com/api/v3/moments`, headers: { Cookie: cookie }, searchParams: { desktop: true, limit: ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit")) : 15 } })).data.data; const urlBase = "https://zhihu.com"; const buildLink = (e) => { if (!e || !e.target || !e.target.type) return ""; const id = e.target.id; switch (e.target.type) { case "answer": return `${urlBase}/question/${e.target.question.id}/answer/${id}`; case "pin": case "article": return e.target.url; case "question": return `${urlBase}/question/${id}`; default: return; } }; /** * Returns one non-undefined/null element from a list of items * make sure there exists at least one element that is non-undefined * * @param {Array} list a list of items to be filtered */ const getOne = (list) => list.find((e) => e !== void 0 && e !== null); const buildActors = (e) => { const actors = e.actors; if (!actors) return ""; return actors.map((e$1) => e$1.name).join(", "); }; const getContent = (content) => { if (!content || !Array.isArray(content)) return content; return content.map((e) => e.content).filter((e) => !!e && typeof e === "string").map((e) => `<div>${e}</div>`).join(""); }; const buildItem = (e) => { if (!e || !e.target) return {}; const link = buildLink(e); return { title: `${e.action_text_tpl.replace("{}", buildActors(e))}: ${getOne([e.target.title, e.target.question ? e.target.question.title : ""])}`, description: processImage(`<div>${getOne([ e.target.content_html, getContent(e.target.content), e.target.detail, e.target.excerpt, "" ])}</div>`), pubDate: parseDate(e.updated_time * 1e3), link, author: e.target.author ? e.target.author.name : "", guid: link, category: [e.verb] }; }; return { title: `知乎关注动态`, link: `https://www.zhihu.com/follow`, item: feeds.filter((e) => e.verb).map((e) => { if (e && e.type && e.type === "feed_group") return { title: e.group_text.replace("{LIST_COUNT}", e.list.length), description: e.list && Array.isArray(e.list) ? e.list.map((e$1) => buildItem(e$1)).map((e$1) => `<a href="${e$1.link}"><b>${e$1.title}</b></a><br><p>${e$1.description}</p><br>`).join("") : "", pubDate: e.list && Array.isArray(e.list) && e.list.length > 0 ? parseDate(e.list[0].updated_time * 1e3) : /* @__PURE__ */ new Date(), guid: e.link, category: [e.verb] }; return buildItem(e); }) }; } //#endregion export { route };