-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatchel_utils.py
125 lines (107 loc) · 4.03 KB
/
satchel_utils.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
from datetime import datetime, timedelta
import requests
from typing import List, Dict, Tuple, Any
import os
from dotenv import load_dotenv
from utils import get_user_id_and_school_id_for_authtoken
load_dotenv()
AUTHORIZATION = os.getenv("AUTHORIZATION")
USER_ID, SCHOOL_ID = get_user_id_and_school_id_for_authtoken(AUTHORIZATION)
# USER_ID = os.getenv("USER_ID")
# SCHOOL_ID = os.getenv("SCHOOL_ID")
def get_events(
authorization: str,
user_id: int,
school_id: int,
start_date=datetime.now().strftime("%Y-%m-%d")
) -> Dict[str, Any]:
headers = {
"authority": "api.satchelone.com",
"accept": "application/smhw.v2021.5+json",
"accept-language": "en-GB,en;q=0.8",
"authorization": authorization,
"content-type": "application/json",
"origin": "https://www.satchelone.com",
"referer": "https://www.satchelone.com/",
"sec-ch-ua": '"Not_A Brand";v="8", "Chromium";v="120", "Brave";v="120"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Linux"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"sec-gpc": "1",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"x-platform": "web",
}
params = {
"requestDate": start_date,
}
response = requests.get(
f"https://api.satchelone.com/api/timetable/school/{school_id}/student/{user_id}",
params=params,
headers=headers,
)
if response.status_code != 200:
raise Exception(
f"Error getting events from Satchel. Status code: {response.status_code}"
)
return response.json()
def process_all_lessons(
lessons: List[Dict[str,Any]],
add_to_calendar: Dict[str,List[str]]
) -> Dict[str,List[str]]:
for lesson in lessons:
# print(lesson)
name = lesson["classGroup"]["subject"]
start = lesson["period"]["startDateTime"]
end = lesson["period"]["endDateTime"]
room = lesson["room"]
teacher = lesson["teacher"]
teacher_name = (
f"{teacher['title']} {teacher['forename'][0]} {teacher['surname']}"
)
details = f"{teacher_name} - {room}"
# Needed to remove the "Supervised Study" events which clash with lessons
try:
if (
add_to_calendar[start][3] == start
and add_to_calendar[start][0] == "Supervised Study"
):
add_to_calendar[start] = [name, room, details, start, end]
except KeyError:
add_to_calendar[start] = [name, room, details, start, end]
return add_to_calendar
def get_all_events_from_satchel(
) -> Tuple[Dict[str,List[str]], datetime]:
start_date = datetime.now()
add_to_calendar = {}
for _ in range(10):
events = get_events(
AUTHORIZATION, USER_ID, SCHOOL_ID, start_date.strftime("%Y-%m-%d")
)
days = events["weeks"][0]["days"]
for day in days:
if day["lessons"] == []:
continue
lessons = day["lessons"]
add_to_calendar = process_all_lessons(lessons, add_to_calendar)
start_date = start_date + timedelta(days=7)
# start_date is now the date of the last lesson
return add_to_calendar, start_date
def get_all_school_events(
) -> Tuple[Dict[str,List[str]], datetime]:
add_to_calendar = {}
start_date = datetime.datetime.now()
for _ in range(100):
events = get_events(AUTHORIZATION, USER_ID, start_date.strftime("%Y-%m-%d"))
school_events = events["weeks"][0]["events"]
for school_event in school_events:
add_to_calendar[school_event["starts_at"]] = [
school_event["name"],
"",
school_event["event_type"],
school_event["starts_at"],
school_event["ends_at"],
]
start_date = start_date + datetime.timedelta(days=7)
return add_to_calendar, start_date