-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
xwxy-news.ts
96 lines (87 loc) · 2.86 KB
/
xwxy-news.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Route } from '@/types';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/xwxy-news',
categories: ['university'],
example: '/gdufs/xwxy-news',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['xwxy.gdufs.edu.cn/xwzx/xyxw', 'xwxy.gdufs.edu.cn/'],
},
],
name: '新闻学院-学院新闻',
maintainers: ['gz4zzxc'],
handler,
url: 'xwxy.gdufs.edu.cn/xwzx/xyxw',
};
async function handler() {
const BASE_URL = 'https://xwxy.gdufs.edu.cn';
const link = `${BASE_URL}/xwzx/xyxw.htm`;
// 获取列表页面
const response = await got(link);
if (!response.body) {
throw new Error('No response body');
}
const $ = load(response.body);
const list = $('div.flex-center a.clearfix');
const items = list.toArray().map((element) => {
const item = $(element);
const href = item.attr('href') || '';
const dateText = item.find('i').text().trim();
const pubDate = parseDate(dateText).toUTCString();
return {
title: item.find('h5').text().trim(),
link: href.startsWith('http') ? href : new URL(href, BASE_URL).href,
pubDate,
};
});
// 获取文章详情
const fetchArticleDetail = async (item) => {
const contentData = await cache.tryGet(item.link, async () => {
try {
const articleResponse = await got(item.link);
if (!articleResponse.body) {
throw new Error('No article body');
}
const $$ = load(articleResponse.body);
const content = $$('#vsb_content .v_news_content').html() || '';
const authors = $$('.show01 p i')
.toArray()
.map((el) => $$(el).text().trim());
return {
description: content,
author: authors.join(' '),
};
} catch {
return {
description: '无法获取内容',
author: '',
};
}
});
return {
...item,
description: contentData.description,
author: contentData.author,
};
};
const enhancedItems = await Promise.all(items.map((item) => fetchArticleDetail(item)));
return {
title: '广外新传学院-学院新闻',
link,
description: '广东外语外贸大学新闻与传播学院官网-学院新闻',
item: enhancedItems,
};
}