-
Notifications
You must be signed in to change notification settings - Fork 2
/
aulaclient.py
210 lines (182 loc) · 7.08 KB
/
aulaclient.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
# aulaclient.py
import browser_cookie3
import requests.cookies
from rich.console import Console
import requests
class CookieFetcher:
def try_append_aula_cookies(self, aula_cookies, browser_name):
result = 'error'
try:
cookies = self.get_cookies_from_browser(browser_name)
if cookies:
aula_cookies.append(cookies)
result = 'found'
else:
result = 'notFound'
except browser_cookie3.BrowserCookieError:
result = 'notFound'
except Exception as e:
result = 'error'
self.__print(e.args)
if result == 'notFound':
message = "[yellow]not found[/]"
elif result == 'found':
message = "[green]found[/]"
elif result == 'error':
message = "[red]error occured[/]"
else:
message = "[red]unknown error occured[/]"
self.__print(f"{browser_name} cookies: {message}")
def get_cookies_from_browser(self, browser_name):
domain = 'aula.dk'
if browser_name == 'Chrome':
cookies = browser_cookie3.chrome(domain_name=domain)
elif browser_name == 'Chromium':
cookies = browser_cookie3.chromium(domain_name=domain)
elif browser_name == 'Opera':
cookies = browser_cookie3.opera(domain_name=domain)
elif browser_name == 'Opera GX':
cookies = browser_cookie3.opera_gx(domain_name=domain)
elif browser_name == 'Brave':
cookies = browser_cookie3.brave(domain_name=domain)
elif browser_name == 'Edge':
cookies = browser_cookie3.edge(domain_name=domain)
elif browser_name == 'Vivaldi':
cookies = browser_cookie3.vivaldi(domain_name=domain)
elif browser_name == 'Firefox':
cookies = browser_cookie3.firefox(domain_name=domain)
elif browser_name == 'Safari':
cookies = browser_cookie3.safari(domain_name=domain)
else:
raise NotImplementedError("Browser not supported")
return cookies
def get_aula_cookies(self):
aula_cookies = []
self.try_append_aula_cookies(aula_cookies, 'Chrome')
self.try_append_aula_cookies(aula_cookies, 'Chromium')
self.try_append_aula_cookies(aula_cookies, 'Opera')
self.try_append_aula_cookies(aula_cookies, 'Opera GX')
self.try_append_aula_cookies(aula_cookies, 'Brave')
self.try_append_aula_cookies(aula_cookies, 'Edge')
self.try_append_aula_cookies(aula_cookies, 'Vivaldi')
self.try_append_aula_cookies(aula_cookies, 'Firefox')
self.try_append_aula_cookies(aula_cookies, 'Safari')
return aula_cookies
def __print(self, message):
console = Console()
console.print(message)
class AulaClient:
baseUrl = 'https://www.aula.dk/api/v19/'
defaultLimit = 10
def __init__(self, apiVersion=19, cookieString=None):
if not cookieString:
cookie_fetcher = CookieFetcher()
cookies = cookie_fetcher.get_aula_cookies()
self.all_cookies = cookies
else:
jar = requests.cookies.RequestsCookieJar()
cookie = requests.cookies.create_cookie(domain='aula.dk', name='aula', value=cookieString)
jar.set_cookie(cookie)
self.all_cookies = [jar]
self.session = requests.Session()
self.baseUrl = f'https://www.aula.dk/api/v{apiVersion}/'
def get_profiles(self):
params = {
'method': 'profiles.getProfilesByLogin'
}
for cookies in self.all_cookies:
self.session.cookies = cookies
response_profile = self.__send_request(params)
if response_profile['status']['code'] != 448:
break
if response_profile['status']['code'] == 448:
message = "Cannot request profile, cookies may be expired or missing; try to log in."
raise PermissionError(message)
return response_profile['data']['profiles']
def get_threads(self, custom_params=None):
if custom_params is None:
custom_params = {}
default_params = {
'sortOn': 'date',
'orderDirection': 'desc',
'page': 0
}
params = self.__merge_params(
default_params,
custom_params,
'messaging.getThreads')
response = self.__send_request(params)
return response['data']
def get_messages_for_thread(self, thread_id, custom_params=None):
if custom_params is None:
custom_params = {}
default_params = {
'threadId': thread_id,
'page': 0
}
params = self.__merge_params(
default_params,
custom_params,
'messaging.getMessagesForThread')
response = self.__send_request(params)
return response['data']
def get_posts(self, institution_profile_ids, custom_params=None):
if custom_params is None:
custom_params = {}
default_params = {
'parent': 'profile',
'index': 0,
'limit': self.defaultLimit,
'isUnread': False,
'isImportant': False,
# 'creatorPortalRole': '', # guardian, employee
'ownPost': False,
'institutionProfileIds[]': institution_profile_ids
}
params = self.__merge_params(
default_params,
custom_params,
'posts.getAllPosts')
response = self.__send_request(params)
return response['data']['posts']
def get_albums(self, institution_profile_ids, custom_params=None):
if custom_params is None:
custom_params = {}
default_params = {
'index': 0,
'limit': self.defaultLimit,
'sortOn': 'createdAt',
'orderDirection': 'desc',
'filterBy': 'all',
'filterInstProfileIds[]': institution_profile_ids
}
params = self.__merge_params(
default_params,
custom_params,
'gallery.getAlbums')
response = self.__send_request(params)
return response['data']
def get_pictures(self, institution_profile_ids, album_id, custom_params=None):
if custom_params is None:
custom_params = {}
default_params = {
'albumId': album_id,
'index': 0,
'limit': self.defaultLimit,
'sortOn': 'uploadedAt',
'orderDirection': 'desc',
'filterBy': 'all',
'filterInstProfileIds[]': institution_profile_ids
}
params = self.__merge_params(
default_params,
custom_params,
'gallery.getMedia')
response = self.__send_request(params)
return response['data']['results']
def __send_request(self, params):
return self.session.get(self.baseUrl, params=params).json()
def __merge_params(self, default_params, custom_params, method):
params = default_params | custom_params
params['method'] = method
return params