rsshub
Version:
Make RSS Great Again!
47 lines (45 loc) • 1.89 kB
JavaScript
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs";
import { t as parseDate } from "./parse-date-CjhZE69i.mjs";
import { t as cache_default } from "./cache-CiDoUSLo.mjs";
import { t as timezone } from "./timezone-DE--ze1V.mjs";
import { load } from "cheerio";
//#region lib/routes/ngd/index.ts
const route = {
path: "/:slug?",
categories: ["government"],
example: "/ngd",
parameters: { slug: "见下文" },
name: "新闻中心",
maintainers: ["nczitzk"],
description: `将目标栏目的网址拆解为 \`http://www.ngd.org.cn/\` 和后面的字段,去掉 \`.htm\` 后,把后面的字段中的 \`/\` 替换为 \`-\`,即为该路由的 slug
如:(要闻动态)\`http://www.ngd.org.cn/xwzx/ywdt/index.htm\` 的网址在 \`http://www.ngd.org.cn/\` 后的字段是 \`xwzx/ywdt/index.htm\`,则对应的 slug 为 \`xwzx-ywdt-index\`,对应的路由即为 \`/ngd/xwzx-ywdt-index\``,
handler
};
const rootUrl = "https://www.ngd.org.cn";
async function handler(ctx) {
const { slug = "xwzx-ywdt-index" } = ctx.req.param();
const currentUrl = `${rootUrl}/${slug.replaceAll("-", "/")}.htm`;
const $ = load(await rofetch(currentUrl));
const list = $(".gp-ellipsis a").slice(0, 15).toArray().map((item) => {
const $item = $(item);
return {
title: $item.text(),
link: `${currentUrl.replace("/index.htm", "")}/${$item.attr("href")}`
};
});
const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => {
const content = load(await rofetch(item.link));
const info = content(".articleAuthor").text().split("|");
item.author = info[0].replace("来源:", "");
item.description = content(".gp-article").html();
item.pubDate = timezone(parseDate(info.at(-1).replace("发布时间:", "")), 8);
return item;
})));
return {
title: $("title").text(),
link: currentUrl,
item: items
};
}
//#endregion
export { route };