forked from Jules-GP/Ecoledirect-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcoleDirect.py
More file actions
194 lines (154 loc) · 8.54 KB
/
EcoleDirect.py
File metadata and controls
194 lines (154 loc) · 8.54 KB
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
import requests
import json
import datetime
class EcoleDirect:
""" Permet de se connecter et faire des requetes a l'API d'école direct
Allow to connect and make requests to Ecoledirect's API """
def __init__(self, username = None, password = None):
"""Create a new EcoleDirect object"""
self.token = None
self.id = None
if username is not None and password is not None:
self.connect(username, password)
def __req(self, url, payload):
"""this function make all requests to the api"""
return requests.post(url, data = payload)
def connect(self, username : str, password : str):
"""create an connection to the API"""
connection = self.__req('https://api.ecoledirecte.com/v3/login.awp', """data={}"identifiant": "{}","motdepasse": "{}"{}""".format("{",username, password, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
self.token = connection.json()['token']
self.id = connection.json()['data']['accounts'][0]['id']
def getWT(self, startDate = datetime.date.today().strftime("%Y-%m-%d"), endDate = (datetime.date.today() + datetime.timedelta(days=6) ).strftime("%Y-%m-%d")):
"""get the work time from the api"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/E/{}/emploidutemps.awp?verbe=get&'.format(self.id), """data={}"token":"{}","dateDebut": "{}","dateFin": "{}","avecTrous": false,{}""".format("{", self.token,startDate,endDate, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
def getHW(self):
"""get homework from the api"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/Eleves/{}/cahierdetexte.awp?verbe=get&'.format(self.id), """data={}"token":"{}"{}""".format("{", self.token, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
def getNotes(self):
"""get notes from the api"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/eleves/{}/notes.awp?verbe=get&'.format(self.id), """data={}"token":"{}"{}""".format("{", self.token, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
def getSL(self):
"""get sochlar life from the API"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/eleves/{}/viescolaire.awp?verbe=get&='.format(self.id), """data={}"token":"{}"{}""".format("{", self.token, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
def getCloud(self):
"""get the cloud from the API"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/cloud/E/{}.awp?verbe=get&='.format(self.id), """data={}"token":"{}"{}""".format("{", self.token, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
#news
def GetWorkTimeById(self, ID = None,helpId = False, startDate = datetime.date.today().strftime("%Y-%m-%d"), endDate = (datetime.date.today() + datetime.timedelta(days=6) ).strftime("%Y-%m-%d")):
"""retrieves the content of the schedule according to the name of the given dictionary key """
if ID == None: #checks if an ID is given
print("error ! Id not specified")
else:
"""like GetWT()"""
"""get the work time from the api"""
if self.token is None or self.id is None:
print("Error connection must be activate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/E/{}/emploidutemps.awp?verbe=get&'.format(self.id), """data={}"token":"{}","dateDebut": "{}","dateFin": "{}","avecTrous": false,{}""".format("{", self.token,startDate,endDate, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
#retrieves the contents of all dictionaries from the given key/ID
megaData = connection.json()['data']
IDlist = []
for x in range(0,len(megaData)):
dataDico = megaData[x]
IDlist.append(dataDico[ID])
pass
#bonus : allows to give all the IDs available in the schedule to the devs : obj.GetWorkTimeById("matiere", True)
if helpId:
exemple = megaData[x]
print("ID available : \n")
for cle,valeur in exemple.items():
print(cle) #give ID
pass
return IDlist
def getMessage(self):
"""get messagerie from the API"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/eleves/{}/messages.awp?verbe=getall&'.format(self.id),"""data={}"token":"{}"{}""".format("{", self.token, "}"))
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
return connection.json()['data']
def getMessageById(self, ID = None, classe = None): # ID is the key content, classe is the class holds the message : received, sent
"""get key of Message => exemple : "subject", "id" """
if ID == None:
print("error ! Id not specified")
else:
message = self.getMessage()
messages = message['messages']
try:
classe = messages[classe]
IdListMessage = []
for x in range(0,len(classe)):
Group = classe[x]
IdListMessage.append(Group[ID])
return IdListMessage
except:
print("error ! invalid class : ",classe," isn't valid")
print("class valid :\n")
for cle,valeur in messages.items():
print(cle)
pass
def getMessageContent(self, MessageId):
"""get the content of message from the given ids, to get the ids => getMessageById('id', "received") ('received' only for received messages)"""
if self.token is None or self.id is None:
print("Error connection must be acitvate")
return
connection = self.__req('https://api.ecoledirecte.com/v3/eleves/{}/messages/{}.awp?verbe=get&mode=destinataire'.format(self.id, MessageId),"""data={}"token":"{}"{}""".format("{", self.token, "}") )
if connection.json()['code'] != 200:
print("error ! bad username or password")
else:
# self.token = connection.json()['token']
content = connection.json()['data']
trueContent = content['content']
decrypte = base64.b64decode(trueContent)
print(decrypte)