UNPKG

rsshub

Version:
121 lines (120 loc) 3.77 kB
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/zju/math/index.ts const base = "http://www.math.zju.edu.cn/"; const categoryMap = /* @__PURE__ */ new Map([ [0, { id: "zytz/list.htm", title: "浙江大学数学科学院-重要通知" }], [1, { id: "bkstz/list.htm", title: "浙江大学数学科学院-本科生" }], [2, { id: "yjstz/list.htm", title: "浙江大学数学科学院-研究生" }], [3, { id: "kytz/list.htm", title: "浙江大学数学科学院-科研" }], [4, { id: "jxtz/list.htm", title: "浙江大学数学科学院-教学" }], [5, { id: "38069/list.htm", title: "浙江大学数学科学院-人事" }], [6, { id: "gs/list.htm", title: "浙江大学数学科学院-公示" }] ]); async function fetchNewsItemsByCategory(categoryId) { const $ = load((await got_default({ method: "get", url: new URL(categoryId, base).href })).data); return $(".news_list #wp_news_w12 li").toArray().map((item) => { const element = $(item); const link = element.find("a[href]").attr("href"); const titleNode = element.find(".title"); const intranetOnly = titleNode.find("img[src=\"/_images/news/icon/unopen.gif\"]").length > 0; const title = titleNode.text() || element.find("a[href]").attr("title"); const dateText = `${element.find(".date .y").text()}-${element.find(".date .d").text()}`; if (!(title && link)) return null; return { item: { title, link: new URL(link, base).href, pubDate: timezone(parseDate(dateText), 8) }, intranetOnly }; }).filter(Boolean); } async function enrichNewsItemWithDetails(item, refererUrl) { const dataItem = item.item; if (item.intranetOnly || !dataItem.link) return dataItem; return await cache_default.tryGet(dataItem.link, async () => { try { const $ = load((await got_default({ method: "get", url: dataItem.link, headers: { Referer: refererUrl } })).data); const description = $(".wp_articlecontent").html(); const [, author, pubDate] = $(".item_info").text().match(/来源:([\s\S]*?)发布时间:(\d{4}-\d{2}-\d{2})/) ?? []; dataItem.description = description; dataItem.author = author; if (pubDate) dataItem.pubDate = timezone(parseDate(pubDate), 8); return dataItem; } catch { return dataItem; } }); } const route = { path: "/math/:type", categories: ["university"], example: "/zju/math/0", parameters: { type: "分类,见下表" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, name: "数学科学学院", description: `| 重要通知 | 本科生 | 研究生 | 科研 | 教学 | 人事 | 公示 | | -------- | ------ | ------ | ---- | ---- | ---- | ---- | | 0 | 1 | 2 | 3 | 4 | 5 | 6 |`, maintainers: ["Alex222222222222"], handler, url: "www.math.zju.edu.cn" }; async function handler(ctx) { const type = Math.trunc(Number(ctx.req.param("type"))); const categoryInfo = categoryMap.get(type); if (!categoryInfo) { const validTypes = [...categoryMap.keys().toArray()].join(", "); throw new Error(`Invalid type: ${type}. Valid types are: ${validTypes}`); } const categoryUrl = new URL(categoryInfo.id, base).href; const newsItems = await fetchNewsItemsByCategory(categoryInfo.id); const items = await Promise.all(newsItems.map((item) => enrichNewsItemWithDetails(item, categoryUrl))); return { title: categoryInfo.title, link: categoryUrl, item: items }; } //#endregion export { route };