/
yjs.ts
60 lines (58 loc) 路 1.74 KB
/
yjs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
const xmut = 'https://yjs.xmut.edu.cn';
export const route: Route = {
path: '/jwc/yjjw/:category?',
name: 'Unknown',
maintainers: [],
handler,
};
async function handler(ctx) {
const { category = 'yjsc' } = ctx.req.param();
const url = `${xmut}/index/${category}.htm`;
const res = await got(url, {
headers: {
referer: xmut,
},
https: {
rejectUnauthorized: false,
},
});
const $ = load(res.data);
const items = $('.mainWrap .main_con .main_conR ul li')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('em').text(),
link: `${xmut}/` + item.find('a').attr('href'),
pubDate: parseDate(item.find('span').text()),
};
});
const itemPromises = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link, {
headers: {
referer: xmut,
},
https: {
rejectUnauthorized: false,
},
});
const $item = load(detailResponse.data);
const content = $item('body .mainWrap .main_content .v_news_content').html();
item.description = content;
return item;
})
)
);
return {
title: $('title').text(),
link: url,
item: itemPromises,
};
}