-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
323 lines (293 loc) · 12.6 KB
/
functions.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
############################################################
# Ecole-direct-in-Python #
# Author : Intermarch3 #
# start at : 07/2021 #
# End at : 02/2022 #
############################################################
#################### - Import space - ######################
import requests as rqs
import os
import platform
from datetime import date, datetime, timedelta
from ics import Calendar, Event
############################################################
def clear_screen():
"""
Clear the terminal screen
"""
if platform.system() == "Windows":
command = "cls"
else:
command = "clear"
os.system(command)
class BadCreditentials(Exception):
def __init__(self, message):
super(BadCreditentials, self).__init__(message)
class UnknownError(Exception):
def __init__(self, message):
super(UnknownError, self).__init__(message)
class BadToken(Exception):
def __init__(self, message):
super(BadToken, self).__init__(message)
class BadPeriode(Exception):
def __init__(self, message):
super(BadPeriode, self).__init__(message)
class RequestError(Exception):
def __init__(self, message):
super(RequestError, self).__init__(message)
class EcoleDirecte:
"""
Ecole direct class that return your data like grades, course schedule, homework ...
- - - - - - -
Attributes:
user: your username (str)
password: your password (str)
debug_mode: display request response and more (bool)
- - - - - - -
Methods:
fetch_homework(date_choisie):
- get your homework
fetch_schedule(date_start, date_end):
- get your schedule
ScheduleInCalendar (class)
- create calendar object, add events and export it
"""
def __init__(self, user, password, version="4.18.3"):
"""
Constructs all the necessary attributes and login with your credentials
- - - - - - -
args:
user: your username (str)
password: your password (str)
version: the api version (str)
- - - - - - -
return: none
"""
# assert test on args
try:
assert type(user) == str and user != ''
assert type(password) == str and password != ''
except:
raise ValueError("Bad args !!!")
# initialize attributs
self.v = version
self.user = user
self.password = password
self.token = ""
self.header = {
'authority': 'api.ecoledirecte.com',
'accept': 'application/json, text/plain, */*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'sec-gpc': '1',
'origin': 'https://www.ecoledirecte.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.ecoledirecte.com/',
'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7'
}
# login
data = 'data={\n\t\"uuid\": \"\",\n\t\"identifiant\": \"' + self.user + '\",\n\t\"motdepasse\": \"' + self.password + '\"\n}'
url = "https://api.ecoledirecte.com/v3/login.awp?v=" + self.v
response = rqs.post(url=url, data=data, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
self.header['x-token'] = self.token
self.id = response['data']['accounts'][0]['id']
self.email = response['data']['accounts'][0]['email']
self.header['origin'] = "https://www.ecoledirecte.com"
elif response['code'] == 505:
raise BadCreditentials("Bad username or password !!!")
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
def fetch_homework(self, date_choisie=False):
"""
Get your homework of today or a choosen date
- - - - - - -
args:
date_choisie: if true return the upcoming homework (bool)
- - - - - - -
return: None
"""
# assert test on args
try:
assert type(date_choisie) == bool or type(date_choisie) == str
except:
raise ValueError("Bad args !!!")
# return homework of today
data_rqs = 'data={\n\t\"token\": \"' + self.token + '\"\n}'
if date_choisie == False:
date_iso = datetime.now()
date_today = datetime.strftime(date_iso, '%Y-%m-%d')
url = 'https://api.ecoledirecte.com/v3/Eleves/' + str(self.id) + '/cahierdetexte/' + str(date_today) + '.awp?v=' + self.v + '&verbe=get&'
response = rqs.post(url, data_rqs, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return date_today, response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
# return list of incoming homework
elif date_choisie == "A_venir":
url = 'https://api.ecoledirecte.com/v3/Eleves/' + str(self.id) + '/cahierdetexte.awp?v=' + self.v + '&verbe=get'
response = rqs.post(url=url, data=data_rqs, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
# return homework of a given date
elif date_choisie != False and date_choisie != "A_venir":
url = 'https://api.ecoledirecte.com/v3/Eleves/' + str(self.id) + '/cahierdetexte/' + str(date_choisie) + '.awp?v=' + self.v + '&verbe=get&'
response = rqs.post(url=url, data=data_rqs, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
else:
raise ValueError("Bad args !!!")
def fetch_schedule(self, date_start="", date_end="", today=False):
"""
Get your schedule of the week or of a choosen date
- - - - - - -
args:
date_start: the start date of the wanted period in DD-MM-YYYY date format (str)
date_end: the end date of the wanted period in DD-MM-YYYY date format (str)
today: if True, fetch schedule of today (bool)
if no args: return week schedule
- - - - - - -
return: response: request response (json)
"""
# assert test on args
try:
assert type(date_start) == str and type(date_end) == str and type(today) == bool
except:
raise ValueError("Bad args !!!")
# get week schedule
if date_start == "" and date_end == "" and today == False:
day_iso = date.today()
day_str = datetime.strftime(day_iso, '%d-%m-%Y')
day_obj = datetime.strptime(day_str, '%d-%m-%Y')
date_start = day_obj - timedelta(days=day_obj.weekday())
date_end = date_start + timedelta(days=5)
data = 'data={"dateDebut": "' + str(date_start) + '", "dateFin": "' + str(date_end) + '", "avecTrous": false, "token": "' + str(self.token) + '"}'
url = "https://api.ecoledirecte.com/v3/E/" + str(self.id) + "/EmploiDuTemps.awp?v=" + self.v + "&verbe=get&"
response = rqs.post(url, data, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
# get today schedule
elif today == True:
day = date.today()
data = 'data={"dateDebut": "' + str(day) + '", "dateFin": "' + str(day) + '", "avecTrous": false, "token": "' + str(self.token) + '"}'
url = "https://api.ecoledirecte.com/v3/E/" + str(self.id) + "/EmploiDuTemps.awp?v=" + self.v + "&verbe=get&"
response = rqs.post(url, data, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
# get schedule of given date
else:
try:
assert date_start != "" and date_end != ""
except:
raise BadPeriode("Date manquantes !!!")
data = 'data={"dateDebut": "' + str(date_start) + '", "dateFin": "' + str(date_end) + '", "avecTrous": false, "token": "' + str(self.token) + '"}'
url = "https://api.ecoledirecte.com/v3/E/" + str(self.id) + "/EmploiDuTemps.awp?v=" + self.v + "&verbe=get&"
response = rqs.post(url, data, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
def fetch_grades(self):
"""
Get your grades
- - - - - - -
args: none
- - - - - - -
return: response: request response (json)
"""
data = 'data={\n\t\"token\": \"' + self.token + '\"\n}'
url = 'https://api.ecoledirecte.com/v3/eleves/'+ str(self.id) +'/notes.awp?v=' + self.v + '&verbe=get&'
response = rqs.post(url, data, headers=self.header).json()
if response['code'] == 200:
self.token = response['token']
return response['data']
else:
raise RequestError('Error {}: {}'.format(response['code'], response['message']))
class ScheduleInCalendar:
"""
Schedule in Calendar class that create a calendar object, add event and export it (.ics)
- - - - - - -
Attributes:
name: name of the calendar file (str)
- - - - - - -
Methods:
add_calendar_event(calendar, data)
- add event to calendar object
export_calendar(calendar, name)
- create a calendar file with a calendar object
"""
def __init__(self, name=""):
"""
create calendar object
- - - - - - -
args:
name: the file name (str)
- - - - - - -
return: none
"""
# assert test on args
try:
assert type(name) == str and name != ""
except:
raise ValueError("Bad args !!!")
# initialize attributs
self.name = name
self.calendar = Calendar(creator="Intermarch3")
def add_calendar_event(self, data):
"""
add event in a calendar object with your schedule data
- - - - - - -
args:
data: the response of schedule request (json)
- - - - - - -
return:
calendar object (obj)
"""
# assert test on args
try:
assert type(data) == dict or type(data) == list
except:
raise ValueError("Bad args !!!")
# add event of each course
for cour in data:
e = Event()
e.name = cour['matiere']
e.begin = cour['start_date']
e.end = cour['end_date']
desc = "Prof: " + cour['prof']
if cour['salle'] != "":
desc += "\nSalle:" + cour['salle']
e.description = desc
self.calendar.events.add(e)
return self.calendar
def export_calendar(self):
"""
Create a calendar file (ics)
- - - - - - -
args:
calendar: the calendar object (obj)
name: the file name (str)
- - - - - - -
return: none
"""
name = str(self.name) + ".ics"
with open(name, 'w') as file:
file.writelines(self.calendar.serialize_iter())
file.close()