UNPKG

rsshub

Version:
90 lines (89 loc) 3.41 kB
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as getPlaywrightPage } from "./playwright-o20DiLNh.mjs"; import R from "node:zlib"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { renderToString } from "hono/jsx/dom/server"; import { raw } from "hono/html"; //#region lib/routes/foresightnews/util.tsx const constants = { labelHot: "热门", labelImportant: "重要消息", defaultType: "article" }; const params = { article: "article", event: "timeline", news: "news" }; const rootUrl = "https://foresightnews.pro"; const apiRootUrl = "https://api.foresightnews.pro"; const imgRootUrl = "https://img.foresightnews.pro"; const icon = new URL("foresight.ico", rootUrl).href; const image = new URL("vertical_logo.png", imgRootUrl).href; const processItems = async (apiUrl, limit, ...parameters) => { let searchParams = { size: limit }; for (const param of parameters) searchParams = { ...searchParams, ...param }; const info = { column: "" }; const requestUrl = new URL(apiUrl); for (const [key, value] of Object.entries(searchParams)) requestUrl.searchParams.set(key, String(value)); let responsePromise; const { destroy } = await getPlaywrightPage(requestUrl.href, { onBeforeLoad: async (page) => { await page.route("**/*", (route) => { route.request().resourceType() === "document" ? route.continue() : route.abort(); }); responsePromise = page.waitForResponse(requestUrl.href); } }); let response; try { response = await (await responsePromise).json(); } finally { await destroy(); } const buffer = Buffer.from(response.data?.list ?? response.data, "base64"); let items = JSON.parse(String(R.inflateSync(buffer))); items = (items?.list ?? items).slice(0, limit).map((item) => { const sourceType = item.source_type ?? (item.source_link ? item.column?.title ? "article" : "news" : item.event_type ? "event" : constants.defaultType); item = item.source_type ? item[item.source_type] : item; const column = item.column?.title; info.column ||= column; const categories = [ column, item.event_type, item.is_hot ? constants.labelHot : void 0, item.is_important ? item.important_tag?.name ?? constants.labelImportant : "", item.label, ...item.tags?.map((c) => c.name) ?? [] ].filter((v, index, self) => v && self.indexOf(v) === index); const link = new URL(`${params[sourceType]}/detail/${item.id}`, rootUrl).href; return { title: item.title, link, description: renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [ raw(item.content ?? item.brief ?? ""), item.source_link ? /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("a", { href: item.source_link, children: "来源链接" }), /* @__PURE__ */ jsx("br", {}) ] }) : null, item.img ? /* @__PURE__ */ jsx("figure", { children: /* @__PURE__ */ jsx("img", { src: item.img.split("?", 1)[0] }) }) : null ] })), author: item.column?.title ?? item.author?.username, category: categories, guid: `foresightnews-${sourceType}#${item.id}`, pubDate: item.published_at ? parseDate(item.published_at * 1e3) : void 0, updated: item.last_update_at ? parseDate(item.last_update_at * 1e3) : void 0 }; }); return { items, info }; }; //#endregion export { rootUrl as a, processItems as i, icon as n, image as r, apiRootUrl as t };