|
| 1 | +import { Route } from '@/types'; |
| 2 | +import cache from '@/utils/cache'; |
| 3 | +import ofetch from '@/utils/ofetch'; |
| 4 | +import { load } from 'cheerio'; |
| 5 | +import { parseDate } from '@/utils/parse-date'; |
| 6 | +import timezone from '@/utils/timezone'; |
| 7 | + |
| 8 | +export const route: Route = { |
| 9 | + path: '/index/tzgg', |
| 10 | + categories: ['university'], |
| 11 | + example: '/xyu/index/tzgg', |
| 12 | + features: { |
| 13 | + requireConfig: false, |
| 14 | + requirePuppeteer: false, |
| 15 | + antiCrawler: false, |
| 16 | + supportBT: false, |
| 17 | + supportPodcast: false, |
| 18 | + supportScihub: false, |
| 19 | + }, |
| 20 | + radar: [ |
| 21 | + { |
| 22 | + source: ['www.xyc.edu.cn/index/tzgg.htm'], |
| 23 | + }, |
| 24 | + ], |
| 25 | + name: '官网通知公告', |
| 26 | + maintainers: ['JinMokai'], |
| 27 | + handler, |
| 28 | + url: 'www.xyc.edu.cn/index/tzgg.htm', |
| 29 | +}; |
| 30 | + |
| 31 | +async function handler() { |
| 32 | + const baseUrl = 'https://www.xyc.edu.cn'; |
| 33 | + const url = `${baseUrl}/index/tzgg.htm`; |
| 34 | + |
| 35 | + const response = await ofetch(url); |
| 36 | + if (!response) { |
| 37 | + return { |
| 38 | + title: '新余学院 - 通知公告', |
| 39 | + link: url, |
| 40 | + item: [], |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + const $ = load(response); |
| 45 | + |
| 46 | + const list = $('.text-list ul li') |
| 47 | + .toArray() |
| 48 | + .map((item) => { |
| 49 | + const currentItem = $(item); |
| 50 | + const link = currentItem.find('a').attr('href'); |
| 51 | + if (!link) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const title = currentItem.find('.list-tx h3').text().trim(); |
| 56 | + const description = currentItem.find('.list-tx p').text().trim(); |
| 57 | + const day = currentItem.find('.date p').text().trim(); |
| 58 | + const yearMonth = currentItem.find('.date span').text().trim(); |
| 59 | + const dateText = `${yearMonth}-${day.padStart(2, '0')}`; |
| 60 | + |
| 61 | + return { |
| 62 | + title, |
| 63 | + link: new URL(link, baseUrl).href, |
| 64 | + description: description || title, |
| 65 | + pubDate: timezone(parseDate(dateText, 'YYYY-MM-DD'), +8), |
| 66 | + }; |
| 67 | + }) |
| 68 | + .filter(Boolean); |
| 69 | + |
| 70 | + const items = await Promise.all( |
| 71 | + list.map((item) => |
| 72 | + cache.tryGet(item?.link || '', async () => { |
| 73 | + if (!item) { |
| 74 | + return ''; |
| 75 | + } |
| 76 | + try { |
| 77 | + const detailResponse = await ofetch(item?.link); |
| 78 | + if (!detailResponse) { |
| 79 | + return { |
| 80 | + ...item, |
| 81 | + description: '该通知无法直接预览,请点击原文链接查看', |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + const $$ = load(detailResponse); |
| 86 | + const content = $$('.v_news_content, .content, .article-content').html(); |
| 87 | + |
| 88 | + if (content) { |
| 89 | + const $content = load(content); |
| 90 | + $content('a').each(function () { |
| 91 | + const a = $(this); |
| 92 | + const href = a.attr('href'); |
| 93 | + if (href && !href.startsWith('http')) { |
| 94 | + a.attr('href', new URL(href, baseUrl).href); |
| 95 | + } |
| 96 | + }); |
| 97 | + item.description = $content.html(); |
| 98 | + } |
| 99 | + |
| 100 | + return item; |
| 101 | + } catch { |
| 102 | + return { |
| 103 | + ...item, |
| 104 | + description: '该通知无法直接预览,请点击原文链接查看', |
| 105 | + }; |
| 106 | + } |
| 107 | + }) |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + return { |
| 112 | + title: '新余学院 - 通知公告', |
| 113 | + link: url, |
| 114 | + item: items, |
| 115 | + }; |
| 116 | +} |
0 commit comments