-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetICS.js
More file actions
164 lines (154 loc) · 4.41 KB
/
getICS.js
File metadata and controls
164 lines (154 loc) · 4.41 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const axios = require('axios');
const fs = require('fs').promises;
const SocksProxyAgent = require('socks-proxy-agent');
const dayjs = require('dayjs');
let conf = {
"site": "",
"xh": "",
"password": "",
"firstWeek": "20200831",
"xnxqid": "2020-2021-1",
"proxy": "",
"filename": "calendar.ics"
}
const ua = 'Mozilla/5.0 (Winsdows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'
const weekMonday = new Array(31);
let agent = undefined;
// 初始化 设置代理 查询
async function init() {
if (conf.proxy !== '') {
agent = new SocksProxyAgent(conf.proxy);
}
try {
await createFile(conf.filename);
} catch (err) {
console.log(err);
}
getEachWeekDay();
const token = await apiLogin().catch(err => console.log('账号登录失败\n' + err));
await Promise.all(weekMonday.map((monday, week) => {
return new Promise((resolve, reject) => {
getData(token, week + 1)
.then(res => { if (res) parseData(monday, res.data) })
.then(() => resolve())
.catch(err => reject(err))
})
}))
.catch(err => console.log(err));
appendFile('END:VCALENDAR');
console.log("下载完成");
}
// 计算每个周一的日期并存储
const getEachWeekDay = () => {
let mondayDate = dayjs(conf.firstWeek);
for (let i = 0; i < 30; i++) {
weekMonday[i] = mondayDate;
mondayDate = mondayDate.add(7, 'day');
}
}
// 获取指定周的课程表
function getData(token, zc) {
return new Promise((resolve) => {
axios({
url: `http://${conf.site}/app.do?method=getKbcxAzc&xh=${conf.xh}&xnxqid=${conf.xnxqid}&zc=${zc}`,
method: 'GET',
headers: {
'User-Agent': ua,
'token': token
},
timeout: 5000,
httpAgent: agent
})
.catch(err => console.log(`ERROR: 第${zc}周查询失败 ${err}`))
.then(res => {
if (JSON.stringify(res.data[0]) === 'null') console.log(`INFO: 第${zc}周无课 `) || resolve();
else resolve(res)
});
})
}
// 将 json 数据解析成 iCalendar 数据
function parseData(monday, data) {
return new Promise((resolve, reject) => {
const now = dayjs();
for (event in data) {
let day = data[event].kcsj.slice(0, 1);
curday = monday.add(day - 1, 'day');
try {
appendFile(
`BEGIN:VEVENT
DTSTART;TZID=Asia/Shanghai:${curday.format('YYYYMMDD')}T${data[event].kssj.replace(/:/, '')}00
DTEND;TZID=Asia/Shanghai:${curday.format('YYYYMMDD')}T${data[event].jssj.replace(/:/, '')}00
DTSTAMP:${now.format('YYYYMMDD[T]HHmmss[Z]')}
CREATED:${now.format('YYYYMMDD[T]HHmmss[Z]')}
DESCRIPTION:
LAST-MODIFIED:${now.format('YYYYMMDD[T]HHmmss[Z]')}
LOCATION:${data[event].jsmc} ${data[event].jsxm}
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:${data[event].kcmc}
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:This is an event reminder
TRIGGER:-P0DT0H15M0S
END:VALARM
END:VEVENT
`
)
}
catch (err) {
reject(err);
}
resolve();
}
})
}
// api 登录
function apiLogin() {
return new Promise((resolve, reject) => {
axios({
url: `http://${conf.site}/app.do?method=authUser&xh=${conf.xh}&pwd=${conf.password}`,
method: 'GET',
headers: {
'User-Agent': ua,
},
timeout: 5000,
httpAgent: agent
}).then((res) => {
token = res.data.token;
if (token === '-1')
reject(res.data.msg);
else
resolve(token);
}).catch((err) => (reject(err)));
});
}
// 创建文件
function createFile(filename) {
return fs.writeFile(filename,
`BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:Asia/Shanghai
BEGIN:VTIMEZONE
TZID:Asia/Shanghai
X-LIC-LOCATION:Asia/Shanghai
BEGIN:STANDARD
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
TZNAME:CST
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE
`, { encoding: 'utf8', flag: 'w' }, err => {
if (err) throw err;
})
}
// 向文件中追加内容
function appendFile(content) {
return fs.writeFile(conf.filename, content, { encoding: 'utf8', flag: 'a' }, err => {
if (err) throw err;
});
}
init();