From 4ddfe2813cca3e6754039d6da7a15457fd70966e Mon Sep 17 00:00:00 2001 From: Jianjun Xiao Date: Wed, 1 Jan 2025 11:39:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=B7=BB=E5=8A=A0=E5=8C=97?= =?UTF-8?q?=E5=B8=88=E5=A4=A7-=E6=95=99=E8=82=B2=E5=AD=A6=E9=83=A8-?= =?UTF-8?q?=E5=9F=B9=E5=85=BB=E5=8A=A8=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/bnu/fe.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lib/routes/bnu/fe.ts diff --git a/lib/routes/bnu/fe.ts b/lib/routes/bnu/fe.ts new file mode 100644 index 00000000000000..599b79164848eb --- /dev/null +++ b/lib/routes/bnu/fe.ts @@ -0,0 +1,72 @@ +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/fe/:category', + categories: ['university'], + example: '/bnu/fe/18', + parameters: {}, + radar: [ + { + source: ['fe.bnu.edu.cn/pc/cms1info/list/1/:category'], + }, + ], + name: '教育学部-培养动态', + maintainers: ['etShaw-zh'], + handler, + description: `\`https://fe.bnu.edu.cn/pc/cms1info/list/1/18\` 则对应为 \`/bnu/fe/18`, +}; + +async function handler(ctx) { + const { category } = ctx.req.param(); + const apiUrl = 'https://fe.bnu.edu.cn/pc/cmscommon/nlist'; + let response; + try { + // 发送 POST 请求 + response = await got.post(apiUrl, { + headers: { + Accept: 'application/json, text/javascript, */*; q=0.01', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + Origin: 'https://fe.bnu.edu.cn', + Referer: 'https://fe.bnu.edu.cn/pc/cms1info/list/1/18', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', + 'X-Requested-With': 'XMLHttpRequest', + }, + body: `columnid=${category}&page=1`, // POST 数据 + }); + } catch { + throw new Error('Failed to fetch data from API'); + } + const jsonData = JSON.parse(response.body); + // 检查返回的 code + if (jsonData.code !== 0 || !jsonData.data) { + throw new Error('Invalid API response'); + } + + const list = jsonData.data.map((item) => ({ + title: item.title, + link: `https://fe.bnu.edu.cn/html/1/news/${item.htmlpath}/n${item.newsid}.html`, + pubDate: parseDate(item.happendate, 'YYYY-MM-DD'), + })); + + const out = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const response = await got(item.link); + const $ = load(response.data); + item.author = '北京师范大学教育学部'; + item.description = $('.news02_div').text() || '暂无详细内容'; + return item; + }) + ) + ); + + return { + title: '北京师范大学教育学部-培养动态', + link: 'https://fe.bnu.edu.cn/pc/cms1info/list/1/18', + description: '北京师范大学教育学部-培养动态最新通知', + item: out, + }; +}