Skip to content

Commit ee266b0

Browse files
JinMokaiTonyRL
andauthored
feat(route): 新增新余学院官网通知及图书馆通知路由 (#20051)
* feat(route): 新余学院官网通知及图书馆通知路由 * fix: remove router * refactor(xyu): Remove paging processing logic * fix: eslint error fix --------- Co-authored-by: Tony <[email protected]>
1 parent e196e2a commit ee266b0

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

lib/routes/xyu/library.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Route } from '@/types';
2+
import ofetch from '@/utils/ofetch';
3+
import { load } from 'cheerio';
4+
import { parseDate } from '@/utils/parse-date';
5+
6+
export const route: Route = {
7+
path: '/library',
8+
categories: ['university'],
9+
example: '/xyu/library',
10+
features: {
11+
requireConfig: false,
12+
requirePuppeteer: false,
13+
antiCrawler: false,
14+
supportBT: false,
15+
supportPodcast: false,
16+
supportScihub: false,
17+
},
18+
name: '图书馆通知公告',
19+
maintainers: ['JinMokai'],
20+
handler,
21+
url: 'lib.xyc.edu.cn/index/tzgg.htm',
22+
radar: [
23+
{
24+
source: ['lib.xyc.edu.cn/index/tzgg.htm'],
25+
target: '/library',
26+
},
27+
],
28+
};
29+
30+
async function handler() {
31+
const baseUrl = 'https://lib.xyc.edu.cn';
32+
const url = `${baseUrl}/index/tzgg.htm`;
33+
34+
const response = await ofetch(url);
35+
const $ = load(response);
36+
37+
const items = $('.text-list ul li')
38+
.toArray()
39+
.map((item) => {
40+
const $item = $(item);
41+
const $link = $item.find('a');
42+
const title = $link.attr('title') || $link.text().trim();
43+
const relativeUrl = $link.attr('href');
44+
const link = relativeUrl ? new URL(relativeUrl, baseUrl).href : '';
45+
// 提取日期
46+
const dateText = $item.find('.date').text().trim() || $item.text().match(/\d{4}-\d{2}-\d{2}/)?.[0] || '';
47+
const pubDate = parseDate(dateText, 'YYYY-MM-DD');
48+
49+
return {
50+
title,
51+
link,
52+
pubDate,
53+
description: title,
54+
};
55+
})
56+
.filter((item): item is NonNullable<typeof item> => Boolean(item.title && item.link));
57+
58+
return {
59+
title: '新余学院图书馆通知公告',
60+
link: url,
61+
item: items,
62+
};
63+
}

lib/routes/xyu/namespace.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Namespace } from '@/types';
2+
3+
export const namespace: Namespace = {
4+
name: '新余学院',
5+
url: 'xyc.edu.cn',
6+
lang: 'zh-CN',
7+
};

lib/routes/xyu/notices.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)