rsshub
Version:
Make RSS Great Again!
62 lines (61 loc) • 2.19 kB
JavaScript
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs";
import { t as got_default } from "./got-BTowW50G.mjs";
import { t as timezone } from "./timezone-BBvDwS_3.mjs";
import { t as namespace } from "./namespace-T_kZtxVK.mjs";
import { load } from "cheerio";
//#region lib/routes/capitalmuseum/news.ts
const route = {
path: "/news/:type?",
categories: ["travel"],
example: "/capitalmuseum/news/notice",
parameters: { type: "News type, supported values: news(新闻资讯), notice(通知公告). Default: All news." },
name: "News",
maintainers: ["magazian"],
radar: [{
source: ["www.capitalmuseum.org.cn/news"],
target: "/news"
}],
handler: async (ctx) => {
const typeParam = ctx.req.param("type") || "all";
const typeMap = {
notice: "通知公告",
news: "新闻资讯"
};
const targetType = typeMap[typeParam];
const baseUrl = "https://www.capitalmuseum.org.cn";
const apiUrl = `${baseUrl}/news`;
const museumName = namespace.zh?.name || namespace.name;
const $ = load((await got_default({
method: "get",
url: apiUrl
})).data);
const nuxtDataStr = $("#__NUXT_DATA__").text() || "{}";
const nuxtData = JSON.parse(nuxtDataStr);
const getLinksBySection = (sectionName) => {
return $(`span:contains("${sectionName}")`).parent().nextAll("div").first().find("a[href^=\"/news/\"]").toArray().map((el) => $(el).attr("href"));
};
let targetHrefs = [];
if (targetType) targetHrefs = getLinksBySection(targetType);
else if (typeParam === "all") targetHrefs = Object.values(typeMap).flatMap((sectionName) => getLinksBySection(sectionName));
const items = targetHrefs.map((href) => {
const newsId = href.split("/").pop();
const link = `${baseUrl}${href}`;
const newsObj = nuxtData.find((item) => nuxtData[item.newsid] === newsId);
const pubTime = nuxtData[newsObj.publishtime];
const pubDate = timezone(parseDate(pubTime, "YYYY-MM-DD"), 8);
return {
title: nuxtData[newsObj.title],
link,
pubDate
};
});
return {
title: `${museumName} - 首博快讯${targetType ? ` - ${targetType}` : ""}`,
link: apiUrl,
language: "zh-CN",
item: items
};
}
};
//#endregion
export { route };