-
Notifications
You must be signed in to change notification settings - Fork 28
/
export_timetable.py
135 lines (124 loc) · 4.64 KB
/
export_timetable.py
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
# Copyright (C) 2022 by the XiDian Open Source Community.
#
# This file is part of xidian-scripts.
#
# xidian-scripts is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# xidian-scripts is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with xidian-scripts. If not, see <http://www.gnu.org/licenses/>.
import pkg_resources
import subprocess
import sys
import os
try:
pkg_resources.require(('libxduauth', 'icalendar'))
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict):
subprocess.check_call([
sys.executable, '-m', 'pip', 'install', 'libxduauth', 'icalendar'
])
try:
import credentials
USERNAME = credentials.IDS_USERNAME
PASSWORD = credentials.IDS_PASSWORD
except ImportError:
USERNAME, PASSWORD = [os.getenv(i) for i in ('IDS_USER', 'IDS_PASS')]
if not USERNAME or not PASSWORD:
print('请设置环境变量 IDS_USER 和 IDS_PASS')
exit(1)
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from libxduauth import EhallSession
TIME_SCHED = [
("8:30", "10:05"),
("10:25", "12:00"),
("14:00", "15:35"),
("15:55", "17:30"),
("19:00", "20:35")
]
ses = EhallSession(USERNAME, PASSWORD)
ses.use_app(4770397878132218)
semesterCode = ses.post(
'http://ehall.xidian.edu.cn/jwapp/sys/wdkb/modules/jshkcb/dqxnxq.do',
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01'
}
).json()['datas']['dqxnxq']['rows'][0]['DM']
termStartDay = datetime.strptime(ses.post(
'http://ehall.xidian.edu.cn/jwapp/sys/wdkb/modules/jshkcb/cxjcs.do',
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01'
},
data={
'XN': semesterCode.split('-')[0] + '-' + semesterCode.split('-')[1],
'XQ': semesterCode.split('-')[2]
}
).json()['datas']['cxjcs']['rows'][0]["XQKSRQ"].split(' ')[0], '%Y-%m-%d')
qResult = ses.post(
'http://ehall.xidian.edu.cn/jwapp/sys/wdkb/modules/xskcb/xskcb.do',
headers={ # 学生课程表
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01'
}, data={
'XNXQDM': semesterCode
}
).json()
qResult = qResult['datas']['xskcb'] # 学生课程表
if qResult['extParams']['code'] != 1:
raise Exception(qResult['extParams']['msg'])
courseList = []
for i in qResult['rows']:
while len(courseList) < len(i['SKZC']):
courseList.append([[], [], [], [], [], [], []])
for j in range(len(i['SKZC'])):
if i['SKZC'][j] == '1' and int(i['KSJC']) <= 10 and int(i['JSJC']) <= 10:
courseList[j][int(i['SKXQ']) - 1].append({
'name': i['KCM'],
'location': i['JASMC'],
'sectionSpan': (int(i['KSJC']), int(i['JSJC']))
})
cal = Calendar()
for week_cnt in range(len(courseList)):
for day_cnt in range(len(courseList[week_cnt])):
for course in courseList[week_cnt][day_cnt]:
e = Event()
if course['sectionSpan'][0] > 10:
continue
elif course['location'] == None:
course['location'] = '待定'
e.add(
"description",
'课程名称:' + course['name'] +
';上课地点:' + course['location']
)
e.add('summary', course['name'] + '@' + course['location'])
date = termStartDay + \
timedelta(days=week_cnt * 7 + day_cnt) # 从第一 周的第一天起
(beginTime, endTime) = TIME_SCHED[int(course['sectionSpan'][1] / 2 - 1)]
(beginTime, endTime) = (beginTime.split(':'), endTime.split(':'))
e.add(
"dtstart",
date.replace(
hour=int(beginTime[0]),
minute=int(beginTime[1])
)
)
e.add(
"dtend",
date.replace(
hour=int(endTime[0]),
minute=int(endTime[1])
)
)
cal.add_component(e)
f = open(USERNAME + '_' + semesterCode + ".ics", 'wb')
f.write(cal.to_ical())
f.close()
print("日历文件已保存到 " + USERNAME + '_' + semesterCode + ".ics")