-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedt.py
211 lines (182 loc) · 6.97 KB
/
edt.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
import time
from selenium import webdriver
import selenium.webdriver.support.ui as ui
import json
import credentials_window
from event import *
class EDT():
def __init__(self, debugging=False):
self.driver = ''
self.username = ''
self.password = ''
self.schedule = []
self._wait = ui.WebDriverWait(self.driver, 10)
self._debugging = debugging
def access_edt(self):
print('Starting Google Chrome')
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
self.driver = webdriver.Chrome(chrome_options=options)
website = 'https://scolarite.ec-lyon.fr/'
self.driver.get(website)
print('\nAccessing website: ' + website + ' ...')
if self.driver.title == 'Connexion - SSO - École Centrale de Lyon':
self.connection()
if self.driver.title != 'Scolarité':
self.vpn_wrong()
self.access_edt_tab()
def get_credentials(self):
credentials_window.Credentials(self)
def pass_credentials(self, a, b):
self.username = a
self.password = b
self.connect()
def connect(self):
id = self.driver.find_element_by_id('username')
id.clear()
id.send_keys(self.username)
pwd = self.driver.find_element_by_id('password')
pwd.clear()
pwd.send_keys(self.password)
pwd.submit()
if self.driver.title == 'Connexion - SSO - École Centrale de Lyon':
self.wrong_password()
def wrong_password(self):
print('\nOups... Something went wrong')
print('Please check your credentials')
self.get_credentials()
def connection(self):
if self.driver.title == 'Connexion - SSO - École Centrale de Lyon':
print('\nConnecting...')
self.get_credentials()
def vpn_wrong(self):
print('\nOups... Something went wrong')
print('Make sure you are connected to Centrale\'s VPN')
self.driver.quit()
exit()
def access_edt_tab(self):
print('\nAccessing EDT tab...')
edt_tab = self.driver.find_element_by_name('lien_3')
edt_tab.click()
time.sleep(2)
self._wait.until(self.edt_loading)
time.sleep(0.1)
def get_current_week(self):
weeks = self.driver.find_element_by_id("numsemaine")
current_week = weeks.find_element_by_xpath("//option[@selected]")
return current_week.get_attribute("value")
def get_max_value(self):
weeks = self.driver.find_element_by_id("numsemaine")
options = weeks.find_elements_by_tag_name("option")
return options[-1].get_attribute("value")
def get_edt_week(self, id):
print('\nGetting week #' + str(id) + ' ...')
weeks = self.driver.find_element_by_id("numsemaine")
week_to_select = weeks.find_element_by_xpath(
"//option[@value=" + str(id) + "]")
# date format at the beginning '0 (27/08/2018) '
# at the end '2018-08-27'
date = week_to_select.text.split(' ')[1]
date = date[1:-1].split('/')
date.reverse()
date = '-'.join(date)
week_to_select.click()
print('Starting on ' + date)
return date
def save_source(self, file_name):
print('\nSaving page source as ' + file_name + ' ...')
content_page = self.driver.page_source
with open(file_name, 'w') as f:
f.write(content_page)
def extract_edt(self):
current_week_id = int(self.get_current_week())
max_id = int(self.get_max_value())
if self._debugging:
max_id = current_week_id + 5 # we don't need to look at all the schedules
for id in range(current_week_id, max_id + 1):
first_date = self.get_edt_week(id)
self._wait.until(self.edt_loading)
self.extract_week_edt()
self.driver.quit()
def edt_loading(self, driver):
element = self.driver.find_element_by_id('progress_abs')
style = element.get_attribute('style')
return 'display: none;' in style
def extract_week_edt(self):
rows_edt = self.driver.find_elements_by_css_selector(
'.InfoCentreListeEDT')
week_events = []
for row_edt in rows_edt:
event = Event(row_edt)
event.extract_event()
event.format_event()
if event.title != '':
week_events.append(event)
if len(week_events) > 0:
self.group_events(week_events)
def group_events(self, week_events):
i = 0
event = week_events[i]
while i < len(week_events) - 1:
next_event = week_events[i + 1]
if event.title == next_event.title and \
event.location == next_event.location:
event.merge_events(next_event)
else:
self.schedule.append(event)
event = week_events[i + 1]
i += 1
if len(week_events) > 0 and i < len(week_events):
self.schedule.append(event)
def list_per_day(self):
# create a list of events per day
self.events_per_day = {}
for event in self.schedule:
key = event.start_datetime.strftime('%Y-%m-%d')
if key not in self.events_per_day.keys():
self.events_per_day[key] = []
self.events_per_day[key].append(event)
def create_json(self):
output = []
for event in self.schedule:
output.append(event.to_dict())
return output
def json_serial(self, obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, (datetime)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
def save(self, file_name):
content = self.create_json()
with open(file_name, 'w') as f:
json.dump(content, f, default=self.json_serial, indent="\t")
def open(self, file_name):
print('Loading ' + file_name + ' ...')
with open(file_name, 'r') as f:
content = json.load(f)
for event in content:
evt = Event()
evt.from_dict(event)
self.schedule.append(evt)
print('... ' + file_name + ' loaded')
def create_from_google(self, events):
for event in events:
evt = Event()
evt.from_google_event(event)
self.schedule.append(evt)
def compare_edt(self, edt):
new = []
for event in self.schedule:
if event not in edt.schedule:
new.append(event)
old = []
for event in edt.schedule:
if event not in self.schedule:
old.append(event)
return new, old
def __repr__(self):
return str(self.schedule)
if __name__ == '__main__':
edt = EDT()
# print(len(edt))