-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileman.py
139 lines (124 loc) · 4.53 KB
/
fileman.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
# !/usr/bin/env python
# --------------------------------------------------------------
# File: calendar.py
# Project: FDU_Timetable
# Created: Wednesday, 13th November 2019 5:50:47 pm
# @Author: Molin Liu, MSc in Data Science
# Contact: [email protected]
# Last Modified: Wednesday, 13th November 2019 5:50:54 pm
# Copyright © Rockface 2019 - 2020
# --------------------------------------------------------------
from fetchdata import Course
from icalendar import Calendar, Event
from datetime import date
import datetime
import pytz
from log import LogConfig
'''
Initialize Logger
'''
logger = LogConfig('console').getLogger()
'''
FDU Course Time
'''
time_slot_start = {
0: (8, 00),
1: (8, 55),
2: (9, 55),
3: (10, 50),
4: (11, 45),
5: (13, 30),
6: (14, 25),
7: (15, 25),
8: (16, 20),
9: (17, 15),
10: (18, 30),
11: (19, 25),
12: (20, 20)
}
def iso_year_start(iso_year):
"The gregorian calendar date of the first day of the given ISO year"
fourth_jan = datetime.date(iso_year, 1, 4)
delta = datetime.timedelta(fourth_jan.isoweekday()-1)
return fourth_jan - delta
def iso_to_gregorian(iso_year, iso_week, iso_day):
"Gregorian calendar date for the given ISO year, week and day"
year_start = iso_year_start(iso_year)
return year_start + datetime.timedelta(days=iso_day-1, weeks=iso_week-1)
def convertDate(week, day, slot):
'''
Convert school week to format date.
Parameters:
week: week number from FDU timetable;
教学周;
day: day from FDU timetable;
星期几
Note: 0 represents Monday, 1 is Tuesday, and so forth.
0 代表 星期一
slot: class number in a day;
第几节课
Note: FDU has 14 classes a day; Class number starts with 0.
FDU 一天14节课,从0开始。
Returns:
Tuple of start datetime, and end datetime.
A class is 45mins long.
'''
tz = pytz.timezone('Asia/Shanghai')
first_week_in_year = datetime.datetime(2019, 9, 8).isocalendar()[1]
week += first_week_in_year
day = int(day)
GC_date = iso_to_gregorian(2019, week, day+1)
start_hour, start_min = time_slot_start[int(slot)]
GC_datetime = datetime.datetime.combine(
GC_date, datetime.time(hour=start_hour, minute=start_min))
GC_endTime = GC_datetime+datetime.timedelta(minutes=45)
GC_datetime.replace(tzinfo=tz)
GC_endTime.replace(tzinfo=tz)
return GC_datetime, GC_endTime
def createCourseEvent(course, calendar):
'''
Create events for a course.
为单个课程创建日历事件;
Note: 需要注意的是本身FDU课程表数据存在重复,已经在Course类中使用
set做了去重,但由于课程太多,没有做到一一对比。
Note: The data from FDU teaching system exeists duplicate
events, which has been solved in class Course. However, due
to the tremendous courses we have, we didn't check them
respectively.
'''
for week in course.available_week:
for one_course in course.course_time:
day, time = one_course.split(',')
start_time, end_time = convertDate(week, day, time)
event = Event()
event.add('summary', course.course_name)
event.add('dtstamp', datetime.datetime.now())
event.add('dtstart', start_time)
event.add('dtend', end_time)
event.add('location', course.room_name)
event.add('description', course.teacher_names)
calendar.add_component(event)
return calendar
def createCalendar(course_list):
'''
Create .isc file
'''
cal = Calendar()
cal['version'] = '2.0'
cal['prodid'] = '-//Fudan, ©2019 Molin. L//Timetable//CN'
for course in course_list:
cal = createCourseEvent(course, cal)
'''
生成的ics文件最好不要airdrop到手机,这样会自动添加进日历,且无法设置标记。
建议用邮件发给自己,仅测试过系统自带邮件app,可以先确认,再添加。
It's not recommanded to airdrop the produced ics file to your phone.
As the result, these events will be automatically loaded to your
calendar, which I'm sure you will not like this, what's more,
you can't add tag to these events either.
The best way to add these events is to send to your email inbox,
open it on your phone. Recommend to use the ios default mail app.
'''
f = open('fdu_timetable.ics', 'wb')
f.write(cal.to_ical())
f.close()
logger.info("SUCCESS: All success, enjoy.")