From da71fba92f8aae42ef3946d1edcc2698c5a5059c Mon Sep 17 00:00:00 2001 From: Pat Ramsey <35958163+vicguo0724@users.noreply.github.com> Date: Fri, 18 Jul 2025 00:23:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=96=B0=E5=A2=9E=E5=8D=97?= =?UTF-8?q?=E5=BC=80=E5=A4=A7=E5=AD=A6=E6=95=99=E5=8A=A1=E5=A4=84=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=85=AC=E5=91=8A=E8=B7=AF=E7=94=B1=20(#19622)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 新增南开大学教务处通知公告路由 - 添加nankai/jwc路由,抓取通知公告列表 - 支持公告详情内容获取与PDF链接处理 - 自动转换相对链接为绝对链接 - 适配发布时间和内容解析 * fix(route): 修复南开大学教务处文件名解析 - 修正sudyfile-attr属性解析为JSON,避免正则提取截断文件名 * chore(route): 更新/nankai/jwc的维护者信息 - 将维护者从guozz修改为vicguo0724 * refactor(route): 简化/nankai/jwc item返回逻辑 * refactor(route): 删除/nankai/jwc 日期格式拼接中的额外填充 - 修正日期拼接时多余的padStart调用,原本的day已经是两位数格式 --- lib/routes/nankai/jwc.ts | 131 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 lib/routes/nankai/jwc.ts diff --git a/lib/routes/nankai/jwc.ts b/lib/routes/nankai/jwc.ts new file mode 100644 index 0000000000..e2c9ce5236 --- /dev/null +++ b/lib/routes/nankai/jwc.ts @@ -0,0 +1,131 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; +import timezone from '@/utils/timezone'; + +export const route: Route = { + path: '/jwc', + categories: ['university'], + example: '/nankai/jwc', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['jwc.nankai.edu.cn/tzgg/list.htm'], + target: '/jwc', + }, + ], + name: '教务处通知公告', + maintainers: ['vicguo0724'], + description: '南开大学教务处通知公告', + url: 'jwc.nankai.edu.cn', + handler: async () => { + const baseUrl = 'https://jwc.nankai.edu.cn'; + const { data: response } = await got(`${baseUrl}/tzgg/list.htm`); + const $ = load(response); + + // 解析列表页面中的所有通知项 + const list = $('.page-con-list-news .item') + .toArray() + .map((item) => { + const $item = $(item); + const $link = $item.find('.t a'); + const $dateDay = $item.find('.d .d-d'); + const $dateMonth = $item.find('.d .d-m'); + + // 构建完整的日期 + const day = $dateDay.text().trim(); // 格式:04 + const monthYear = $dateMonth.text().trim(); // 格式:2025/06 + const fullDate = `${monthYear}/${day}`; // 2025/06/04 + + let linkStr = $link.attr('href'); + // 处理相对链接 + if (linkStr && !linkStr.startsWith('http')) { + linkStr = `${baseUrl}${linkStr}`; + } + + return { + title: $link.text().trim(), + link: linkStr, + pubDate: timezone(parseDate(fullDate, 'YYYY/MM/DD'), +8), + }; + }) + .filter((item) => item.link); // 过滤掉没有链接的项目 + + // 获取每个通知的详细内容 + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + try { + const { data: response } = await got(item.link); + const $ = load(response); + + // 获取发布时间(更精确的时间) + const publishTimeText = $('.page-news-souse').text(); + const timeMatch = publishTimeText.match(/发布时间:(\d{4}-\d{2}-\d{2})/); + if (timeMatch) { + item.pubDate = timezone(parseDate(timeMatch[1]), +8); + } + + // 获取文章内容 + const content = $('.page-news-con .wp_articlecontent'); + if (content.length > 0) { + // 处理PDF链接,转换为绝对链接 + content.find('a').each((i, el) => { + const $el = $(el); + const href = $el.attr('href'); + if (href && !href.startsWith('http')) { + if (href.startsWith('/')) { + $el.attr('href', `${baseUrl}${href}`); + } else { + $el.attr('href', `${baseUrl}/${href}`); + } + } + }); + + // 处理PDF播放器div,提取PDF链接 + content.find('.wp_pdf_player').each((i, el) => { + const $el = $(el); + + const pdfSrc = $el.attr('pdfsrc'); + const sudyfileAttr = ($(el).attr('sudyfile-attr') || '{}').replaceAll("'", '"'); + const sudyfileAttrJson = JSON.parse(sudyfileAttr); + const fileName = sudyfileAttrJson.title || '未命名文件.pdf'; + if (pdfSrc) { + let pdfUrl = pdfSrc; + if (!pdfUrl.startsWith('http')) { + pdfUrl = `${baseUrl}${pdfUrl}`; + } + // 替换PDF播放器为下载链接 + $el.replaceWith(`
`); + } + }); + + item.description = content.html(); + } else { + item.description = '无法获取内容详情'; + } + } catch { + item.description = '获取内容失败'; + } + return item; + }) + ) + ); + + return { + title: '南开大学教务处-通知公告', + link: `${baseUrl}/tzgg/list.htm`, + item: items, + }; + }, +};