rsshub
Version:
Make RSS Great Again!
69 lines (65 loc) • 2.92 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 timezone } from "./timezone-BBvDwS_3.mjs";
import { load } from "cheerio";
//#region lib/routes/bast/index.ts
const route = {
path: "/:path{.+}?",
categories: ["new-media"],
example: "/bast/col/col31266",
parameters: { path: "路径,默认为通知公告" },
features: { antiCrawler: true },
name: "通用",
maintainers: ["nczitzk"],
description: `路径处填写对应页面 URL 中 \`https://www.bast.net.cn/\` 后的字段。下面是两个例子。
若订阅 [通知公告](https://www.bast.net.cn/col/col31266) 则将对应页面 URL <https://www.bast.net.cn/col/col31266> 中 \`https://www.bast.net.cn/\` 后的字段 \`col/col31266\` 作为路径填入。此时路由为 [\`/bast/col/col31266\`](https://rsshub.app/bast/col/col31266)
若订阅 [学术动态](https://www.bast.net.cn/col/col31530) 则将对应页面 URL <https://www.bast.net.cn/col/col31530> 中 \`https://www.bast.net.cn/\` 后的字段 \`col/col31530\` 作为路径填入。此时路由为 [\`/bast/col/col31530\`](https://rsshub.app/bast/col/col31530)
如果路由符合 \`/col/colXXXXX\` 的格式,可以由 [\`/bast/col/col31266\`](https://rsshub.app/bast/col/col31266) 精简为 [\`/bast/31266\`](https://rsshub.app/bast/31266)`,
handler
};
async function handler(ctx) {
const colPath = ctx.req.param("path") ?? "32942";
const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit")) : 50;
const currentUrl = `https://www.bast.net.cn/${Number.isNaN(Number(colPath)) ? colPath : `col/col${colPath}`}/`;
let $ = load((await got_default({
method: "get",
url: currentUrl
})).data);
$(".list-title-bif").remove();
const title = $("title").text();
let selection = $("a[title]");
if (selection.length === 0) {
$ = load($("ul.cont-list div script").first().text());
$(".list-title-bif").remove();
selection = $("a[title]");
}
let items = selection.slice(0, limit).toArray().map((item) => {
const $item = $(item);
return {
title: $item.text().trim(),
link: $item.attr("href")
};
});
items = await Promise.all(items.map((item) => cache_default.tryGet(item.link, async () => {
if (/bast\.net\.cn/.test(item.link)) {
const content = load((await got_default({
method: "get",
url: item.link
})).data);
item.title = content("meta[name=\"ArticleTitle\"]").attr("content");
item.author = content("meta[name=\"contentSource\"]").attr("content");
item.pubDate = timezone(parseDate(content("meta[name=\"pubdate\"]").attr("content")), 8);
item.category = [content("meta[name=\"ColumnName\"]").attr("content")];
item.description = content(".arccont").html();
}
return item;
})));
return {
title,
link: currentUrl,
item: items
};
}
//#endregion
export { route };