forked from TheJordanDev/360-Schedule-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
101 lines (91 loc) · 3.56 KB
/
utils.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
import re
from objects import EDTTime
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
def get_cours_hour(cours: WebElement) -> int:
slope = 10.5555
intercept = 15.1505
percentage = cours.value_of_css_property('top')
if percentage:
return round((float(percentage.split('.')[0]) - intercept) / slope)
return 0
def get_cours_duration(cours: WebElement) -> int:
slope = 10.55555
intercept = -0.8
style = cours.get_attribute('style')
if style:
match = re.search(r'height:\s*([\d.]+)%', style)
if match:
height = float(match.group(1))
return round((height + intercept) / slope)
return 0
def get_day_by_left(cours: WebElement) -> int:
style = cours.get_attribute('style')
if style:
match = re.search(r'left:\s*([\d.]+)%', style)
if match:
left = float(match.group(1))
if 103 <= left < 122:
return 0
elif 122 <= left < 141:
return 1
elif 141 <= left < 161:
return 2
elif 161 <= left < 180:
return 3
elif 180 <= left < 200:
return 4
print(f"Error: Could not find day by left: {style}")
return 0
def get_day_by_index(driver: Firefox, index: int) -> str:
todays_index = index + 5
day = driver.find_elements(By.CSS_SELECTOR, "div.Jour")[todays_index]
if day:
tc_jour = day.find_element(By.CSS_SELECTOR, "td.TCJour")
return tc_jour.text
return ""
def get_cours_salle(cours: WebElement) -> str:
try:
salle_element = cours.find_element(By.CSS_SELECTOR, "td.TCSalle")
salle = salle_element.text.strip()
if not salle:
print(f"Salle element found but text is empty: {salle_element}")
return salle
except Exception as e:
print(f"An error occurred while locating the salle element: {e}")
return ""
def get_cours_info(cours: WebElement) -> tuple:
try:
prof = cours.find_element(By.CSS_SELECTOR, "td.TCProf")
inner_html = prof.get_attribute('innerHTML')
inner_html = re.sub(r'<img[^>]*>', '', inner_html)
inner_html = re.sub(r'<span[^>]*>.*?<\/span>', '', inner_html)
profs = inner_html.split("<br>")
prof_name = profs[0].strip() if len(profs) > 0 else ""
promo = profs[1].strip() if len(profs) > 1 else ""
return prof_name, promo
except Exception as e:
print(f"An error occurred while locating the prof element: {e}")
return "", ""
def get_cours_start_and_end_time(cours: WebElement) -> tuple:
try:
time_element = cours.find_element(By.CSS_SELECTOR, "td.TChdeb")
times = time_element.text.split(" - ")
start_hour, start_minute = times[0].strip().split(":")
end_hour, end_minute = times[1].strip().split(":")
return EDTTime(int(start_hour), int(start_minute)), EDTTime(int(end_hour), int(end_minute))
except Exception as e:
if type(e) == ValueError and "not enough values to unpack" in str(e):
print(time_element.text)
return None, None
print(f"An error occurred while locating the time element: {e}")
return None, None
def get_cours_name(cours: WebElement) -> str:
try:
t_case = cours.find_element(By.CSS_SELECTOR, "td.TCase")
if t_case:
return t_case.text
except Exception as e:
print(f"An error occurred while locating the name element: {e}")
return ""