Skip to content

Commit 249381f

Browse files
authored
fix(route): Remove white spaces from the description (#19638)
1 parent 281b51e commit 249381f

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

lib/routes/sicau/jiaowu.ts

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import { load } from 'cheerio';
44
import cache from '@/utils/cache';
55
import { parseDate } from '@/utils/parse-date';
66
import timezone from '@/utils/timezone';
7+
import { Language } from '@/routes/kurogames/wutheringwaves/constants';
78

89
const $get = async (url: string, encoding = 'gb2312') => new TextDecoder(encoding).decode(await ofetch(url, { responseType: 'arrayBuffer' }));
10+
const $trim = (str: string) => {
11+
let s = str.trim();
12+
s = s.startsWith('  ') ? s.slice(12) : s;
13+
s = s.endsWith('<br>') ? s.slice(0, Math.max(0, s.length - 4)) : s;
14+
return s.trim();
15+
};
916

1017
export const route: Route = {
11-
path: '/jiaowu/jxtz',
18+
path: '/jiaowu/jxtz/:detail?',
1219
categories: ['university'],
13-
example: '/sicau/jiaowu/jxtz',
20+
example: '/sicau/jiaowu/jxtz/detail',
21+
parameters: { detail: '是否抓取全文,该值只要不为空就抓取全文返回,否则只返回标题' },
1422
features: {
1523
requireConfig: false,
1624
requirePuppeteer: false,
@@ -27,51 +35,51 @@ export const route: Route = {
2735
],
2836
name: '教务处',
2937
maintainers: ['hualiong'],
38+
description: `
39+
::: tip
40+
抓取全文返回会导致更长的响应时间,可以尝试使用 \`/sicau/jiaowu/jxtz\` 路径,这将只返回标题,然后再在应用内抓取全文内容。
41+
:::
42+
`,
3043
url: 'jiaowu.sicau.edu.cn/',
31-
handler: async () => {
44+
handler: async (ctx) => {
3245
const baseUrl = 'https://jiaowu.sicau.edu.cn/web/web/web';
46+
const { detail = null } = ctx.req.param();
3347

34-
const response = await $get(`${baseUrl}/index.asp`);
48+
const response = await $get(`${baseUrl}/gwmore.asp`);
3549
const $ = load(response);
3650

37-
const list = $('ul.notice1:nth-child(1) a')
51+
let items = $('tbody > .text-c:nth-child(-n+10)')
3852
.toArray()
3953
.map((item) => {
40-
const a = $(item);
41-
const href = a.attr('href')!;
54+
const children = $(item).children();
55+
const a = children.eq(2).find('a');
4256
return {
43-
link: `${baseUrl}/${href.slice(href.lastIndexOf('/') + 1)}`,
57+
category: [children.eq(1).text()],
58+
link: `${baseUrl}/${a.attr('href')!}`,
59+
title: a.children().first().text(),
60+
pubDate: timezone(parseDate(children.eq(3).text(), 'YYYY-M-D'), +8),
61+
author: children.eq(4).text(),
62+
description: '请在应用内抓取全文内容',
4463
} as DataItem;
4564
});
4665

47-
const items = await Promise.all(
48-
list.map((item) =>
49-
cache.tryGet(item.link!, async () => {
50-
const response = await $get(item.link!);
51-
const $ = load(response);
52-
53-
item.title = $('body > .page-title-2').text();
54-
55-
const date = $('body > p.page-title-3').text();
56-
item.pubDate = timezone(parseDate(date.match(/(\d{4}(?:-\d{1,2}){2})/)![0], 'YYYY-M-D'), +8);
57-
58-
const str = $('.text1[valign="bottom"]').text();
59-
const match = str.match(/(.+?)\[(.+?)]/)!;
60-
item.author = match[1];
61-
item.category = [match[2]];
62-
63-
item.description = $('.text1[width="95%"]').html()!;
64-
65-
return item;
66-
})
67-
)
68-
);
66+
if (detail) {
67+
items = await Promise.all(
68+
items.map((item) =>
69+
cache.tryGet(item.link!, async () => {
70+
const $ = load(await $get(item.link!));
71+
item.description = $trim($('.text1[width="95%"] b').html()!);
72+
return item;
73+
})
74+
)
75+
);
76+
}
6977

7078
return {
71-
title: '教学通知 - 川农教务处',
72-
link: 'https://jiaowu.sicau.edu.cn/web/web/web/index.asp',
73-
language: 'zh-cn',
74-
item: items as DataItem[],
79+
title: '教学通知 - 四川农业大学教务处',
80+
link: 'https://jiaowu.sicau.edu.cn/web/web/web/gwmore.asp',
81+
language: Language.Chinese,
82+
item: items,
7583
};
7684
},
7785
};

0 commit comments

Comments
 (0)