-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebParser.py
179 lines (127 loc) · 5.34 KB
/
WebParser.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from other import keys_and_strings
def convert_to_cap_greek( s : str ) -> str:
dict_accented_caps = { 'Ό' : 'Ο', 'Ά' : 'Α', 'Ί' : 'Ι', 'Έ' : 'Ε', 'Ύ' : 'Υ', 'Ή' : 'Η', 'Ώ' : 'Ω'}
res = s.upper()
for orig, new in dict_accented_caps.items():
res = res.replace(orig , new)
#print(s + ' -->\n' + res)
return res
class SeleniumWebParser:
def __init__(self):
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
self.driver = webdriver.Chrome(keys_and_strings.PATH_TO_DRIVER, options=chrome_options)
self.driver.set_window_size(width=1280, height=720) # if window too narrow : dropdown doesnt appear !
# todo: headless?? problem with width (left menu) ^^^ ?
def login_website(self, site : int):
# mystudies
if site == 1 :
from other import mycredentials
url = 'https://my-studies.uoa.gr/secr3w/connect.aspx'
elem_usr = 'username'
elem_pass = 'password'
val_usr = mycredentials.hidden_u
val_pass = mycredentials.hidden_p
else : # site == 2:
url = 'https://eclass-sandbox.noc.uoa.gr/'
elem_usr = 'uname'
elem_pass = 'pass'
val_usr = 'stud11'
val_pass = 'teststudpass'
# initiate
self.driver.get(url) # go to the url
# login
username_field = self.driver.find_element_by_name(elem_usr)
password_field = self.driver.find_element_by_name(elem_pass)
username_field.send_keys(val_usr)
password_field.send_keys(val_pass)
password_field.send_keys(Keys.RETURN)
def get_average_grades(self) -> str:
# mystudies : get average grade
self.login_website(1)
sum_grades: float = 0
counter = 0
self.driver.get('https://my-studies.uoa.gr/Secr3w/app/accHistory/default.aspx')
self.driver.switch_to.frame('accmain')
all_tr_rows = self.driver.find_elements_by_xpath('//table/tbody/tr')
for row in all_tr_rows:
if not str(row.text).endswith('\n '):
continue # this row is not a course-grade
td_columns = row.text.split('\n')
course: str = td_columns[0]
course = course[course.find('- ') + 2: course.rfind('(')]
grade: str = td_columns[1]
grade = grade[grade.find('(') + 1: grade.find(')')]
if ',' in grade or '.' in grade:
grade: float = float(grade.replace(',', '.'))
else:
grade: int = int(grade)
if grade < 5:
continue
sum_grades = sum_grades + grade
counter = counter + 1
print("\t__WB__ //mystudies: ", course, '\t= ', grade)
self.driver.close()
# this takes alot of time :: self.driver.quit()
return str( (sum_grades / counter).__round__(2) if counter != 0 else 0)
def get_grade_of(self, param_target_course: str = '') -> str:
self.login_website(1)
# mystudies : get grade
grade: str = ''
self.driver.get('https://my-studies.uoa.gr/Secr3w/app/accHistory/default.aspx')
self.driver.switch_to.frame('accmain')
all_tr_rows = self.driver.find_elements_by_xpath('//table/tbody/tr')
for row in all_tr_rows:
if not str(row.text).endswith('\n '):
continue # this row is not a course-grade
td_columns = row.text.split('\n')
course: str = td_columns[0]
course = course[course.find('- ') + 2: course.rfind('(')]
# string comparison: check if this course == {:param_target_course}
if param_target_course.upper() in convert_to_cap_greek(course):
grade = td_columns[1]
grade = grade[grade.find('(') + 1: grade.find(')')]
print("\t__WB__ //mystudies found : ", param_target_course, '\t= ', grade)
break
self.driver.close()
return grade
def get_eclass_element(self, type_element, param_target_course: str = '') -> str:
self.login_website(2)
# eclass : get anakoinwseis + ergasies + plhrofories ma8hmatos
# get list of courses from main page
webelem_courses = self.driver.find_elements_by_xpath('//table/tbody/tr/td/b/a')
# #webelem_courses = self.driver.find_elements_by_class_name('text-left')
# (string comparison) click on the course with name == [ most similar to the string parameter {:param_target_course} ]
# https://www.datacamp.com/community/tutorials/fuzzy-string-python
for c in webelem_courses:
if convert_to_cap_greek(param_target_course) in convert_to_cap_greek(c.text):
c.click()
w_side_categories = self.driver.find_elements_by_class_name('list-group-item')
if w_side_categories is None:
print("!course: |"+ param_target_course+"| no side category=", type_element)
self.driver.close()
return 'not-found'
result : str
# indexes ::: 0=anakoinwseis 1=ergasies 2=ergasies 5=plhrofories
w_side_categories[type_element].click()
self.driver.implicitly_wait(0.7)
if type_element == 0:
#latest anouncement
elem = self.driver.find_elements_by_xpath("//*[@id=\"ann_table3\"]/tbody/tr[1]/td[1]/div")
announcement : str = elem[0].text
elem = self.driver.find_elements_by_xpath("//*[@id=\"ann_table3\"]/tbody/tr[1]/td[2]")
date_of_announcement =elem[0].text
result = date_of_announcement + " :\n " + announcement.replace('\n' , ' ')
if type_element == 1:
#latest deadline
pass
self.driver.close()
return result
if __name__ == "__main__":
wb = SeleniumWebParser()
test = wb.get_eclass_element( 0 , 'Εισαγωγή στον Προγραμματισμό' )
print("=" + test)
#print("\n\n", wb.get_average_grades(), "/10") # ok