rsshub
Version:
Make RSS Great Again!
83 lines (80 loc) • 3.01 kB
JavaScript
import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs";
import { t as got_default } from "./got-BTowW50G.mjs";
import { a as renderDescription, i as processMeta, n as getMeta, r as processItems, t as domain } from "./util-CW0W4HoM.mjs";
import { load } from "cheerio";
//#region lib/routes/whu/news.ts
const route = {
path: "/news/:category{.+}?",
categories: ["university"],
example: "/whu/news",
parameters: { category: "分类,见下表,默认为 `wdzx/wdyw`, 即 **武大要闻**" },
name: "新闻网",
maintainers: ["SChen1024", "nczitzk"],
handler,
description: `category 参数可选,范围如下:
| 新闻栏目 | 武大资讯 | 学术动态 | 珞珈影像 | 武大视频 |
| -------- | ---------------- | ----------- | ---------------- | ---------------- |
| 参数 | 0 或 \`wdzx/wdyw\` | 1 或 \`kydt\` | 2 或 \`stkj/ljyx\` | 3 或 \`stkj/wdsp\` |
此外 route 后可以加上 \`?limit=n\` 的查询参数,表示只获取前 n 条新闻;如果不指定默认为 10。`
};
const parseCategory = (category) => {
const outputs = [
"wdzx/wdyw",
"kydt",
"stkj/ljyx",
"stkj/wdsp"
];
if ([
"0",
"1",
"2",
"3"
].includes(category)) return outputs[category];
if (outputs.includes(category)) return category;
return "wdzx/wdyw";
};
async function handler(ctx) {
let { category } = ctx.req.param();
const limit = ctx.req.query("limit") ? Number(ctx.req.query("limit")) : 10;
category = parseCategory(category);
const rootUrl = `https://news.${domain}`;
const currentUrl = new URL(`${category}.htm`, rootUrl).href;
const { data: response } = await got_default(currentUrl);
const $ = load(response);
let items = $("ul.wdzxList li a[title], ul.xsdtList li a, div.topPic a[title], ul.nypicList li a[title], div.topVid a[title], ul.nyvidList li a[title]").slice(0, limit).toArray().map((item) => {
const $item = $(item);
const image = $item.find("div.img img");
return {
title: $item.prop("title") ?? $item.find("h4.eclips").text(),
link: new URL($item.prop("href"), rootUrl).href,
pubDate: parseDate($item.find("time").text(), ["YYYY.MM.DD", "DDYYYY.MM"]),
description: renderDescription({
description: $item.find("div.txt p").html() ?? void 0,
image: image.prop("src") ? {
src: new URL(image.prop("src"), rootUrl).href,
alt: image.prop("alt")
} : void 0
})
};
});
items = await processItems(items, rootUrl);
const meta = processMeta(response);
const siteName = getMeta(meta, "SiteName");
const columnName = getMeta(meta, "ColumnName");
const icon = new URL($("link[rel=\"shortcut icon\"]").prop("href"), rootUrl).href;
return {
item: items,
title: `${siteName} - ${columnName}`,
link: currentUrl,
description: getMeta(meta, "description"),
language: $("html").prop("lang"),
image: new URL($("div.logo img").prop("src"), rootUrl).href,
icon,
logo: icon,
subtitle: columnName,
author: siteName,
allowEmpty: true
};
}
//#endregion
export { route };