Commit 26e24b76 authored by Gabriel Dengler's avatar Gabriel Dengler
Browse files

New fallback mechanism

parent c5892627
......@@ -128,47 +128,20 @@ class Connection:
dom = fromstring(oauth_rq.text)
saml_res = dom.xpath('//input[@name=\'SAMLResponse\']/@value')[0]
payload = {'SAMLResponse': saml_res}
saml_rq = self.s.post(SAML_URL, data=payload)
self.s.post(SAML_URL, data=payload)
return True
def __get_grades_overview_page(self):
while True:
try:
return self.__get(self.url_exam_results).content
except Exception:
print_warn('connection lost, reconnecting:')
try:
while not self.__connect():
pass
except Exception as e:
print_warn(e)
time.sleep(1)
return self.__get(self.url_exam_results).content
def __get_course_page(self, base_url, node_id, payload):
while True:
try:
return self.__get(base_url,
data={'nodeID': node_id, **payload}).content
except Exception as e:
print(e)
print_warn('connection lost, reconnecting:')
try:
while not self.__connect():
pass
except Exception as e:
print_warn(e)
time.sleep(1)
return self.__get(base_url,
data={'nodeID': node_id, **payload}).content
def __get_grades_for_course(self, course_html):
rows = None
while not rows:
try:
rows = BeautifulSoup(course_html, 'html5lib') \
.find('table', attrs={'id': 'notenspiegel'}) \
.find('tbody').find_all('tr')
except Exception as e:
print_warn(e)
self.__connect()
rows = BeautifulSoup(course_html, 'html5lib') \
.find('table', attrs={'id': 'notenspiegel'}) \
.find('tbody').find_all('tr')
def p(list): return [' '.join(s.get_text().split()) for s in list]
......@@ -191,32 +164,40 @@ class Connection:
def get_grades(self):
# Case 1: No overview page, overview page is results page
overview_page = self.__get_grades_overview_page()
if 'selectStg' not in str(overview_page):
return self.__get_grades_for_course(overview_page)
# Case 2: Multiple pages
base_url = BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.get('action')
courses = [ele.get('value') for ele in \
BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.findAll('input', attrs={'name': 'nodeID'})]
payload = {ele.get('name'): ele.get('value') for ele in \
BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.findAll('input', attrs={'type': 'hidden'})}
modules = []
for course in courses:
course_html = self.__get_course_page(base_url, course, payload)
modules += self.__get_grades_for_course(course_html)
return modules
while True:
try:
# Case 1: No overview page, overview page is results page
overview_page = self.__get_grades_overview_page()
if 'selectStg' not in str(overview_page):
return self.__get_grades_for_course(overview_page)
# Case 2: Multiple pages
base_url = BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.get('action')
courses = [ele.get('value') for ele in \
BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.findAll('input', attrs={'name': 'nodeID'})]
payload = {ele.get('name'): ele.get('value') for ele in \
BeautifulSoup(overview_page, 'html5lib') \
.find('form', attrs={'id': 'selectStg'}) \
.findAll('input', attrs={'type': 'hidden'})}
modules = []
for course in courses:
course_html = self.__get_course_page(base_url, course, payload)
modules += self.__get_grades_for_course(course_html)
return modules
except Exception as e:
print_warn(e)
time.sleep(5)
self.__connect()
def find_exam_results_url(page):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment