|
| 1 | +import { DataItem, Route } from '@/types'; |
| 2 | +import cache from '@/utils/cache'; |
| 3 | +import got from '@/utils/got'; |
| 4 | +import { parseDate } from '@/utils/parse-date'; |
| 5 | +import { load } from 'cheerio'; |
| 6 | + |
| 7 | +const BASE_URL = 'https://www.xbmu.edu.cn/xwzx/tzgg.htm'; |
| 8 | + |
| 9 | +const handler: Route['handler'] = async () => { |
| 10 | + try { |
| 11 | + // Fetch the announcements page |
| 12 | + const { data: listResponse } = await got(BASE_URL); |
| 13 | + const $ = load(listResponse); |
| 14 | + |
| 15 | + // Select all list items containing announcement information |
| 16 | + const ITEM_SELECTOR = 'body > div.container.list-container.ny_mani > div > div.news_list > ul > li'; |
| 17 | + const listItems = $(ITEM_SELECTOR); |
| 18 | + |
| 19 | + // Map through each list item to extract details |
| 20 | + const announcementLinkList = await Promise.all( |
| 21 | + listItems.toArray().map((element) => { |
| 22 | + const rawDate = $(element).find('span').text().trim(); |
| 23 | + const [day, yearMonth] = rawDate.split('/').map((s) => s.trim()); |
| 24 | + const formattedDate = parseDate(`${yearMonth}-${day}`).toUTCString(); |
| 25 | + |
| 26 | + const title = $(element).find('a').attr('title') || '通知公告'; |
| 27 | + const relativeHref = $(element).find('a').attr('href') || ''; |
| 28 | + const link = `https://www.xbmu.edu.cn/${relativeHref.replaceAll('../', '')}`; |
| 29 | + |
| 30 | + return { |
| 31 | + date: formattedDate, |
| 32 | + title, |
| 33 | + link, |
| 34 | + }; |
| 35 | + }) |
| 36 | + ); |
| 37 | + |
| 38 | + return { |
| 39 | + title: '西北民族大学通知公告', |
| 40 | + description: '西北民族大学近日通知公告', |
| 41 | + link: BASE_URL, |
| 42 | + image: 'http://210.26.0.114:9090/mdxg/img/weex/default_img.jpg', |
| 43 | + item: (await Promise.all( |
| 44 | + announcementLinkList.map((item) => |
| 45 | + cache.tryGet(item.link, async () => { |
| 46 | + const CONTENT_SELECTOR = '#vsb_content > div'; |
| 47 | + const { data: contentResponse } = await got(item.link); |
| 48 | + const contentPage = load(contentResponse); |
| 49 | + const content = contentPage(CONTENT_SELECTOR).html() || ''; |
| 50 | + return { |
| 51 | + title: item.title, |
| 52 | + pubDate: item.date, |
| 53 | + link: item.link, |
| 54 | + description: content, |
| 55 | + category: ['university'], |
| 56 | + guid: item.link, |
| 57 | + id: item.link, |
| 58 | + image: 'http://210.26.0.114:9090/mdxg/img/weex/default_img.jpg', |
| 59 | + content, |
| 60 | + updated: item.date, |
| 61 | + language: 'zh-cn', |
| 62 | + }; |
| 63 | + }) |
| 64 | + ) |
| 65 | + )) as DataItem[], |
| 66 | + allowEmpty: true, |
| 67 | + language: 'zh-cn', |
| 68 | + feedLink: 'https://rsshub.app/xbmu/announcement', |
| 69 | + id: 'https://rsshub.app/xbmu/announcement', |
| 70 | + }; |
| 71 | + } catch (error) { |
| 72 | + throw new Error(`Error fetching announcements: ${error}`); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +export const route: Route = { |
| 77 | + path: '/announcement', |
| 78 | + name: '通知公告', |
| 79 | + maintainers: ['PrinOrange'], |
| 80 | + handler, |
| 81 | + categories: ['university'], |
| 82 | + features: { |
| 83 | + requireConfig: false, |
| 84 | + requirePuppeteer: false, |
| 85 | + antiCrawler: false, |
| 86 | + supportBT: false, |
| 87 | + supportPodcast: false, |
| 88 | + supportScihub: false, |
| 89 | + }, |
| 90 | + example: '/xbmu/announcement', |
| 91 | +}; |
0 commit comments