UNPKG

rsshub

Version:
88 lines (87 loc) 3.26 kB
import { t as rofetch } from "./ofetch-U0CdpE2g.mjs"; import { t as config } from "./config-BfTrYkQS.mjs"; import { t as isWorker } from "./is-worker-Dkdjp-Po.mjs"; import { t as getSignedHeaders } from "./sign-JzOJfAzW.mjs"; import { load } from "cheerio"; //#region lib/routes/zhihu/utils.ts const header = { "x-api-version": "3.0.91" }; const fixImageUrl = (url) => url.split("?", 1)[0].replace("_b.jpg", ".jpg").replace("_r.jpg", ".jpg").replace("_720w.jpg", ".jpg"); const processImage = (content) => { const $ = load(content, null, false); $("noscript, a[data-draft-type=\"mcn-link-card\"]").remove(); $("a").each((_, elem) => { const href = $(elem).attr("href"); if (href?.startsWith("http://link.zhihu.com/?target=") || href?.startsWith("https://link.zhihu.com/?target=")) { const target = new URL(href).searchParams.get("target") || ""; try { $(elem).attr("href", decodeURIComponent(target)); } catch {} } }); $("img.content_image, img.origin_image, img.content-image, img.data-actualsrc, figure>img").each((i, e) => { if (e.attribs["data-actualsrc"]) { $(e).attr({ src: fixImageUrl(e.attribs["data-actualsrc"]), width: null, height: null }); $(e).removeAttr("data-actualsrc"); } else if (e.attribs["data-original"]) { $(e).attr({ src: fixImageUrl(e.attribs["data-original"]), width: null, height: null }); $(e).removeAttr("data-original"); } else $(e).attr({ src: fixImageUrl(e.attribs.src), width: null, height: null }); }); return $.html(); }; const getCookieValueFrom = (cookieStr, key) => cookieStr?.split(";").map((e) => e.trim()).find((e) => e.startsWith(key + "="))?.slice(key.length + 1) || ""; const withZhihuClient = async (pageUrl, callback) => { const configured = config.zhihu.cookies || ""; const dc0 = getCookieValueFrom(configured, "d_c0"); const hasConfiguredSession = !!dc0 && !!getCookieValueFrom(configured, "__zse_ck"); if (!isWorker && !hasConfiguredSession) { const { createJSDOMClient } = await import("./jsdom-w-JuyAVV.mjs"); return callback(createJSDOMClient(pageUrl, configured, dc0)); } let browserPromise; const startBrowser = async (apiPath) => { const { createBrowserClient } = await import("./browser-CRoQIvpn.mjs"); return createBrowserClient(pageUrl, dc0 ? configured : "", apiPath?.startsWith("/api/v4/columns/") ? apiPath : void 0); }; const getBrowser = (apiPath) => browserPromise ??= startBrowser(apiPath); try { return await callback({ get: async (apiPath) => { if (!apiPath.startsWith("/api/")) throw new Error("zhihu: expected an API path"); if (hasConfiguredSession) return rofetch(`https://www.zhihu.com${apiPath}`, { headers: { ...getSignedHeaders(apiPath, dc0), cookie: configured, Referer: pageUrl } }); return (await getBrowser(apiPath)).get(apiPath); }, getPage: async () => hasConfiguredSession ? rofetch(pageUrl, { headers: { cookie: configured, Referer: pageUrl }, parseResponse: (text) => text }) : (await getBrowser()).getPage() }); } finally { let browser; try { browser = await browserPromise; } catch {} await browser?.close(); } }; //#endregion export { processImage as n, withZhihuClient as r, header as t };