UNPKG

rsshub

Version:
67 lines (65 loc) 3.01 kB
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as cache_default } from "./cache-BkqOokyU.mjs"; import { t as got_default } from "./got-BTowW50G.mjs"; import { t as timezone } from "./timezone-BBvDwS_3.mjs"; import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime"; import { load } from "cheerio"; import { renderToString } from "hono/jsx/dom/server"; import { raw } from "hono/html"; //#region lib/routes/ifeng/news.tsx const route = { path: "/news/:path{.+}?", categories: ["new-media"], example: "/ifeng/news", parameters: { path: "路径,对应分类资讯页 URL 路径,默认为空" }, name: "资讯", maintainers: ["nczitzk"], handler, description: `::: tip 路径处填写对应页面 URL 中 \`https://news.ifeng.com/\` 后的字段。下面是一个例子。 若订阅 [大湾区\\_资讯\\_凤凰网](https://news.ifeng.com/shanklist/3-305565-) 则将对应页面 URL \`https://news.ifeng.com/shanklist/3-305565-\` 中 \`https://news.ifeng.com/\` 后的字段 \`shanklist/3-305565-\` 作为路径填入。此时路由为 [\`/ifeng/news/shanklist/3-305565-\`](https://rsshub.app/ifeng/news/shanklist/3-305565-) :::` }; async function handler(ctx) { const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit")) : 20; const path = ctx.req.param("path"); const currentUrl = `https://news.ifeng.com${path ? `/${path}` : ""}`; const response = await got_default({ method: "get", url: currentUrl }); const $ = load(response.data); let items = JSON.parse(response.data.match(/"newsstream":(\[.*?\]),"cooperation"/)[1]).slice(0, limit).map((item) => ({ title: item.title, link: item.url, pubDate: timezone(parseDate(item.newsTime), 8), description: item.thumbnails.image.pop() })); items = await Promise.all(items.map((item) => cache_default.tryGet(item.link, async () => { const detailResponse = await got_default({ method: "get", url: item.link }); item.author = detailResponse.data.match(/"editorName":"(.*?)",/)[1]; item.category = detailResponse.data.match(/\},"keywords":"(.*?)",/)[1].split(","); const image = item.description; const description = JSON.parse(detailResponse.data.match(/"contentList":(\[.*?\]),/)[1]).map((content) => content.data); item.description = renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [image ? /* @__PURE__ */ jsx("figure", { children: /* @__PURE__ */ jsx("img", { src: image.url, height: image.height, width: image.width }) }) : null, description?.length ? description.map((entry) => entry?.attachmentType === "video" ? /* @__PURE__ */ jsx("video", { controls: true, poster: entry.bigPosterUrl, children: /* @__PURE__ */ jsx("source", { src: entry.playUrl }) }) : typeof entry === "string" ? /* @__PURE__ */ jsx(Fragment, { children: raw(entry.replaceAll("data-lazyload=", "src=")) }) : null) : null] })); return item; }))); return { title: $("title").text(), link: currentUrl, item: items }; } //#endregion export { route };