|
| 1 | +import type { Data, DataItem, Route } from '@/types'; |
| 2 | +import ofetch from '@/utils/ofetch'; |
| 3 | +import { load } from 'cheerio'; |
| 4 | +import { parseDate } from '@/utils/parse-date'; |
| 5 | +import cache from '@/utils/cache'; |
| 6 | +import logger from '@/utils/logger'; |
| 7 | +import timezone from '@/utils/timezone'; |
| 8 | + |
| 9 | +export const route: Route = { |
| 10 | + path: '/yz/:category?', |
| 11 | + categories: ['university'], |
| 12 | + example: '/xjtu/yz/zsdt', |
| 13 | + parameters: { category: '栏目类型,默认请求`zsdt`,详见下方表格' }, |
| 14 | + features: { |
| 15 | + requireConfig: false, |
| 16 | + requirePuppeteer: false, |
| 17 | + antiCrawler: false, |
| 18 | + supportBT: false, |
| 19 | + supportPodcast: false, |
| 20 | + supportScihub: false, |
| 21 | + }, |
| 22 | + radar: [ |
| 23 | + { |
| 24 | + source: ['yz.xjtu.edu.cn/index/:category.htm'], |
| 25 | + target: '/yz/:category', |
| 26 | + }, |
| 27 | + ], |
| 28 | + name: '研究生招生信息网', |
| 29 | + maintainers: ['YoghurtGuy'], |
| 30 | + handler, |
| 31 | + description: `栏目类型 |
| 32 | +
|
| 33 | +| 招生动态 | 通知公告 | 政策法规 | 招生统计 | 历年复试线 | 博士招生 | 硕士招生 | 推免生 | 其他招生 | |
| 34 | +| -------- | -------- | -------- | -------- | ---------- | -------- | -------- | ------ | -------- | |
| 35 | +| zsdt | tzgg | zcfg | zstj | lnfsx | bszs | sszs | tms | qtzs |`, |
| 36 | +}; |
| 37 | +async function handler(ctx) { |
| 38 | + const { category = 'zsdt' } = ctx.req.param(); |
| 39 | + const baseUrl = 'https://yz.xjtu.edu.cn'; |
| 40 | + |
| 41 | + const response = await ofetch(`${baseUrl}/index/${category}.htm`); |
| 42 | + const $ = load(response); |
| 43 | + const list = $('div.list-con ul li') |
| 44 | + .toArray() |
| 45 | + .map((item_) => { |
| 46 | + const item = $(item_); |
| 47 | + const a = item.find('a'); |
| 48 | + return { |
| 49 | + title: a.attr('title'), |
| 50 | + link: new URL(a.attr('href')!, baseUrl).href, |
| 51 | + pubDate: timezone(parseDate(item.find('span.date.fr').text()), +8), |
| 52 | + } as DataItem; |
| 53 | + }); |
| 54 | + const items = await Promise.all( |
| 55 | + list.map((item) => |
| 56 | + cache.tryGet(item.link!, async () => { |
| 57 | + try { |
| 58 | + const res = await ofetch(item.link!); |
| 59 | + const content = load(res); |
| 60 | + item.description = content('#vsb_content').html()! + (content('form ul').length > 0 ? content('form ul').html() : ''); |
| 61 | + return item; |
| 62 | + } catch (error) { |
| 63 | + logger.error(`Fetch failed for ${item.link}:`, error); |
| 64 | + return item; |
| 65 | + } |
| 66 | + }) |
| 67 | + ) |
| 68 | + ); |
| 69 | + return { |
| 70 | + title: '西安交通大学研究生招生信息网', |
| 71 | + link: 'https://yz.xjtu.edu.cn', |
| 72 | + item: items, |
| 73 | + } as Data; |
| 74 | +} |
0 commit comments