rsshub
Version:
Make RSS Great Again!
125 lines (124 loc) • 4.52 kB
JavaScript
import { t as rofetch } from "./ofetch-C3ts-Hud.mjs";
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs";
import { t as cache_default } from "./cache-BkqOokyU.mjs";
import { n as rootUrl, t as getSearchParams } from "./utils-BTIOSr4q.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/cls/templates/description.tsx
const renderDescription = ({ images, intro, description }) => renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [
images?.length ? images.map((image) => image?.src ? /* @__PURE__ */ jsx("figure", { children: /* @__PURE__ */ jsx("img", {
src: image.src,
alt: image.alt
}) }, image.src) : null) : null,
intro ? /* @__PURE__ */ jsx("blockquote", { children: intro }) : null,
description ? /* @__PURE__ */ jsx(Fragment, { children: raw(description) }) : null
] }));
//#endregion
//#region lib/routes/cls/subject.ts
const handler = async (ctx) => {
const { id = "1103" } = ctx.req.param();
const limit = ctx.req.query("limit") ? Number(ctx.req.query("limit")) : 20;
const currentUrl = new URL(`subject/${id}`, rootUrl).href;
const apiUrl = new URL(`api/subject/${id}/article`, rootUrl).href;
let items = (await rofetch(apiUrl, { query: getSearchParams({ Subject_Id: id }) })).data.slice(0, limit).map((item) => {
const title = item.article_title;
const description = renderDescription({ intro: item.article_brief });
const guid = `cls-${item.article_id}`;
const image = item.article_img;
return {
title,
description,
pubDate: parseDate(item.article_time, "X"),
link: new URL(`detail/${item.article_id}`, rootUrl).href,
category: item.subjects.map((s) => s.subject_name),
author: item.article_author,
guid,
id: guid,
content: {
html: description,
text: item.article_brief
},
image,
banner: image
};
});
items = await Promise.all(items.map((item) => cache_default.tryGet(item.link, async () => {
const $$ = load(await rofetch(item.link));
const data = JSON.parse($$("script#__NEXT_DATA__").text())?.props?.pageProps?.articleDetail ?? void 0;
if (!data) return item;
const title = data.title;
const description = renderDescription({
images: data.images?.map((i) => ({
src: i,
alt: title
})),
intro: data.brief,
description: data.content
});
const guid = `cls-${data.id}`;
const image = data.images?.[0] ?? void 0;
item.title = title;
item.description = description;
item.pubDate = parseDate(data.ctime, "X");
item.category = [...new Set(data.subject?.flatMap((s) => [s.name, ...s.subjectCategory?.flatMap((c) => [c.columnName || [], c.name || []]) ?? []]))].filter(Boolean);
item.author = data.author?.name ?? item.author;
item.guid = guid;
item.id = guid;
item.content = {
html: description,
text: data.content
};
item.image = image;
item.banner = image;
item.enclosure_url = data.audioUrl;
item.enclosure_type = item.enclosure_url ? `audio/${item.enclosure_url.split(/\./).pop()}` : void 0;
item.enclosure_title = title;
return item;
})));
const $ = load(await rofetch(currentUrl));
const data = JSON.parse($("script#__NEXT_DATA__").text())?.props?.pageProps?.data ?? void 0;
const author = "财联社";
const image = data?.img ?? void 0;
return {
title: `${author} - ${data?.name ?? $("title").text()}`,
description: data?.description ?? void 0,
link: currentUrl,
item: items,
allowEmpty: true,
image,
author
};
};
const route = {
path: "/subject/:id?",
name: "话题",
url: "www.cls.cn",
maintainers: ["nczitzk"],
handler,
example: "/cls/subject/1103",
parameters: { category: "分类,默认为 1103,即A股盘面直播,可在对应话题页 URL 中找到" },
description: `::: tip
若订阅 [有声早报](https://www.cls.cn/subject/1151),网址为 \`https://www.cls.cn/subject/1151\`。截取 \`https://www.cls.cn/subject/\` 到末尾的部分 \`1151\` 作为参数填入,此时路由为 [\`/cls/subject/1151\`](https://rsshub.app/cls/subject/1151)。
:::`,
categories: ["finance"],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["www.cls.cn/subject/:id"],
target: (params) => {
const id = params.id;
return `/subject${id ? `/${id}` : ""}`;
}
}]
};
//#endregion
export { handler, route };