Skip to content

Commit ea87eb8

Browse files
authored
fix(route): 西南石油大学 办公网、教务处、计算机与软件学院、电气信息学院 (DIYgod#15621)
* fix: 西南石油大学 办公网 * fix: 西南石油大学 计算机与软件学院、电气信息学院、教务处 * 西南石油大学 办公网:移除 学术报告(网页改版) * use `map()` * use `.html()!` instead of `.html()?.toString()`
1 parent 34e1e75 commit ea87eb8

File tree

4 files changed

+113
-121
lines changed

4 files changed

+113
-121
lines changed

lib/routes/swpu/bgw.ts

+31-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Route } from '@/types';
1+
import { DataItem, Route, Data } from '@/types';
22
import cache from '@/utils/cache';
33
import { joinUrl } from './utils';
44
import { parseDate } from '@/utils/parse-date';
@@ -29,53 +29,51 @@ export const route: Route = {
2929
maintainers: ['CYTMWIA'],
3030
handler,
3131
url: 'swpu.edu.cn/',
32-
description: `| 栏目 | 重要通知公告 | 部门通知公告 | 本周活动 | 学术报告 |
33-
| ---- | ------------ | ------------ | -------- | -------- |
34-
| 代码 | zytzgg | bmtzgg | bzhd | xsbg |`,
32+
description: `| 栏目 | 重要通知公告 | 部门通知公告 | 本周活动 |
33+
| ---- | ------------ | ------------ | -------- |
34+
| 代码 | zytzgg | bmtzgg | bzhd |`,
3535
};
3636

37-
async function handler(ctx) {
38-
const url = `https://www.swpu.edu.cn/bgw2/${ctx.req.param('code')}.htm`;
37+
async function handler(ctx): Promise<Data> {
38+
const url = `https://www.swpu.edu.cn/bgw/${ctx.req.param('code')}.htm`;
3939

4040
const res = await got.get(url);
4141
const $ = load(res.data);
4242

4343
const title = $('.title').text();
4444

4545
// 获取标题、时间及链接
46-
const items = [];
47-
$('.notice > ul > li > a').each((i, elem) => {
48-
items.push({
46+
const items: DataItem[] = $('.notice > ul > li > a')
47+
.toArray()
48+
.map((elem) => ({
4949
title: $(elem.children[0]).text(),
5050
pubDate: timezone(parseDate($(elem.children[1]).text()), +8),
5151
link: joinUrl('https://www.swpu.edu.cn', $(elem).attr('href')), // 实际获得连接 "../info/1312/17891.htm"
52-
});
53-
});
52+
}));
5453

5554
// 请求全文
56-
const out = await Promise.all(
57-
items.map(async (item) => {
58-
const $ = await cache.tryGet(item.link, async () => {
59-
const res = await got.get(item.link);
60-
return load(res.data);
61-
});
62-
63-
if ($('title').text().startsWith('系统提示')) {
64-
item.author = '系统';
65-
item.description = '无权访问';
66-
} else {
67-
item.author = '办公网';
68-
item.description = $('.v_news_content').html();
69-
for (const elem of $('.v_news_content p')) {
70-
if ($(elem).css('text-align') === 'right') {
71-
item.author = $(elem).text();
72-
break;
55+
const out: DataItem[] = await Promise.all(
56+
items.map(
57+
async (item: DataItem) =>
58+
(await cache.tryGet(item.link!, async () => {
59+
const resp = await got.get(item.link);
60+
const $ = load(resp.data);
61+
if ($('title').text().startsWith('系统提示')) {
62+
item.author = '系统';
63+
item.description = '无权访问';
64+
} else {
65+
item.author = '办公网';
66+
item.description = $('.v_news_content').html()!;
67+
for (const elem of $('.v_news_content p')) {
68+
if ($(elem).css('text-align') === 'right') {
69+
item.author = $(elem).text();
70+
break;
71+
}
72+
}
7373
}
74-
}
75-
}
76-
77-
return item;
78-
})
74+
return item;
75+
})) as DataItem
76+
)
7977
);
8078

8179
return {

lib/routes/swpu/dean.ts

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Route } from '@/types';
1+
import { DataItem, Route, Data } from '@/types';
22
import cache from '@/utils/cache';
33
import { joinUrl } from './utils';
44
import { parseDate } from '@/utils/parse-date';
@@ -34,7 +34,7 @@ export const route: Route = {
3434
| 代码 | tzgg | xwbd | sdsy |`,
3535
};
3636

37-
async function handler(ctx) {
37+
async function handler(ctx): Promise<Data> {
3838
const url = `https://www.swpu.edu.cn/dean/${ctx.req.param('code')}.htm`;
3939

4040
const res = await got.get(url);
@@ -44,39 +44,37 @@ async function handler(ctx) {
4444
title = title.substring(title.indexOf(':') + 1);
4545

4646
// 获取标题、时间及链接
47-
const items = [];
48-
$('.r_list > ul > li').each((i, elem) => {
49-
items.push({
47+
const items: DataItem[] = $('.r_list > ul > li')
48+
.toArray()
49+
.map((elem) => ({
5050
title: $('label:eq(0)', elem).text().trim(),
5151
link: joinUrl('https://www.swpu.edu.cn/dean/', $('a', elem).attr('href')),
52-
});
53-
});
52+
}));
5453

5554
// 请求全文
5655
const out = await Promise.all(
57-
items.map(async (item) => {
58-
const $ = await cache.tryGet(item.link, async () => {
59-
const res = await got.get(item.link);
60-
return load(res.data);
61-
});
62-
63-
if ($('title').text().startsWith('系统提示')) {
64-
item.author = '系统';
65-
item.description = '无权访问';
66-
} else {
67-
item.author = '教务处';
68-
item.description = $('.v_news_content').html();
69-
item.pubDate = timezone(parseDate($('#lbDate').text(), '更新时间:YYYY年MM月DD日'), +8);
70-
for (const elem of $('.v_news_content p')) {
71-
if ($(elem).css('text-align') === 'right') {
72-
item.author = $(elem).text();
73-
break;
56+
items.map(
57+
async (item) =>
58+
(await cache.tryGet(item.link!, async () => {
59+
const resp = await got.get(item.link);
60+
const $ = load(resp.data);
61+
if ($('title').text().startsWith('系统提示')) {
62+
item.author = '系统';
63+
item.description = '无权访问';
64+
} else {
65+
item.author = '教务处';
66+
item.description = $('.v_news_content').html()!;
67+
item.pubDate = timezone(parseDate($('#lbDate').text(), '更新时间:YYYY年MM月DD日'), +8);
68+
for (const elem of $('.v_news_content p')) {
69+
if ($(elem).css('text-align') === 'right') {
70+
item.author = $(elem).text();
71+
break;
72+
}
73+
}
7474
}
75-
}
76-
}
77-
78-
return item;
79-
})
75+
return item;
76+
})) as DataItem
77+
)
8078
);
8179

8280
return {

lib/routes/swpu/dxy.ts

+26-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Route } from '@/types';
1+
import { DataItem, Route, Data } from '@/types';
22
import cache from '@/utils/cache';
33
import { joinUrl } from './utils';
44
import { parseDate } from '@/utils/parse-date';
@@ -34,7 +34,7 @@ export const route: Route = {
3434
| 代码 | 1122 | 1156 |`,
3535
};
3636

37-
async function handler(ctx) {
37+
async function handler(ctx): Promise<Data> {
3838
// 移除 urltype=tree.TreeTempUrl 虽然也能顺利访问页面,
3939
// 但标题会缺失,而且在其他地方定位提取标题也比较麻烦。
4040
const url = `https://www.swpu.edu.cn/dxy/list1.jsp?urltype=tree.TreeTempUrl&wbtreeid=${ctx.req.param('code')}`;
@@ -46,39 +46,37 @@ async function handler(ctx) {
4646
title = title.substring(0, title.indexOf('-'));
4747

4848
// 获取标题、时间及链接
49-
const items = [];
50-
$('tr[height="20"]').each((i, elem) => {
51-
items.push({
49+
const items: DataItem[] = $('tr[height="20"]')
50+
.toArray()
51+
.map((elem) => ({
5252
title: $('a[title]', elem).text().trim(),
5353
pubDate: timezone(parseDate($('td:eq(1)', elem).text(), 'YYYY年MM月DD日'), +8),
5454
link: joinUrl('https://www.swpu.edu.cn/dxy/', $('a[title]', elem).attr('href')),
55-
});
56-
});
55+
}));
5756

5857
// 请求全文
5958
const out = await Promise.all(
60-
items.map(async (item) => {
61-
const $ = await cache.tryGet(item.link, async () => {
62-
const res = await got.get(item.link);
63-
return load(res.data);
64-
});
65-
66-
if ($('title').text().startsWith('系统提示')) {
67-
item.author = '系统';
68-
item.description = '无权访问';
69-
} else {
70-
item.author = '电气信息学院';
71-
item.description = $('.v_news_content').html();
72-
for (const elem of $('.v_news_content p')) {
73-
if ($(elem).css('text-align') === 'right') {
74-
item.author = $(elem).text();
75-
break;
59+
items.map(
60+
async (item) =>
61+
(await cache.tryGet(item.link!, async () => {
62+
const resp = await got.get(item.link);
63+
const $ = load(resp.data);
64+
if ($('title').text().startsWith('系统提示')) {
65+
item.author = '系统';
66+
item.description = '无权访问';
67+
} else {
68+
item.author = '电气信息学院';
69+
item.description = $('.v_news_content').html()!;
70+
for (const elem of $('.v_news_content p')) {
71+
if ($(elem).css('text-align') === 'right') {
72+
item.author = $(elem).text();
73+
break;
74+
}
75+
}
7676
}
77-
}
78-
}
79-
80-
return item;
81-
})
77+
return item;
78+
})) as DataItem
79+
)
8280
);
8381

8482
return {

lib/routes/swpu/scs.ts

+29-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Route } from '@/types';
1+
import { DataItem, Route, Data } from '@/types';
22
import cache from '@/utils/cache';
33
import { joinUrl } from './utils';
44
import { parseDate } from '@/utils/parse-date';
@@ -25,7 +25,7 @@ export const route: Route = {
2525
target: '',
2626
},
2727
],
28-
name: '计算机科学学院',
28+
name: '计算机与软件学院',
2929
maintainers: ['CYTMWIA'],
3030
handler,
3131
url: 'swpu.edu.cn/',
@@ -34,7 +34,7 @@ export const route: Route = {
3434
| 代码 | tzgg | xwsd |`,
3535
};
3636

37-
async function handler(ctx) {
37+
async function handler(ctx): Promise<Data> {
3838
const url = `https://www.swpu.edu.cn/scs/index/${ctx.req.param('code')}.htm`;
3939

4040
const res = await got.get(url);
@@ -43,45 +43,43 @@ async function handler(ctx) {
4343
const title = $('.r_list > h3').text();
4444

4545
// 获取标题、时间及链接
46-
const items = [];
47-
$('.main_conRCb > ul > li').each((i, elem) => {
48-
items.push({
46+
const items: DataItem[] = $('.main_conRCb > ul > li')
47+
.toArray()
48+
.map((elem) => ({
4949
title: $('em', elem).text().trim(),
5050
pubDate: timezone(parseDate($('span', elem).text()), +8),
5151
link: joinUrl('https://www.swpu.edu.cn/scs/index/', $('a', elem).attr('href')),
52-
});
53-
});
52+
}));
5453

5554
// 请求全文
5655
const out = await Promise.all(
57-
items.map(async (item) => {
58-
const $ = await cache.tryGet(item.link, async () => {
59-
const res = await got.get(item.link);
60-
return load(res.data);
61-
});
62-
63-
if ($('title').text().startsWith('系统提示')) {
64-
item.author = '系统';
65-
item.description = '无权访问';
66-
} else {
67-
item.author = '计算机科学学院';
68-
item.description = $('.v_news_content').html();
69-
for (const elem of $('.v_news_content p')) {
70-
if ($(elem).css('text-align') === 'right') {
71-
item.author = $(elem).text();
72-
break;
56+
items.map(
57+
async (item) =>
58+
(await cache.tryGet(item.link!, async () => {
59+
const resp = await got.get(item.link);
60+
const $ = load(resp.data);
61+
if ($('title').text().startsWith('系统提示')) {
62+
item.author = '系统';
63+
item.description = '无权访问';
64+
} else {
65+
item.author = '计算机与软件学院';
66+
item.description = $('.v_news_content').html()!;
67+
for (const elem of $('.v_news_content p')) {
68+
if ($(elem).css('text-align') === 'right') {
69+
item.author = $(elem).text();
70+
break;
71+
}
72+
}
7373
}
74-
}
75-
}
76-
77-
return item;
78-
})
74+
return item;
75+
})) as DataItem
76+
)
7977
);
8078

8179
return {
82-
title: `西南石油大学计算机科学学院 ${title}`,
80+
title: `西南石油大学计算机与软件学院 ${title}`,
8381
link: url,
84-
description: `西南石油大学计算机科学学院 ${title}`,
82+
description: `西南石油大学计算机与软件学院 ${title}`,
8583
language: 'zh-CN',
8684
item: out,
8785
};

0 commit comments

Comments
 (0)