forked from wallforfry/ADE-ESIEE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aurion_api.py
406 lines (330 loc) · 12.7 KB
/
aurion_api.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
402
403
404
405
406
"""
Project : ADE-ESIEE
File : aurion_api.py
Author : DELEVACQ Wallerand
Date : 21/03/2017
"""
from html.parser import HTMLParser
import requests
import csv
class DataParser(HTMLParser):
"""
Parse data in aurion page
"""
def __init__(self):
super().__init__()
self.recording = 0
self.data = []
self._data = ""
self._starttag = None
def handle_starttag(self, tag, attrs):
if tag != 'td':
return
self._starttag = tag
def handle_data(self, data):
self._data = data
def handle_endtag(self, tag):
if self._starttag == "td":
self.data.append(self._data)
self._data = ""
self._starttag = None
class MenuParser(HTMLParser):
"""
Parse menu idt value
"""
def __init__(self):
super().__init__()
self.recording = 0
self.data = []
self.idt = []
def handle_starttag(self, tag, attrs):
if tag != 'a':
return
if len(attrs) != 6:
return
if "link " not in attrs[2][1]:
return
attribute = attrs[1][1]
self.idt.append(attribute)
if self.recording:
self.recording += 1
return
self.recording = 1
def handle_endtag(self, tag):
if tag == 'a' and self.recording:
self.recording -= 1
def handle_data(self, data):
if data.strip():
if self.recording:
self.data.append(data.strip())
class ViewStateParser(HTMLParser):
"""
Parse viewstate hidden value
"""
def __init__(self):
super().__init__()
self.recording = 0
self.data = []
def handle_starttag(self, tag, attrs):
if tag != 'input':
return
if self.recording:
self.recording += 1
return
for name, value in attrs:
if name == 'name' and value == 'javax.faces.ViewState':
self.data.append(attrs[3][1])
break
else:
return
self.recording = 1
def handle_endtag(self, tag):
if tag == 'input' and self.recording:
self.recording -= 1
def handle_data(self, data):
if data.strip():
if self.recording:
self.data.append(data.strip())
class PersoException(Exception):
def __init__(self, raison):
self.raison = raison
def __str__(self):
return self.raison
class Aurion():
"""
Aurion class allow interraction with https://webaurion.esiee.fr website
"""
menu_idt = []
menu_data = []
viewstate = ""
session = None
def __init__(self):
None
def connect(self, username, password):
"""
Create connection on Aurion and set unites_and_groups data
:param username: Username ESIEE
:param password: Password
:return:
"""
self.session = requests.session()
login_url = "https://webaurion.esiee.fr/faces/Login.xhtml"
result = self.session.get(login_url)
payload = {
"j_username": username,
"j_password": password,
}
login_url = "https://webaurion.esiee.fr/j_spring_security_check"
result = self.session.post(
login_url,
data=payload,
headers=dict(referer=login_url, cookies=self.session.cookies["JSESSIONID"])
)
viewstate_url = "https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml"
viewstate = self.session.get(
viewstate_url,
headers=dict(referer=viewstate_url, cookies=self.session.cookies["JSESSIONID"])
)
parser = ViewStateParser()
parser.feed(viewstate.text)
self.viewstate = parser.data[0]
menu_parser = MenuParser()
menu_parser.feed(viewstate.text)
try:
self.menu_data = menu_parser.data
self.menu_idt = menu_parser.idt
if menu_parser.data == []:
raise PersoException("Wrong credentials")
except ValueError:
raise PersoException("Wrong credentials")
def get_unites_and_groups(self):
data = self.getGroupsFirstPage(self.session, self.viewstate) + self.getGroupsSecondPage(
self.session,
self.viewstate)
return data
def get_marks(self):
"""
set Marks
:param session_requests: requests's session
:param viewstate_value: value of hidden filed viewstate
:return: return data likes [{"year": YEAR, "unite": UNITE, "name": NAME, "mark": MARK, "coeff": COEFF}, ....]
"""
idt = self.menu_idt[self.menu_data.index("Mes Notes")]
url = 'https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml'
"""payload = {"form": "form",
"formlargeurDivCenter": "1691",
"form:headerSubview:j_idt46": "44807",
"form:j_idt168-value": "false",
"javax.faces.ViewState": self.viewstate,
"form:Sidebar:j_idt" + idt: "form:Sidebar:j_idt" + idt # E2 321 E1 76 BJ6D)jfVu
}"""
payload = {"form": "form",
"formlargeurDivCenter": "1608",
"form:sauvegarde": "",
"form:j_idt751:j_idt753_dropdown": "1",
"form:j_idt751:j_idt753_mobiledropdown": "1",
"form:j_idt751:j_idt753_page": "0",
"form:j_idt856:j_idt753_page": "0",
"form:j_idt829_focus": "",
"form:j_idt829_input": "44808",
"javax.faces.ViewState": self.viewstate,
idt: idt # E2 321 E1 76 BJ6D)jfVu
}
result = self.session.post(
url,
headers=dict(referer=url),
data=payload
)
parser = DataParser()
parser.feed(result.text)
data = parser.data[15:]
marks = [{"year": data[i], "unite": data[i + 1], "name": data[i + 2], "mark": data[i + 3], "coeff": data[i + 5]}
for i in range(0, len(data), 6)]
average = 0.0
coeff = 0.0
for elt in marks:
average += float(elt['mark'].replace(",", "."))*float(elt['coeff'].replace(",", "."))
coeff += float(elt['coeff'].replace(",", "."))
average /= coeff
marks.append({"year": "", "unite": "", "name": "Moyenne", "mark": round(average, 2), "coeff": "1"})
return marks
def get_absences(self):
"""
get absences
:param session_requests: requests's session
:param viewstate_value: value of hidden filed viewstate
:return: return data likes [{"date": DATE, "hours": START-END, "unite_code": UNITE_CODE, "name": UNITE_NAME, "prof": professor's name, "type": TYPE_OF_COURSE, "number": NUMBER_OF_HOURS_MISSING, "reason": MOTIF}, ....]
"""
idt = self.menu_idt[self.menu_data.index("Mes Absences")]
url = 'https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml'
payload = {"form": "form",
"formlargeurDivCenter": "1608",
"form:sauvegarde": "",
"form:j_idt751:j_idt753_dropdown": "1",
"form:j_idt751:j_idt753_mobiledropdown": "1",
"form:j_idt751:j_idt753_page": "0",
"form:j_idt856:j_idt753_page": "0",
"form:j_idt829_focus": "",
"form:j_idt829_input": "44808",
"javax.faces.ViewState": self.viewstate,
idt: idt # E2 321 E1 76 BJ6D)jfVu
}
result = self.session.post(
url,
headers=dict(referer=url),
data=payload
)
parser = DataParser()
parser.feed(result.text)
data = parser.data[4:]
return [
{"date": data[i], "hours": data[i + 1], "unite_code": data[i + 2], "name": data[i + 3], "prof": data[i + 4],
"type": data[i + 5], "number": data[i + 6], "reason": data[i + 7]}
for i in range(11, len(data), 8)]
def get_appreciations(self):
"""
get appreciations
:param session_requests: requests's session
:param viewstate_value: value of hidden filed viewstate
:return: return data likes [{"year": YEAR, "period": SEMESTER, "appreciation": APPRECIATION}, ....]
"""
idt = self.menu_idt[self.menu_data.index("Mes Appréciations")]
url = 'https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml'
payload = {"form": "form",
"formlargeurDivCenter": "1608",
"form:sauvegarde": "",
"form:j_idt751:j_idt753_dropdown": "1",
"form:j_idt751:j_idt753_mobiledropdown": "1",
"form:j_idt751:j_idt753_page": "0",
"form:j_idt856:j_idt753_page": "0",
"form:j_idt829_focus": "",
"form:j_idt829_input": "44808",
"javax.faces.ViewState": self.viewstate,
idt: idt # E2 321 E1 76 BJ6D)jfVu
}
result = self.session.post(
url,
headers=dict(referer=url),
data=payload
)
parser = DataParser()
parser.feed(result.text)
data = parser.data[12:]
return [{"year": data[i], "period": data[i + 1], "appreciation": data[i + 2]} for i in
range(3, len(data), 3)]
def getGroupsFirstPage(self, session_requests, viewstate_value):
"""
Get first page of groups
:param session_requests: requests's session
:param viewstate_value: value of hidden filed viewstate
:return: return data likes ["16_E2_ESP_2003_S2_1, ...]
"""
idt = self.menu_idt[self.menu_data.index("Mes Groupes")]
url = 'https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml'
payload = {"form": "form",
"formlargeurDivCenter": "1691",
"form:headerSubview:j_idt46": "44807",
"form:j_idt168-value": "false",
"javax.faces.ViewState": viewstate_value,
"form:Sidebar:j_idt" + idt: "form:Sidebar:j_idt" + idt # E2 321 E1 76 BJ6D)jfVu
}
result = session_requests.post(
url,
headers=dict(referer=url),
data=payload
)
parser = DataParser()
parser.feed(result.text)
return parser.data[2:-1]
def getGroupsSecondPage(self, session_requests, viewstate_value):
"""
Get seconde page of groups
:param session_requests: requests's session
:param viewstate_value: value of hidden filed viewstate
:return: return data likes ["16_E2_ESP_2003_S2_1, ...]
"""
url = 'https://webaurion.esiee.fr/faces/ChoixDonnee.xhtml'
payload = {"form": "form",
"formlargeurDivCenter": "729",
"form:headerSubview:j_idt46": "44807",
"form:j_idt168-value": "false",
"javax.faces.ViewState": viewstate_value,
"javax.faces.source": "form:bas",
"javax.faces.partial.event": "rich:datascroller:onscroll",
"javax.faces.partial.execute": "form:bas @component",
"javax.faces.partial.render": "@component",
"form:bas:page": "2",
"org.richfaces.ajax.component": "form:bas",
"form:bas": "form:bas",
"rfExt": "null",
"AJAX:EVENTS_COUNT": "1",
"javax.faces.partial.ajax": "true"
}
result = session_requests.post(
url,
headers=dict(referer=url),
data=payload
)
parser = DataParser()
parser.feed(result.text)
return parser.data[2:-1]
def get_unites_and_groups_from_csv(self, mail):
groups = []
url = "http://test.wallforfry.fr/BDE_MES_GROUPES.csv"
response = requests.get(url)
# f = response.content.decode("ISO-8859-1")
f = response.content.decode("utf-8")
fieldsnames = ["login.Individu", "Coordonnée.Coordonnée", "Code.Groupe"]
data = csv.DictReader(f.splitlines(), fieldsnames, delimiter=';')
for row in data:
if mail in row.get(fieldsnames[1]):
groups.append(row.get(fieldsnames[2]))
elif mail in row.get(fieldsnames[0]):
groups.append(row.get(fieldsnames[2]))
if len(groups) == 0:
raise PersoException("Wrong credentials")
else:
return groups
if __name__ == "__main__":
aurion = Aurion()
print(aurion.get_unites_and_groups_from_csv("delevacw"))