-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport_functions.py
166 lines (132 loc) · 7.04 KB
/
support_functions.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
from bs4 import BeautifulSoup
import requests
import re
from concurrent.futures import ThreadPoolExecutor
from collections import defaultdict
class SupportFunctions:
def __init__(self):
self.url_base, self.url_login, self.url_teachers, self.url_result = (
"https://qalam.nust.edu.pk",
"https://qalam.nust.edu.pk/web/login",
"https://qalam.nust.edu.pk/student/enrolled/courses",
"https://qalam.nust.edu.pk/student/results/"
)
self.csrf_token = lambda x: x.find("input", {"name": "csrf_token"})["value"]
def auth(self, username, password):
session = requests.Session()
r = session.get(self.url_login)
soup = BeautifulSoup(r.text, "html.parser")
csrf_token = self.csrf_token(soup)
login_data = {"csrf_token": csrf_token, "login": username, "password": password}
r = session.post(self.url_login, data=login_data)
session.close()
if r.status_code == 200:
soup = BeautifulSoup(r.text, "html.parser")
name = soup.find('span', {'class': 'uk-text-truncate'}).text.strip()
return True, name
return False, None
@staticmethod
def fetch_previous_terms(soup):
result = {} # {term: [{course: [credits, aggregate, grade]}, ...], ...}
temp = soup.find('ul', {'id': 'tabs_anim1'}).find_all('li')[-1]
for term in temp.find_all('tr', {'class': 'table-parent-row show_child_row'}):
term_name = term.find('a').text.strip().lower()
term_details = []
temp = term.find_next_sibling('tr').find_next_sibling('tr')
while temp and temp.get('class') == ['table-child-row']:
sub_details = list(temp.find_all('td'))
course_name = sub_details[0].text.strip()
course_credits = sub_details[1].text.strip()
course_aggregate = sub_details[2].text.strip()
course_grade = sub_details[5].text.strip()
term_details.append({course_name: [course_credits, course_aggregate, course_grade]})
temp = temp.find_next_sibling('tr')
result[term_name] = term_details
return result
def fetch_all_details(self, username, password):
session = requests.Session()
# Log in to the system
r = session.get(self.url_login)
soup = BeautifulSoup(r.text, "html.parser")
csrf_token = self.csrf_token(soup)
login_data = {"csrf_token": csrf_token, "login": username, "password": password}
session.post(self.url_login, data=login_data)
r = session.get(self.url_result)
soup = BeautifulSoup(r.content, "html.parser")
# Get Previous Terms
previous_terms = self.fetch_previous_terms(soup)
# Get Current Term
cur_subs = soup.find_all('a', {'data-uk-tooltip': '{pos:\'top\'}'})
all_subs = list(map(lambda x: (self.url_base + x.get('href'), session,
x.find('span', {'class': 'md-list-heading'}).text.strip(),
x.find_next_sibling("div").find('span',
{'class': 'md-list-heading'}).text.strip()
), cur_subs))
with ThreadPoolExecutor() as executor:
results = executor.map(self.result, list(all_subs))
cur_sub_details = list(results)
# Get Teacher Names
teachers = self.get_teacher_names(session)
session.close()
return previous_terms, cur_sub_details, teachers
def get_teacher_names(self, session):
r = session.get(self.url_teachers)
soup = BeautifulSoup(r.text, "html.parser")
result = {}
for img in soup.find_all('img', {'data-uk-tooltip': "{pos:'top'}"}):
sub = img.find_parent('div').find_parent('div').find_next_sibling('div').find('span', {
'class': 'md-list-heading'}).text.strip()
result[sub.lower()] = img['title'].lower()
return result
@staticmethod
def result(inputs):
url, session, name, course_credits = inputs
results = {'name': name.lower(),
'credits': course_credits} # {name, credits, quiz, quiz_avg, assign, assign_avg, mid_avg, final, final_avg, lab, lab_avg, project, project_avg}
soup = BeautifulSoup(session.get(url).text, features='html.parser')
temp_score = defaultdict(list)
class_avg = defaultdict(list)
exam_list = soup.find_all('a', class_="js-toggle-children-row")
for exam in exam_list:
score_row = exam.find_next('tr')
while score_row:
score_row = score_row.find_next('tr', class_='table-child-row')
if not score_row or score_row.findChild('th'):
break
subject_details = score_row.find_all('td')
max_marks = float(subject_details[1].text.strip())
avg = float(subject_details[3].text.strip())
score = float(subject_details[4].text.strip())
temp_score[exam.text.strip()].append(score)
class_avg[exam.text.strip()].append(round(avg * 100 / max_marks, 2))
temp_score[exam.text.strip()].append(
float(re.findall(r"(.*)(?:\s*</td>, '\\n'])", str(score_row.contents))[0].strip()))
for key in temp_score.keys():
if 'quiz' in key.lower():
results['quiz'] = sum(temp_score[key]) / len(temp_score[key])
results['quiz_avg'] = sum(class_avg[key]) / len(class_avg[key])
elif 'assignment' in key.lower():
results['assign'] = sum(temp_score[key]) / len(temp_score[key])
results['assign_avg'] = sum(class_avg[key]) / len(class_avg[key])
elif 'mid term' in key.lower() or 'one hour' in key.lower():
results['midterm'] = sum(temp_score[key]) / len(temp_score[key])
results['midterm_avg'] = sum(class_avg[key]) / len(class_avg[key])
elif 'lab work' in key.lower():
results['lab'] = sum(temp_score[key]) / len(temp_score[key])
results['lab_avg'] = sum(class_avg[key]) / len(class_avg[key])
elif 'lab' in key.lower() or 'project' in key.lower():
results['project'] = sum(temp_score[key]) / len(temp_score[key])
results['project_avg'] = sum(class_avg[key]) / len(class_avg[key])
elif 'final' in key.lower():
results['finals'] = sum(temp_score[key]) / len(temp_score[key])
results['finals_avg'] = sum(class_avg[key]) / len(class_avg[key])
for k in ['quiz', 'assign', 'midterm', 'lab', 'project', 'finals']:
if k not in results.keys():
results[k] = 0
results[k + '_avg'] = 0
return results
if __name__ == '__main__':
# Create an instance of the class
obj = SupportFunctions()
# Fetch all details
obj.fetch_all_details('aaleem.bscs21seecs', 'Student.123')