From 08a869df113f52dad3a99507a0bbeaad4f74b287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=B9=E6=9E=97=E9=87=8C=E6=9C=89=E5=86=B0?= Date: Sat, 19 Oct 2024 14:37:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(route/zjut):=20Add=20=E6=B5=99=E6=B1=9F?= =?UTF-8?q?=E5=B7=A5=E4=B8=9A=E5=A4=A7=E5=AD=A6=E8=AE=A1=E7=AE=97=E6=9C=BA?= =?UTF-8?q?=E5=AD=A6=E9=99=A2=20(#17192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route/zjut): Add 浙江工业大学计算机学院 * fix(route/zjut): improve timezone setting --- lib/routes/zjut/cs/index.ts | 105 ++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 lib/routes/zjut/cs/index.ts diff --git a/lib/routes/zjut/cs/index.ts b/lib/routes/zjut/cs/index.ts new file mode 100644 index 000000000..7ccdf383f --- /dev/null +++ b/lib/routes/zjut/cs/index.ts @@ -0,0 +1,105 @@ +import { Data, Route } from '@/types'; +import cache from '@/utils/cache'; +import { parseDate } from '@/utils/parse-date'; +import { load } from 'cheerio'; +import ofetch from '@/utils/ofetch'; +import timezone from '@/utils/timezone'; + +const rootUrl = 'https://cs.zjut.edu.cn/jsp/newsclass.jsp?wcId='; +const host = 'cs.zjut.edu.cn'; + +export const route: Route = { + path: '/cs/:type', + categories: ['university'], + example: '/zjut/cs/54', + parameters: { type: '分类,见下表' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + name: '浙江工业大学计算机科学与技术学院、软件学院', + maintainers: ['zhullyb'], + url: 'cs.zjut.edu.cn', + handler, + radar: [ + { + source: ['cs.zjut.edu.cn/jsp/newsclass.jsp?wcId=:type'], + target: '/cs/:type', + }, + ], + description: `| 新闻资讯 | 学术动态 | 通知公告 | +| ------- | ------- | ------- | +| 54 | 55 | 53 |`, +}; + +async function handler(ctx) { + const type = Number.parseInt(ctx.req.param('type')); + const response = await ofetch(rootUrl + type); + const $ = load(response); + + const list = $('dl.news') + .toArray() + .map((item) => { + const cheerioItem = $(item); + const a = cheerioItem.find('a'); + + try { + const title = a.text() || ''; + let link = a.attr('href'); + if (!link) { + link = ''; + } else if (!link.startsWith('http')) { + link = 'https://' + host + '/jsp/' + link; + } + const pubDate = timezone(parseDate(cheerioItem.find('.datetime').text().slice(1, -1)), +8); + + return { + title, + link, + pubDate, + }; + } catch { + return { + title: '', + link: '', + pubDate: Date.now(), + }; + } + }) + .filter((item) => item.title && item.link); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const newItem = { + ...item, + description: '', + }; + if (host === new URL(item.link).hostname) { + if (new URL(item.link).pathname.startsWith('/upload')) { + // 链接为一个文件,直接返回链接 + newItem.description = item.link; + } else { + const response = await ofetch(item.link); + const $ = load(response); + newItem.description = $('div.news1content').html() || ''; + } + } else { + // 涉及到其他站点,不方便做统一的 html 解析,直接返回链接 + newItem.description = item.link; + } + return newItem; + }) + ) + ); + + return { + title: $('li#classname').text() + ' - 浙江工业大学计算机科学与技术学院、软件学院', + link: rootUrl + type, + item: items, + } as Data; +}