rsshub
Version:
Make RSS Great Again!
151 lines (150 loc) • 5.85 kB
JavaScript
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 namespace } from "./namespace-BObCzy4A.mjs";
import { jsx, jsxs } from "hono/jsx/jsx-runtime";
import { load } from "cheerio";
import { renderToString } from "hono/jsx/dom/server";
//#region lib/routes/hnmuseum/exhibitions.tsx
const formatStr = (dateStr) => {
if (!dateStr) return;
const match = dateStr.match(/(\d{4})年\s*(\d{1,2})月\s*(\d{1,2})日/);
if (match) return `${match[1]}-${match[2].padStart(2, "0")}-${match[3].padStart(2, "0")}`;
};
const extractDates = (durationStr) => {
if (!durationStr || durationStr.includes("永久")) return {
startDate: void 0,
endDate: void 0
};
const parts = durationStr.split(/—/);
return {
startDate: formatStr(parts[0]),
endDate: formatStr(parts[1])
};
};
const renderDescription = (imgUrl, location, startDate, endDate, fullDuration) => renderToString(/* @__PURE__ */ jsxs("div", { children: [
/* @__PURE__ */ jsx("img", { src: imgUrl }),
/* @__PURE__ */ jsx("br", {}),
/* @__PURE__ */ jsxs("p", { children: [/* @__PURE__ */ jsx("b", { children: "地点:" }), location ?? "参考展览详情或图片"] }),
/* @__PURE__ */ jsxs("p", { children: [/* @__PURE__ */ jsx("b", { children: "开展:" }), startDate ?? "参考展览详情或图片"] }),
/* @__PURE__ */ jsxs("p", { children: [/* @__PURE__ */ jsx("b", { children: "闭展:" }), endDate ?? "参考展览详情或图片"] }),
fullDuration && /* @__PURE__ */ jsx("p", { children: /* @__PURE__ */ jsxs("small", { children: ["原始展期:", fullDuration] }) })
] }));
const route = {
path: "/current-exhibitions/:type?",
categories: ["travel"],
example: "/hnmuseum/current-exhibitions/special",
parameters: { type: "Exhibition type, supported values: special(临时展览 special+temporary). Default: All exhibitions." },
name: "Current Exhibitions",
maintainers: ["magazian"],
radar: [{
source: ["www.hnmuseum.com/zh-hans/content/当前展览-基本陈列"],
target: "/current-exhibitions"
}],
handler: async (ctx) => {
const isSpecial = ctx.req.param("type") === "special";
const baseUrl = "https://www.hnmuseum.com";
const listUrl = `${baseUrl}/zh-hans/content/当前展览-基本陈列`;
const museumName = namespace.zh?.name || namespace.name;
const $ = load((await got_default({
method: "get",
url: listUrl
})).data);
const list = [
{
selector: "#block-views-a784821b4fd9f41563c7164fd2a2f96e .views-row",
type: "permanent",
extra: ($item) => ({
location: $item.find(".views_zhanting .field-content").text(),
fullDuration: $item.find(".views_startdate .field-content").text().trim()
})
},
{
selector: "#block-views-chen-lie-block .views-row",
type: "special",
extra: ($item) => ({ location: $item.find(".views_zhanting .field-content").text() })
},
{
selector: "#block-views-zhan-lan-block .views-row",
type: "temporary",
extra: (_$item) => ({})
}
].flatMap((config) => $(config.selector).toArray().map((item) => {
const $item = $(item);
const $a = $item.find(".views_title a, .views_img_bg a").first();
const link = $a.attr("href") || "";
return {
exhibitionType: config.type,
title: $a.text(),
itemLink: link.startsWith("http") ? link : `${baseUrl}${link}`,
imgUrl: $item.find("img").attr("src") || "",
...config.extra($item)
};
}));
const targetList = isSpecial ? list.filter((item) => item.exhibitionType === "special" || item.exhibitionType === "temporary") : list;
const items = await Promise.all(targetList.map((item) => {
const cacheKey = item.itemLink;
return cache_default.tryGet(cacheKey, async () => {
if (item.exhibitionType === "permanent" || item.exhibitionType === "special") {
const { startDate, endDate } = extractDates(item.fullDuration);
return {
title: item.title,
link: item.itemLink,
pubDate: startDate ? parseDate(startDate) : void 0,
description: renderDescription(item.imgUrl, item.location, startDate, endDate, item.fullDuration),
_extra: {
museumName,
location: item.location,
startDate,
endDate
}
};
}
if (new URL(item.itemLink).hostname === "vrexhibition.hnmuseum.com") {
const jsSrc = load((await got_default({
method: "get",
url: item.itemLink
})).data)("script[type=\"module\"][src]").attr("src") || "";
const jsUrl = new URL(jsSrc, item.itemLink).href;
const jsContent = (await got_default({
method: "get",
url: jsUrl
})).data;
const title = jsContent.match(/"title"[^)]*\)\s*\},[^"]*"([^"]+)"/)?.[1];
const fullDuration = jsContent.match(/"(\d{4}年\d{1,2}月\d{1,2}日—\d{4}年\d{1,2}月\d{1,2}日)"/)?.[1];
const location = jsContent.match(/"(湖南省博物馆[^"]+厅)"/)?.[1];
const { startDate, endDate } = extractDates(fullDuration);
return {
title,
link: item.itemLink,
pubDate: startDate ? parseDate(startDate) : void 0,
description: renderDescription(item.imgUrl, location, startDate, endDate, fullDuration),
_extra: {
museumName,
location,
startDate,
endDate
}
};
}
return {
title: load((await got_default({
method: "get",
url: item.itemLink
})).data)("h1#page-title").text(),
link: item.itemLink,
description: renderDescription(item.imgUrl),
_extra: { museumName }
};
});
}));
return {
title: `${museumName} - 当前展览${isSpecial ? " - 专题&临时展览" : ""}`,
link: listUrl,
language: "zh-CN",
item: items.filter((item) => item.title && Object.keys(item).length > 0)
};
}
};
//#endregion
export { route };