forked from wallforfry/ADE-ESIEE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar_api.py
253 lines (213 loc) · 9 KB
/
calendar_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
"""
Project : ADE-ESIEE
File : calendar_api.py
Author : DELEVACQ Wallerand
Date : 21/03/2017
"""
import json
import urllib.request
import urllib.error
import re
from unites_api import search_unite, search_unite_from_csv
from planif_parser import ics_to_json_from_ade
import aurion_api
class ADECalendar():
"""
ADECalendar class allow to get ESIEE Calendar from https://bde.esiee.fr/api/calendar/activities api
Attributes :
all_cours : all element provided by the api
groups_unites : list of dict {'unite' : UNITE, 'groupe': GROUPE}
"""
all_cours = []
groups_unites = []
def __init__(self):
'''
Get and set event to all_cours variable
'''
self.all_cours = ics_to_json_from_ade()
def get_cours_of(self, day, month):
"""
Main method
:param day: day
:param month: month
:return: cours of day and month
"""
data = self.all_cours
all = self.get_cours_by_unites_and_groups(data, self.groups_unites)
all = self.get_cours_by_month(all, month)
all = self.get_cours_by_day(all, day)
return [{"name": elt['name'], "start": elt['start'], "end": elt['end'], "rooms": elt["rooms"][0],
"prof": self.prof_finder(elt), "unite": self.unite_name_finder(elt["name"])} for elt in
all]
def get_all_cours(self):
"""
Method to get all cours
:return: all cours
"""
data = self.all_cours
all = self.get_cours_by_unites_and_groups(data, self.groups_unites)
return [{"name": elt['name'], "start": elt['start'], "end": elt['end'], "rooms": elt["rooms"][0],
"prof": self.prof_finder(elt), "unite": self.unite_name_finder(elt["name"]),
"description": elt['description']} for elt in
all]
def is_group(self, description, name, groupe):
'''
:param description: description of event
:param name: name of unite
:param groupe: groupe of this unite
:return: boolean if this group correspond to the unite event
'''
back = [m.start() for m in re.finditer("\n", description)]
startLine = 1
if (len(description[:back[1]]) > 5):
startLine = 1
real_group = description[back[startLine]:description.find(name[:name.find(":")]) - 1]
if str(groupe) in real_group:
return True
else:
return False
def has_this_cours(self, cours, groups_and_unites):
'''
:param cours: cours row
:param groups_and_unites: groupes and unites
:return: boolean if this cours is in list of groups and unites
'''
for elt in groups_and_unites:
if elt['unite'] is not None:
if (elt['unite'][:elt['unite'].find("-")] in cours['name']) and (
elt['unite'][elt['unite'].find("-") + 1:] in cours['name']):
if self.is_group(cours['description'], cours['name'], elt['groupe']):
return True
return False
def get_cours_by_unites_and_groups(self, cours, groups_and_unites):
'''
:param cours: cours database
:param groups_and_unites: groupes and unites to find
:return: cours where groups and unites correspond
'''
return [elt for elt in cours if self.has_this_cours(elt, groups_and_unites)]
def get_cours_by_month(self, cours, month):
'''
:param cours: cours database
:param month: month which must be find
:return: cours of the month
'''
return [elt for elt in cours if elt['start'][5:7] == month]
def get_cours_by_day(self, cours, day):
'''
:param cours: cours database
:param day: day which must be find
:return: cours of the day
'''
return [elt for elt in cours if elt['start'][8:10] == day]
def set_groups_unites(self, aurion_data):
'''
:param aurion_data: list of groups provided by aurion_api
:return: set groups_unites formated as list of dict {'unite' : UNITE, 'groupe': GROUPE}
'''
self.groups_unites = []
for data in aurion_data:
groups = self.groups_finder(data)
for group in groups:
self.groups_unites.append(
{"unite": self.format_unites(self.unites_finder(data)), "groupe": group})
def groups_finder(self, data):
'''
:param data: row of aurion provided data
:return: list of different possible cases of group number
'''
back = [m.start() for m in re.finditer("_", data[::-1])]
if "EIG_2022" in data:
real_group = data[len(data) - back[0]:]
return [real_group, real_group[0].upper() + real_group[1:].lower(), real_group[0].lower() + real_group[1:].upper(), real_group.upper(), real_group.lower()]
if "EIG" in data:
return data[len(data) - 3:len(data) - 2] + data[len(data) - 2:len(data) - 1].lower() + data[len(data):]
if "PR_3001" in data and len(back) > 4:
real_group = data[len(data) - back[0]:].replace("_", "-")
return [real_group, real_group[0].upper() + real_group[1:].lower(),
real_group[0].lower() + real_group[1:].upper(), real_group.upper(), real_group.lower()]
if "PR" in data and len(back) > 4:
real_group = data[len(data) - back[1]:].replace("_", "-")
return [real_group, real_group[0].upper() + real_group[1:].lower(),
real_group[0].lower() + real_group[1:].upper(), real_group.upper(), real_group.lower()]
if "EN3" in data:
real_group = data[len(data) - back[0]:].replace("_", "-")
if len(real_group) >= 2:
return [real_group, real_group[0].upper() + real_group[1:].lower(),
real_group[0].lower() + real_group[1:].upper(), real_group.upper(), real_group.lower()]
else:
return ["xx"]
if len(back) <= 1:
# return [data[data.find("_")+1:]]
return ["", data[data.find("_") + 1:]]
real_group = data[len(data) - back[0]:]
if len(real_group) >= 2:
return [real_group, real_group[0].upper() + real_group[1:].lower(),
real_group[0].lower() + real_group[1:].upper(), real_group.upper(), real_group.lower()]
# elif not "EN3" in data:
else:
return [real_group, real_group.upper(), real_group.lower()]
def unites_finder(self, data):
'''
:param data: row of aurion provided data
:return: unite name
'''
back = [m.start() for m in re.finditer("_", data)]
real_unites = data[:] # back[0]+1
return real_unites
def format_unites(self, data):
'''
:param data: row of aurion provided data
:return: unite name formatted likes unite name in calendar api
'''
back = [m.start() for m in re.finditer("_", data)]
if len(back) == 1:
real_group = data[back[0] + 1:]
real_group = real_group.replace("_", "-")
# Correction pour les noms de promos
real_group += ":"
return real_group
if len(back) == 3: # 16_E4FR_RE4R23_2R
real_group = data[back[1] + 1:back[2]]
real_group = real_group.replace("_", "-")
return real_group
if len(back) == 4: # 16_E2_IGE_2102_2
real_group = data[back[1] + 1:back[3]]
real_group = real_group.replace("_", "-")
return real_group
if len(back) == 5: # 16_E2_ESP_2003_S2_2
if "PR" in data:
real_group = data[back[1] + 1:back[3]]
real_group = real_group.replace("_", "-")
else:
real_group = data[back[1] + 1:back[4]]
real_group = real_group.replace("_", "-")
return real_group
def prof_finder(self, data):
'''
:param data: row of aurion provided data
:return: professors names
'''
description = data["description"]
exp = description.find("(Expor") - 1 #Gère "Exported" ou "Exporté"
aurion = description.find("AURION") + len("AURION") + 1
return description[aurion:exp]
def unite_name_finder(self, data):
'''
:param data: row of aurion provided data
:return: unite natural name
'''
# for unite_name in self.groups_unites:
# #print(unite_name["unite"]+" "+data[:data.find(":")])
# if unite_name["unite"] == data[:data.find(":")]:
# return unite_name["name"]
return search_unite_from_csv(data[:data.find(":")])
if __name__ == "__main__":
aurion = aurion_api.Aurion()
ade = ADECalendar()
ade.set_groups_unites(aurion.get_unites_and_groups_from_csv("delevacw"))
print(ade.groups_unites)
#test = ade.groups_finder("17_E2_IGE_2101_5")
#print(test)
#print(ade.format_unites("17_E3_PR_3001_R_R2"))
pass