-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.py
401 lines (316 loc) · 10.2 KB
/
schedule.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from dataclasses import dataclass
from datetime import datetime, time
from time import sleep
from typing import Callable
import json
import pytz
import requests
@dataclass
class TimetableEvent:
id: str
name: str
group: str
teacher: str
room: str
time_start: datetime
time_end: datetime
@dataclass
class SchoolYear:
guid: str
time_start: datetime
time_end: datetime
def get_timetable_for_teacher(
unit_name: str,
unit_host: str,
teacher_id: str,
year: int,
start_week: int,
end_week: int,
) -> list:
return _get_timetable_for(
_get_teacher_by_id,
_get_timetable_for_teacher,
_convert_to_event_for_teacher,
unit_name,
unit_host,
teacher_id,
year,
start_week,
end_week,
)
def get_timetable_for_group(
unit_name: str,
unit_host: str,
group_id: str,
year: int,
start_week: int,
end_week: int,
) -> list:
return _get_timetable_for(
_get_class_by_id,
_get_timetable_for_class,
_convert_to_event_for_class,
unit_name,
unit_host,
group_id,
year,
start_week,
end_week,
)
def _get_timetable_for(
get_guid: Callable[[requests.Session, dict, str], dict],
get_timetable: Callable[
[requests.Session, dict, dict, str, list[SchoolYear], int, int], list
],
convert_timetable: Callable[[list, str, int, int], list],
unit_name: str,
unit_host: str,
id: str,
year: int,
start_week: int,
end_week: int,
) -> list:
session = requests.Session()
session.headers.update(
{
"X-Scope": "8a22163c-8662-4535-9050-bc5e1923df48",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
}
)
school_years = _get_school_years(session, unit_host)
unit = _get_unit_by_name(session, unit_name, unit_host)
guid = get_guid(session, unit, id)
all_weeks = []
for week in range(start_week, end_week + 1):
# don't blast the server with requests
sleep(0.5)
# the web interface seems to get a new render key every time
render_key = _get_render_key(session)
timetable = get_timetable(
session, unit, guid, render_key, school_years, year, week
)
if timetable != None:
all_weeks.extend(convert_timetable(timetable, id, year, week))
return all_weeks
def _get_school_years(session: requests.Session, host: str) -> list[SchoolYear]:
school_year_request_data = json.dumps(
{"hostName": host, "checkSchoolYearsFeatures": False}
)
resp = session.post(
"https://web.skola24.se/api/get/active/school/years",
data=school_year_request_data,
)
if resp.status_code != 200:
raise RuntimeError("could not get school year data")
school_year_response = resp.json()
active_school_years: list = school_year_response["data"]["activeSchoolYears"]
return [
SchoolYear(
year["guid"], _str_to_datetime(year["from"]), _str_to_datetime(year["to"])
)
for year in active_school_years
]
def _get_unit_by_name(session: requests.Session, name: str, host: str) -> dict:
units_request_data = json.dumps(
{"getTimetableViewerUnitsRequest": {"hostName": host}}
)
resp = session.post(
"https://web.skola24.se/api/services/skola24/get/timetable/viewer/units",
data=units_request_data,
)
if resp.status_code != 200:
raise RuntimeError("could not get data")
units_response_data = resp.json()
data = units_response_data["data"]
time_table_viewer_units_response = data["getTimetableViewerUnitsResponse"]
host_name = time_table_viewer_units_response["hostName"]
units = time_table_viewer_units_response["units"]
for unit in units:
if "unitId" in unit and unit["unitId"] == name:
unit["hostName"] = host_name
return unit
raise RuntimeError("could not find unit by name")
def _get_teacher_by_id(session: requests.Session, unit: dict, id: str) -> dict:
selection_request_data = {
"hostName": unit["hostName"],
"unitGuid": unit["unitGuid"],
"filters": {
"class": False,
"course": False,
"group": False,
"period": False,
"room": False,
"student": False,
"subject": False,
"teacher": True,
},
}
resp = session.post(
"https://web.skola24.se/api/get/timetable/selection",
data=json.dumps(selection_request_data),
)
if resp.status_code != 200:
raise RuntimeError("could not get data")
selection_response_data = resp.json()
data = selection_response_data["data"]
teachers = data["teachers"]
for teacher in teachers:
if "id" in teacher and teacher["id"] == id:
return teacher
raise RuntimeError("could not find teacher by id")
def _get_class_by_id(session: requests.Session, unit: dict, id: str) -> dict:
selection_request_data = {
"hostName": unit["hostName"],
"unitGuid": unit["unitGuid"],
"filters": {
"class": True,
"course": False,
"group": False,
"period": False,
"room": False,
"student": False,
"subject": False,
"teacher": False,
},
}
resp = session.post(
"https://web.skola24.se/api/get/timetable/selection",
data=json.dumps(selection_request_data),
)
if resp.status_code != 200:
raise RuntimeError("could not get data")
selection_response_data = resp.json()
data = selection_response_data["data"]
groups = data["classes"]
for group in groups:
if "groupName" in group and group["groupName"] == id:
return group
raise RuntimeError("could not find group by id")
def _get_render_key(session: requests.Session) -> str:
resp = session.post(
"https://web.skola24.se/api/get/timetable/render/key", data="null"
)
if resp.status_code != 200:
raise RuntimeError("could not get data")
render_key_data = resp.json()
data = render_key_data["data"]
return data["key"]
def _get_timetable_for_teacher(
session: requests.Session,
unit: dict,
teacher: dict,
render_key: str,
school_years: list[SchoolYear],
year: int,
week: int,
) -> list:
return _get_timetable(
session, unit, teacher["personGuid"], 7, render_key, school_years, year, week
)
def _get_timetable_for_class(
session: requests.Session,
unit: dict,
klass: dict,
render_key: str,
school_years: list[SchoolYear],
year: int,
week: int,
) -> list:
return _get_timetable(
session, unit, klass["groupGuid"], 0, render_key, school_years, year, week
)
def _get_timetable(
session: requests.Session,
unit: dict,
guid: str,
selectionType: int,
render_key: str,
school_years: list[SchoolYear],
year: int,
week: int,
) -> list:
school_year = _get_school_year_guid(school_years, year, week)
timetable_request_data = {
"renderKey": render_key,
"host": unit["hostName"],
"unitGuid": unit["unitGuid"],
"width": 1050, # not relevant, set to typical size
"height": 1223, # not relevant, set to typical size
"selectionType": selectionType,
"selection": guid,
"scheduleDay": 0,
"week": week,
"year": year,
"schoolYear": school_year,
}
# print(timetable_request_data)
resp = session.post(
"https://web.skola24.se/api/render/timetable",
data=json.dumps(timetable_request_data),
)
if resp.status_code != 200:
raise RuntimeError("could not get data")
timetable_response_data = resp.json()
# print(timetable_response_data)
data = timetable_response_data["data"]
# print(data)
return data["lessonInfo"]
def _convert_to_event_for_class(
timetable: list, class_name: str, year: int, week: int
) -> list:
classes = []
for klass in timetable:
texts = klass["texts"]
(name, teacher, room) = _get_texts(texts)
id = klass["guidId"]
day = int(klass["dayOfWeekNumber"])
time_start = _to_datetime(year, week, day, klass["timeStart"])
time_end = _to_datetime(year, week, day, klass["timeEnd"])
classes.append(
TimetableEvent(id, name, class_name, teacher, room, time_start, time_end)
)
return classes
def _convert_to_event_for_teacher(
timetable: list, teacher: str, year: int, week: int
) -> list:
classes = []
for klass in timetable:
texts = klass["texts"]
(name, group, room) = _get_texts(texts)
id = klass["guidId"]
day = int(klass["dayOfWeekNumber"])
time_start = _to_datetime(year, week, day, klass["timeStart"])
time_end = _to_datetime(year, week, day, klass["timeEnd"])
classes.append(
TimetableEvent(id, name, group, teacher, room, time_start, time_end)
)
return classes
def _to_datetime(year: int, week: int, day: int, time_of_day: str) -> datetime:
class_date = datetime.fromisocalendar(year, week, day)
time_notz = time.fromisoformat(time_of_day)
class_time = time(time_notz.hour, time_notz.minute, time_notz.second)
dt = datetime.combine(class_date, class_time)
tz = pytz.timezone("Europe/Stockholm")
return tz.localize(dt)
def _str_to_datetime(date_string: str) -> datetime:
date_format = "%Y-%m-%dT%H:%M:%S"
return datetime.strptime(date_string, date_format)
def _get_school_year_guid(school_years: list[SchoolYear], year: int, week: int) -> str:
date_string = f"{year}-{week}-1"
dt = datetime.strptime(date_string, "%Y-%W-%w")
return next(
sy.guid for sy in school_years if sy.time_start <= dt and sy.time_end >= dt
)
def _get_texts(texts):
if texts == None:
return ("", "", "")
count = len(texts)
if count == 0:
return ("", "", "")
elif count == 1:
return (texts[0], "", "")
elif count == 2:
return (texts[0], texts[1], "")
else:
return (texts[0], texts[1], texts[2])