UNPKG

rsshub

Version:
123 lines (122 loc) 3.55 kB
import { t as rofetch } from "./ofetch-U0CdpE2g.mjs"; import { t as config } from "./config-BfTrYkQS.mjs"; import { t as parseDate } from "./parse-date-3kcy2Eau.mjs"; import { t as timezone } from "./timezone-UiMFOuvL.mjs"; //#region lib/routes/yamibo/utils.ts const bbsOrigin = "https://bbs.yamibo.com"; function getDate(date) { return timezone(parseDate(date), 8); } const allowedResources = /* @__PURE__ */ new Set([ "document", "script", "xhr", "fetch" ]); var ThreadFetcher = class { browser; pending = Promise.resolve(); closed = false; async openBrowser(link) { const { getPlaywrightPage } = await import("./playwright-pWUKW7xK.mjs"); const browser = await getPlaywrightPage(link, { noGoto: true, closeTimeout: 0 }); try { const { auth, salt } = config.yamibo; if (auth && salt) await browser.context.addCookies([{ name: "EeqY_2132_saltkey", value: salt, url: bbsOrigin }, { name: "EeqY_2132_auth", value: auth, url: bbsOrigin }]); await browser.page.route("**/*", (route) => allowedResources.has(route.request().resourceType()) ? route.continue() : route.abort()); return browser; } catch (error) { await browser.destroy(); throw error; } } fetchBrowser(link) { const result = this.readBrowser(this.pending, link); this.pending = result; return result; } async readBrowser(previous, link) { try { await previous; } catch {} if (this.closed) throw new Error("yamibo: the browser session has already closed"); this.browser ??= this.openBrowser(link); const { page } = await this.browser; await page.goto(link, { waitUntil: "domcontentloaded" }); try { await page.waitForSelector("#postlist", { state: "attached", timeout: 1e4 }); } catch { throw new Error("yamibo: browser access did not return thread content. Check that the thread is accessible and YAMIBO_AUTH and YAMIBO_SALT are valid."); } return page.content(); } async fetchThread(tid, options) { const params = new URLSearchParams({ mod: "viewthread", tid }); if (options?.ordertype) params.set("ordertype", options.ordertype); const link = `${bbsOrigin}/forum.php?${params}`; const { auth, salt } = config.yamibo; const headers = {}; if (auth && salt) headers.cookie = `EeqY_2132_saltkey=${salt}; EeqY_2132_auth=${auth}`; const data = await rofetch(link, { headers }); return { link, data: data.startsWith("<script type=\"text/javascript\">") ? await this.fetchBrowser(link) : data }; } async close() { this.closed = true; try { await this.pending; } catch {} if (this.browser) { let browser; try { browser = await this.browser; } catch { return; } await browser.destroy(); } } }; async function fetchThread(tid, options) { const fetcher = new ThreadFetcher(); try { return await fetcher.fetchThread(tid, options); } finally { await fetcher.close(); } } function generateDescription($item, postId) { const content = $item.find(`#postmessage_${postId}`).parent(); content.find("img").each((_, img) => { const src = img.attribs.zoomfile ?? img.attribs.src; img.attribs.src = `${bbsOrigin}/${src}`; }); let description = content.html() ?? ""; const images = $item.find(".pattl img").toArray(); for (const img of images) { const src = img.attribs.zoomfile ?? img.attribs.src; description += `<img src="${bbsOrigin}/${src}" />`; } return description; } //#endregion export { getDate as a, generateDescription as i, bbsOrigin as n, fetchThread as r, ThreadFetcher as t };