-
Notifications
You must be signed in to change notification settings - Fork 1
/
fdu_ics_generator.py
124 lines (107 loc) · 4.99 KB
/
fdu_ics_generator.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
import re
import datetime
from bs4 import BeautifulSoup
from ics import Calendar, Event
def printInfo():
print('使用须知:')
print('使用本脚本之前,你需要先将HTML文件名修改为\'课程表.html\',且放置在脚本所在目录,并且使用编辑器编辑本脚本以修改学期开始日期。')
print('导入时,请务必确认时间正确,以避免不必要的麻烦。')
print('\n')
class generator:
soup = None
c = Calendar()
info = []
map = ['一','二','三','四','五','六','日']
start_h = (0,8,8,9,10,11,13,14,15,16,17,18,19,20)
end_h = (0,8,9,10,11,12,14,15,16,17,18,19,20,21)
start_m = (0,0,55,55,50,45,30,25,25,20,15,30,25,20)
end_m = (0,45,40,40,35,30,15,10,10,5,0,15,10,5)
## revise the date here
term_start_time = datetime.datetime.strptime('2020-09-14 00:00:00+0800',
'%Y-%m-%d %H:%M:%S%z')
def __init__(self):
with open("课程表.html", "rb") as f:
html = f.read().decode("utf8")
f.close()
self.soup = BeautifulSoup(html, "html.parser")
def parser(self):
spt = str(self.soup.findAll('script'))
sem = re.findall(r"var semesterIds = \[.+\]",spt)[0].lstrip('var semesterIds = [').rstrip(']').split(',')
print('检测到如下学期,请选择(1-%d):'%len(sem))
print(sem)
choose = int(input())
rows = self.soup.findAll('div', id=('list-'+sem[choose-1][1:-1]))
for row in rows:
columns = row.findAll('tr')
for column in columns:
courses = column.findAll('td')
for course in courses:
course = str(course)
self.info.append(course)
#course = course.lstrip(str(re.search(r'<td>|<td rowspan=\"\d\">',course))).rstrip('</td>')
#print(course)
def write_into_ics(self):
x = 0
while x<len(self.info):
if "rowspan" in self.info[x]:
num = re.findall(r"\d+",self.info[x])
repeat = int(num[0])
course_name = self.info[x+1].lstrip(str(re.search(r'<td>|<td rowspan=\"\d\">',self.info[x+1]))).rstrip('</td>')
x+=2
while repeat>0:
self.add_course(course_name,x)
x+=4
repeat-=1
else :
course_name = self.info[x+1].lstrip(str(re.search(r'<td>|<td rowspan=\"\d\">',self.info[x+1]))).rstrip('</td>')
x+=2
self.add_course(course_name,x)
x+=4
with open('fdu.ics', 'w', encoding='utf-8') as my_file:
my_file.writelines(self.c)
def add_course(self,course_name,start):
local = self.info[start].lstrip(str(re.search(r'<td>|<td rowspan=\"\d\">',self.info[start]))).rstrip('</td>')
weekday = re.findall(r"星期.",self.info[start+1])[0].lstrip('星期')
weekday = self.map.index(weekday)
time = re.findall(r"\d+",self.info[start+1])
week = self.info[start+2].lstrip('</td>第').rstrip('周</td>')
remark = self.info[start+3].lstrip('</td>').rstrip('</td>')
e = Event()
if('-' in week):
week = week.split('-')
#print(week)
week_cur = int(week[0])
week_end = int(week[1])
while week_cur <= week_end:
e = Event()
e.name = course_name
e.location = local
e.description = remark
offset = datetime.timedelta(days=(week_cur-1)*7+weekday,hours=self.start_h[int(time[0])],minutes=self.start_m[int(time[0])])
e.begin = self.term_start_time + offset
offset = datetime.timedelta(days=(week_cur-1)*7+weekday,hours=self.end_h[int(time[1])],minutes=self.end_m[int(time[1])])
e.end = self.term_start_time + offset
week_cur+=1
self.c.events.add(e)
else:
week = week.split(',')
#print(week)
for we in week:
e = Event()
e.name = course_name
e.location = local
e.description = remark
offset = datetime.timedelta(days=(int(we)-1)*7+weekday,hours=self.start_h[int(time[0])],minutes=self.start_m[int(time[0])])
e.begin = self.term_start_time + offset
offset = datetime.timedelta(days=(int(we)-1)*7+weekday,hours=self.end_h[int(time[1])],minutes=self.end_m[int(time[1])])
e.end = self.term_start_time + offset
self.c.events.add(e)
def main():
printInfo()
a = input('确认后输入1以继续...')
if a=='1':
g = generator()
g.parser()
g.write_into_ics()
if __name__ == '__main__':
main()