-
Notifications
You must be signed in to change notification settings - Fork 8
/
generateICS.py
366 lines (337 loc) · 13.1 KB
/
generateICS.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
generateICS 生成及导出.ics日历文件
@Author: MiaoTony, Triple-Z(原作者)
"""
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from pytz import timezone
# import tempfile
from hashlib import md5
import os
# from sys import getsizeof
def create_ics(lessons, semester_start_date):
"""
生成课表的ical
:param lessons: {list} Lesson类组成的列表
:param semester_start_date: {datatime}学期开始日期
:return: cal {Calendar}
"""
cal = Calendar()
cal.add('prodid', '-//miaotony//NUAA_ClassSchedule//CN')
cal.add('version', '2.0')
cal.add('X-WR-TIMEZONE', 'Asia/Shanghai')
for lesson in lessons:
for week in lesson.vaildWeeks:
event = Event()
event.add('summary', lesson.courseName)
# 错峰时间批次对应的教学楼
batch1_jjl = ('1', '6', '7', 'D2') # 将军路校区
batch1_mgg = ('18',) # 明故宫校区
batch2_jjl = ('2', '4', '5', 'D1', 'D3')
batch2_mgg = ('7', '13')
batch1_tmh = ('T11', 'T12', 'T13')
batch2_tmh = ('T14', 'T15')
# Lesson start time
# 潜在bug: roomName为空的情况默认为将军路明故宫的时间表
if '天目湖' in lesson.roomName:
if lesson.roomName.startswith(batch1_tmh):
course_order = {
'1': "08:30",
'2': "09:25",
'3': "10:25",
'4': "11:20",
'5': "14:00",
'6': "14:55",
'7': "16:00",
'8': "16:55",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "09:20",
'2': "10:15",
'3': "11:15",
'4': "12:10",
'5': "14:50",
'6': "15:45",
'7': "16:50",
'8': "17:45",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
elif lesson.roomName.startswith(batch2_tmh):
course_order = {
'1': "08:30",
'2': "09:25",
'3': "10:40",
'4': "11:35",
'5': "14:00",
'6': "14:55",
'7': "16:00",
'8': "16:55",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "09:20",
'2': "10:15",
'3': "11:30",
'4': "12:25",
'5': "14:50",
'6': "15:45",
'7': "16:50",
'8': "17:45",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
else:
course_order = {
'1': "08:30",
'2': "09:25",
'3': "10:30",
'4': "11:25",
'5': "14:00",
'6': "14:55",
'7': "16:00",
'8': "16:55",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "09:20",
'2': "10:15",
'3': "11:20",
'4': "12:15",
'5': "14:50",
'6': "15:45",
'7': "16:50",
'8': "17:45",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
elif ('明故宫' in lesson.roomName and lesson.roomName.startswith(batch1_mgg)) or \
('将军路' in lesson.roomName and lesson.roomName.startswith(batch1_jjl)):
# Batch 1
course_order = {
'1': "08:00",
'2': "08:55",
'3': "10:05",
'4': "11:00",
'5': "14:00",
'6': "14:55",
'7': "16:15",
'8': "17:10",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "08:50",
'2': "09:45",
'3': "10:55",
'4': "11:50",
'5': "14:50",
'6': "15:45",
'7': "17:05",
'8': "18:00",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
elif ('明故宫' in lesson.roomName and lesson.roomName.startswith(batch2_mgg)) or \
('将军路' in lesson.roomName and lesson.roomName.startswith(batch2_jjl)):
# Batch 2
course_order = {
'1': "08:00",
'2': "08:55",
'3': "10:25",
'4': "11:20",
'5': "14:00",
'6': "14:55",
'7': "16:15",
'8': "17:10",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "8:50",
'2': "9:45",
'3': "11:15",
'4': "12:10",
'5': "14:50",
'6': "15:45",
'7': "17:05",
'8': "18:00",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
else:
course_order = {
'1': "08:00",
'2': "08:55",
'3': "10:15",
'4': "11:10",
'5': "14:00",
'6': "14:55",
'7': "16:15",
'8': "17:10",
'9': "18:45",
'10': "19:40",
'11': "20:35",
}
end_time = {
'1': "08:50",
'2': "09:45",
'3': "11:05",
'4': "12:00",
'5': "14:50",
'6': "15:45",
'7': "17:05",
'8': "18:00",
'9': "19:35",
'10': "20:30",
'11': "21:25",
}
lesson_start = course_order[lesson.course_unit[0]].split(":")
lesson_start_hour = int(lesson_start[0])
lesson_start_minute = int(lesson_start[1])
lesson_start_time = semester_start_date + \
timedelta(weeks=week - 1, days=int(lesson.day_of_week) - 1,
hours=lesson_start_hour - semester_start_date.hour,
minutes=lesson_start_minute - semester_start_date.minute,
seconds=-semester_start_date.second,
milliseconds=-semester_start_date.microsecond)
elapsed_time = datetime.strptime(end_time[lesson.course_unit[len(lesson.course_unit) - 1]], "%H:%M") - \
datetime.strptime(course_order[lesson.course_unit[0]], "%H:%M")
lesson_end_time = lesson_start_time + \
timedelta(minutes=(elapsed_time / 60).seconds)
event.add('dtstart', lesson_start_time)
event.add('dtend', lesson_end_time)
# event.add('dtstamp', datetime.now(tz=timezone('Asia/Shanghai')))
event.add('location', lesson.roomName)
try:
event.add('description', lesson.output_description(week=week))
except UnicodeDecodeError:
raise Exception("ERROR!") # 放弃python2.x了
cal.add_component(event)
return cal
def create_exam_ics(cal, exams):
"""
生成考试安排的iCal
:param cal: 加入了课表后的cal
:param exams: {list}考试安排Exam类组成的列表
:return: cal {Calendar}
"""
for exam in exams:
if isinstance(exam.examDate, list): # 如果时间未确定, 不导出
event = Event()
event.add('summary', exam.courseName + '_' + exam.examType)
# exam start time
examStartTime = datetime(exam.examDate[0], exam.examDate[1], exam.examDate[2], exam.examTime[0],
exam.examTime[1], 0,
tzinfo=timezone('Asia/Shanghai')) # examTime[0]、examTime[1] 起始时间
examEndTime = datetime(exam.examDate[0], exam.examDate[1], exam.examDate[2], exam.examTime[2],
exam.examTime[3], 0,
tzinfo=timezone('Asia/Shanghai')) # examTime[2]、examTime[3] 终止时间
event.add('dtstart', examStartTime)
event.add('dtend', examEndTime)
event.add('location', exam.examLocation)
event.add('description', exam.description)
cal.add_component(event)
return cal
def create_weeknum_ics(cal, semester_start_date):
"""
给ical生成周数事件(持续一周的`第x周`事件)
:param cal: 加入了课表后的cal
:param semester_start_date: {datatime}学期开始日期):
:return: cal {Calendar}
"""
for week_num in range(1, 21):
# 20 weeks
event = Event()
event.add('summary', '第 ' + str(week_num) + ' 周')
weekStartTime = semester_start_date + \
timedelta(weeks=week_num - 1, days=0, hours=0,
minutes=0, seconds=0, milliseconds=0)
weekEndTime = semester_start_date + \
timedelta(weeks=week_num - 1, days=7, hours=0,
minutes=0, seconds=0, milliseconds=0)
event.add('dtstart', weekStartTime)
event.add('dtend', weekEndTime)
event.add('location', 'NUAA')
event.add('description', '第 ' + str(week_num) + ' 周')
cal.add_component(event)
return cal
def export_ics(cal, semester_year, semester, stuID):
filename = 'NUAAiCal-Data/Schedule_' + stuID + \
'_' + semester_year + '-' + semester + '.ics'
if os.path.exists('NUAAiCal-Data'):
# print('Directory exists.')
if os.path.isfile(filename):
# File exists, check whether need to be updated.
tem = open('.temp', 'w+b')
tem_path = os.path.abspath(tem.name)
tem.write(cal.to_ical())
tem_filename = tem.name
tem.read() # fix a py2.7 bug...
tem.close()
# print(getsizeof(tem.read()))
is_update = not is_same(tem_path, filename)
# print("Temp file name is %s, in %s" % (tem_filename, os.path.abspath(tem_filename)))
os.remove(tem_path)
if is_update:
print('有更新的信息!')
f = open(os.path.join(filename), 'wb')
f.write(cal.to_ical())
f.close()
print("更新的日历文件已导出到 \"" + os.path.abspath(filename) + "\"。")
else:
print('没有需要更新的信息!')
print("原有的日历文件位置为 \"" + os.path.abspath(filename) + "\"。")
else:
f = open(os.path.join(filename), 'wb')
f.write(cal.to_ical())
f.close()
print("日历文件已导出到 \"" + os.path.abspath(filename) + "\"。")
else:
os.mkdir('NUAAiCal-Data')
f = open(os.path.join(filename), 'wb')
f.write(cal.to_ical())
f.close()
# print('ICS file has successfully exported to \"' + filename + '\".')
print("日历文件已导出到 \"" + os.path.abspath(filename) + "\"。")
return True
def is_same(file1, file2):
hash1 = md5()
with open(file1, 'rb') as f1:
f1_data = f1.read()
# print(getsizeof(f1_data))
hash1.update(f1_data)
md5_1 = hash1.hexdigest()
# print(f1_data)
# print(file1.name)
# print(md5_1)
hash2 = md5()
with open(file2, 'rb') as f2:
f2_data = f2.read()
# print(getsizeof(f2_data))
hash2.update(f2_data)
md5_2 = hash2.hexdigest()
# print(f2_data)
# print(f2.name)
# print(md5_2)
# print(f1_data == f2_data)
if md5_1 == md5_2:
return True
else:
return False