forked from MatthiasReumann/webuntis-to-ics-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
156 lines (118 loc) · 4.13 KB
/
main.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
from ics import Calendar, Event
import webuntis as wu
import datetime
import sys
import toml
import requests
import json
import re
class Config:
def __init__(self, server, username, password, schoolclass, school):
self.server = server
self.username = username
self.password = password
self.schoolclass = schoolclass
self.school = school
def getFirstDay(schoolyear):
#Return current monday
return schoolyear.start
def getLastDay(schoolyear):
#Return last day of current schoolyear
return schoolyear.end
def getCurrentSchoolyear(session):
year = session.schoolyears()
return year.filter(id=year.current.id)[0]
def getStudentId(id, config):
session = requests.Session()
req = session.get('https://{}/WebUntis/api/public/timetable/weekly/pageconfig?type=5'.format(config.server),
cookies = {'JSESSIONID': id})
req_json = json.loads(req.text)
return req_json['data']['elements'][0]['id']
def getTimetable(student, schoolyear, session):
#Return timetable object of webuntis api
#return session.timetable(student=student, start=getFirstDay(schoolyear), end=getLastDay(schoolyear))
return session.timetable_extended(student=student, start=getFirstDay(schoolyear), end=getLastDay(schoolyear))
def getCalendar(session, timetable):
#Return Calendar object with events(subjects) from webuntis
calendar = Calendar()
for i in range(len(timetable)):
print(timetable[i])
subject = timetable[i].subjects
start = timetable[i].start
end = timetable[i].end
cat = str(timetable[i].code) #cancelled oder normal
try:
room = str(timetable[i].rooms)
except IndexError:
room = "n/a"
#cat = str(timetable[i].activityType)
x = timetable[i].studentGroup.split('_')
teacher = x[len(x)-1] # timetable[i].teacher
#print(cat)
if cat == "None":
cat="U"
elif cat == "cancelled":
cat="F"
elif cat == "irregular":
cat = "i"
else:
print(cat)
#elif str(cat) == "Cancelled":
# cat="C"
#elif cat == ""
if len(subject) > 0:
event = createEvent(subject[0], start, end, room, teacher, cat)
calendar.events.add(event)
return calendar
def createEvent(subject, start, end, room, teacher, cat):
#Return Event object
event = Event()
event.name = str(subject)
event.location = room.strip("[]")
event.organizer = teacher
event.description = teacher
event.categories = cat
if cat == "F":
event.status = "CANCELLED"
event.begin = start + datetime.timedelta(hours=-2)
event.end = end + datetime.timedelta(hours=-2)
return event
def createICSFile(calendar, filename):
f = open(filename, 'w')
f.write(re.sub(r'\n+', '', str(calendar)).strip('\n'))
f.close()
#with open(filename, 'w') as f:
# f.writelines(calendar)
def readTOMLFile(filename):
#Return Config object
toml_string = ""
with open(filename, 'r') as f:
toml_string = toml_string + f.read()
parsed_toml = toml.loads(toml_string)
user = parsed_toml["user"]
return Config(user["server"],user["username"],user["password"],
user["class"],user["school"])
def getSession(config):
#Return Session object
session = wu.Session(
server = config.server,
username = config.username,
password = config.password,
school = config.school,
useragent = 'webuntis-ics-calendar'
)
return session
def main():
#config = readTOMLFile(sys.argv[1])
config = readTOMLFile("config.toml")
session = getSession(config)
session.login()
schoolyear = getCurrentSchoolyear(session)
student = getStudentId(session.config['jsessionid'], config)
timetable = getTimetable(student, schoolyear, session)
#print(session.exams(start=getFirstDay(schoolyear), end=getLastDay(schoolyear)))
#print(timetable)
calendar = getCalendar(session, timetable)
createICSFile(calendar, "webuntis-timetable.ics")
session.logout()
main()