-
Notifications
You must be signed in to change notification settings - Fork 1
/
courseadder.py
53 lines (49 loc) · 1.9 KB
/
courseadder.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
from datetime import datetime, date, time, timedelta
from mycalendar import Event, MyCalendar
from converter import read_file, normalize_file, make_courses, normalize_file_json, read_file_json
from course import Course
import uuid
# 把课程加入日历中
def add_course(cal: MyCalendar, course: Course):
for loc, times in course.course_times.items():
descrip = "任课教师:{}".format(course.instr_name)
# times: [(上课时间, 下课时间), ...]
for st, ed in times:
add_event(cal, st, ed, "Phone", course.course_name, descrip, loc)
def add_event(cal: MyCalendar, DTSTART: datetime, DTEND: datetime, ORGANIZER, SUMMARY, DESCRIPTION, LOCATION):
"""
向Calendar日历对象添加事件的方法
:param cal: calender日历实例
:param SUMMARY: 事件名
:param DTSTART: 事件开始时间
:param DTEND: 时间结束时间
:param DESCRIPTION: 备注
:param LOCATION: 时间地点
:return:
"""
time_format = "TZID=Asia/Shanghai:{date.year}{date.month:0>2d}{date.day:0>2d}T{date.hour:0>2d}{date.minute:0>2d}{date.second:0>2d}"
dt_start = time_format.format(date=DTSTART)
dt_end = time_format.format(date=DTEND)
create_time = datetime.today().strftime("%Y%m%dT%H%M%SZ")
cal.add_event(
DTSTART=dt_start,
TRANSP="OPAQUE",
DTEND=dt_end,
ORGANIZER=ORGANIZER,
SUMMARY=SUMMARY,
DESCRIPTION=DESCRIPTION,
LOCATION=LOCATION,
STATUS="CONFIRMED",
DTSTAMP=create_time,
UID="{}".format(uuid.uuid4())
)
if __name__ == "__main__":
path = r".\new\template.json"
contents = normalize_file_json(path)
courses = make_courses(contents, datetime(2021, 8, 30))
mycal = MyCalendar()
for i, course in enumerate(courses):
print("Processing Course {}...".format(i))
add_course(mycal, course)
mycal.save_as_ics_file("课表")
print("Done!")