|
| 1 | +import { load } from 'cheerio'; |
| 2 | + |
| 3 | +import type { Route } from '@/types'; |
| 4 | +import got from '@/utils/got'; |
| 5 | + |
| 6 | +export const route: Route = { |
| 7 | + path: '/sxy/:type', |
| 8 | + categories: ['university'], |
| 9 | + example: '/zzu/sxy/xyxw', |
| 10 | + parameters: { type: '分类名' }, |
| 11 | + features: { |
| 12 | + requireConfig: false, |
| 13 | + requirePuppeteer: false, |
| 14 | + antiCrawler: false, |
| 15 | + supportBT: false, |
| 16 | + supportPodcast: false, |
| 17 | + supportScihub: false, |
| 18 | + }, |
| 19 | + radar: [ |
| 20 | + { |
| 21 | + source: ['www5.zzu.edu.cn/sxy/'], |
| 22 | + }, |
| 23 | + ], |
| 24 | + name: '郑大商学院', |
| 25 | + maintainers: ['amandus1990'], |
| 26 | + handler, |
| 27 | + description: `| 学院新闻 | 通知公告 | 教学科研 | 党工团学 | 讲座报告 | 学者观点 | |
| 28 | +| -------- | -------- | -------- | -------- | -------- | -------- | |
| 29 | +| xyxw | tzgg | jxky | dgtx | jzbg | xzgd |`, |
| 30 | +}; |
| 31 | + |
| 32 | +async function handler(ctx) { |
| 33 | + const type = ctx.req.param('type'); |
| 34 | + const typeDict = { |
| 35 | + xyxw: ['学院新闻', 'https://www5.zzu.edu.cn/sxy/index/xyxw.htm'], |
| 36 | + tzgg: ['通知公告', 'https://www5.zzu.edu.cn/sxy/index/tzgg.htm'], |
| 37 | + jxky: ['教学科研', 'https://www5.zzu.edu.cn/sxy/index/jxky.htm'], |
| 38 | + dgtx: ['党工团学', 'https://www5.zzu.edu.cn/sxy/index/dgtx.htm'], |
| 39 | + jzbg: ['讲座报告', 'https://www5.zzu.edu.cn/sxy/index/jzbg.htm'], |
| 40 | + xzgd: ['学者观点', 'https://www5.zzu.edu.cn/sxy/index/xzgd.htm'], |
| 41 | + }; |
| 42 | + |
| 43 | + // 获取页面内容 |
| 44 | + const response = await got(typeDict[type][1]); |
| 45 | + const $ = load(response.data); |
| 46 | + |
| 47 | + const list = type === 'xyxw' ? parseXyxwList($, typeDict, type) : parseOtherList($, typeDict, type); |
| 48 | + |
| 49 | + return { |
| 50 | + title: `郑大商学院-${typeDict[type][0]}`, |
| 51 | + link: typeDict[type][1], |
| 52 | + item: list, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function parseXyxwList($, typeDict, type) { |
| 57 | + return $('section.n_titu ul li') |
| 58 | + .toArray() |
| 59 | + .slice(0, 6) |
| 60 | + .map((element) => { |
| 61 | + const $element = $(element); |
| 62 | + const $link = $element.find('a').first(); |
| 63 | + const link = new URL($link.attr('href'), typeDict[type][1]).href; |
| 64 | + const title = $link.attr('title') || $link.text().trim(); |
| 65 | + const description = $element.find('.right .con p').text().trim(); |
| 66 | + |
| 67 | + const monthDay = $element.find('.time_con h3').text().trim(); |
| 68 | + const year = $element.find('.time_con h6').text().trim(); |
| 69 | + const pubDateText = `${year}-${monthDay}`; |
| 70 | + |
| 71 | + return { |
| 72 | + title, |
| 73 | + link, |
| 74 | + pubDate: pubDateText, |
| 75 | + description, |
| 76 | + }; |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +function parseOtherList($, typeDict, type) { |
| 81 | + return $('.n_notice ul.ul li') |
| 82 | + .toArray() |
| 83 | + .slice(0, 16) |
| 84 | + .map((element) => { |
| 85 | + const $element = $(element); |
| 86 | + const $link = $element.find('a').first(); |
| 87 | + const link = new URL($link.attr('href'), typeDict[type][1]).href; |
| 88 | + const title = $link.attr('title'); |
| 89 | + |
| 90 | + const pubDateText = $element.find('span.span01').text().trim(); |
| 91 | + |
| 92 | + return { |
| 93 | + title, |
| 94 | + link, |
| 95 | + pubDate: pubDateText || null, |
| 96 | + }; |
| 97 | + }); |
| 98 | +} |
0 commit comments