-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
356 lines (286 loc) · 15.4 KB
/
app.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import aiohttp
import asyncio
from fastapi import FastAPI, Form
from typing_extensions import Annotated
from datetime import datetime
from bs4 import BeautifulSoup, Tag
from datetime import datetime
import os
import re
import random
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
client_url = os.environ.get('CLIENT_URL')
print(f'Client url: {client_url}')
# allow client url to access the api
@app.middleware("http")
async def cors(request, call_next):
response = await call_next(request)
response.headers["Access-Control-Allow-Origin"] = client_url
return response
default_parser = 'html.parser'
emojis = {
'😊',
'😉',
'😎',
'😍',
'😘',
'😗',
'😙',
'👾',
'👽',
'👻',
'👺',
'🐳',
'😁',
'😆',
'😑',
'😐',
'😪',
'😴',
'😖',
'😞',
'😭',
'😢',
'😵',
'😵💫',
'😷',
'🥺',
'🥹'
}
@app.get('/')
async def home():
#return a random emoji
random_emoji = random.choice(list(emojis))
return {'Client': client_url, 'message': f'Version 2: {random_emoji}'}
@app.post('/')
async def forward_request(user_name: Annotated[str, Form(...)], password: Annotated[str, Form(...)]):
print('Processing request...')
url = 'https://portal.aiub.edu'
async with aiohttp.ClientSession() as session:
async with session.post(url, data={'UserName': user_name, 'Password': password}) as resp:
if resp.status != 200:
return {'success': False, 'message': 'Error in request'}
if 'https://portal.aiub.edu/Student' not in str(resp.url):
return {'success': False, 'message': 'Invalid username or password'}
if 'Student/Tpe/Start' in str(resp.url):
print('Evaluation pending')
return {'success': False, 'message': 'TPE Evaluation pending on portal'}
print('Login successful')
async with session.get('https://portal.aiub.edu/Student') as res:
soup = BeautifulSoup(await res.text(), default_parser)
targets = soup.select("#SemesterDropDown > option")
user = soup.select_one('.navbar-link').text
# if user has , in his name, then split it by , and then reverse it
if ',' in user:
user = user.split(',')
user = user[1].strip() + ' ' + user[0].strip()
user = user.title()
current_semester = soup.select_one('#SemesterDropDown > option[selected="selected"]').text
semester_class_routine = {}
tasks = [process_semester(session, target) for target in targets]
result = await asyncio.gather(get_curricumn_data(session), get_completed_courses(session, current_semester), *tasks)
course_map = result[0]
completed_courses = result[1][0]
current_semester_courses = result[1][1]
pre_registered_courses = result[1][2]
for semester in result[2:]:
semester_class_routine.update(semester)
# sort the semesters by year
semester_class_routine = dict(sorted(semester_class_routine.items(), key=lambda x: x[0]))
#iterate over the gradesMap. A student can take a course after completing the prerequisites. And also if he/she has taken the course before, he must have D grade
unlocked_courses = {}
for course_code, course in completed_courses.items():
#if student has taken the course before and has D grade, then he/she can take the course again
if course['grade'] == 'D':
unlocked_courses[course_code] = {'course_name': course['course_name'], 'credit': course_map[course_code]['credit'], 'prerequisites': course_map[course_code]['prerequisites'], 'retake': True}
for course_code, course in course_map.items():
if course_code in completed_courses:
completed_courses[course_code]['credit'] = course['credit']
continue
# if course code has '#' or '*' then skip
if '#' in course_code or '*' in course_code:
continue
if course['course_name'] == 'INTERNSHIP':
continue
#if the course is already unlocked, then skip it
if course_code in unlocked_courses:
continue
#if the course is in the current semester, but has not been dropped, then skip it
if (course_code in current_semester_courses and course["course_name"] == current_semester_courses[course_code]["course_name"]) and current_semester_courses[course_code]['grade'] not in ['W', 'I', 'UW']:
continue
#if the course is in the pre-registered courses, then add it to the unlocked courses
if course_code in pre_registered_courses:
unlocked_courses[course_code] = {'course_name': course['course_name'], 'credit': course['credit'], 'prerequisites': course['prerequisites'], 'retake': False}
continue
prerequisites = course['prerequisites']
#if the course has no prerequisites, then it is unlocked
if len(prerequisites) == 0:
unlocked_courses[course_code] = {'course_name': course['course_name'], 'credit': course['credit'], 'prerequisites': course['prerequisites'], 'retake': False}
continue
#iterate over the prerequisites. If the student has taken every prerequisite, then the course is unlocked
pre_requisites_met = True
for prerequisite in prerequisites:
if prerequisite not in completed_courses and prerequisite not in current_semester_courses:
pre_requisites_met = False
break
if pre_requisites_met:
unlocked_courses[course_code] = {'course_name': course['course_name'], 'credit': course['credit'], 'prerequisites': course['prerequisites'], 'retake': False}
print('Sending response...')
return {'success': True, 'message': 'Success', 'result': { 'semesterClassRoutine': semester_class_routine, 'unlockedCourses': unlocked_courses, 'completedCourses': completed_courses, 'preregisteredCourses': pre_registered_courses, 'currentSemester': current_semester, 'user': user}}
async def get_completed_courses(session: aiohttp.ClientSession, current_semester: str): #gets all completed and attempted courses from the grade report
# get the completed courses
print('Getting completed courses...')
async with session.get('https://portal.aiub.edu/Student/GradeReport/ByCurriculum') as response:
soup = BeautifulSoup(await response.text(), default_parser)
rows = soup.select('table:not(:first-child) tr:not(:first-child):has(td:nth-child(3):not(:empty))')
# first td contains the course code, second td contains the course name, third td contains the grade
completed_courses = {}
current_semester_courses = {}
pre_registered_courses = {}
for row in rows:
course_code = row.select_one('td:nth-child(1)').text.strip()
course_name = row.select_one('td:nth-child(2)').text.strip()
results = row.select_one('td:nth-child(3)').text.strip()
# Use regular expressions to extract the last result
matches = re.findall(r'\(([^)]+)\)\s*\[([^\]]+)\]', results)
# Check if there are any matches
if len(matches) > 0:
# Get the last match
last_result = matches[-1]
# Extract grade and semester from the last result
semester, grade = last_result
grade = grade.strip()
semester = semester.strip()
else:
continue
#print (f'{courseCode} {courseName} {grade} {semester}')
if grade in ['A+', 'A', 'B+', 'B', 'C+', 'C', 'D+', 'D', 'F']:
completed_courses[course_code] = {'course_name': course_name, 'grade': grade}
#print(f'Completed course: {courseCode} {courseName} {grade} {semester}')
elif grade == '-':
if semester == current_semester:
current_semester_courses[course_code] = {'course_name': course_name, 'grade': grade}
#print(f'Current semester course: {courseCode} {courseName} {grade} {semester}')
else:
pre_registered_courses[course_code] = {'course_name': course_name, 'grade': grade}
#print(f'Pre-registered course: {courseCode} {courseName} {grade} {semester}')
return [completed_courses, current_semester_courses, pre_registered_courses]
async def get_curricumn_data(session: aiohttp.ClientSession):
print('Getting curriculum data...')
# get the curriculum data
async with session.get('https://portal.aiub.edu/Student/Curriculum') as response:
soup = BeautifulSoup(await response.text(), default_parser)
target_elements = soup.select('[curriculumid]')
curricumn_id = []
for target in target_elements:
# attribute value is the curriculum id
curricumn_id.append(target.attrs['curriculumid'])
course_map = {} # will contain the course code as key and {coursename, prerequisit[]} as value
#for ID in curricumnID:
# use asyncio to process the curriculums
tasks = [process_curriculum(ID, session) for ID in curricumn_id]
results = await asyncio.gather(*tasks)
for result in results:
course_map.update(result)
return course_map
async def process_curriculum(id: str, session: aiohttp.ClientSession):
# request the getCurricumnLink?IDd=curriculumId
course_map = {}
#print(f'Getting data for curriculum https://portal.aiub.edu/Common/Curriculum?ID={ID}')
async with session.get(f'https://portal.aiub.edu/Common/Curriculum?ID={id}') as response:
soup = BeautifulSoup(await response.text(), default_parser)
table = soup.select('.table-bordered tr:not(:first-child)')
#print(f'{len(table)} courses extracted')
for course in table:
#print(course)
course_code = course.select_one('td:nth-child(1)').text.strip()
course_name = course.select_one('td:nth-child(2)').text.strip()
credit = course.select_one('td:nth-child(3)').text.strip()
# credit is 3 0 0 0, 1 1 0 0 format. Split it, Sort it and get the largest number as credit
credit = sorted([int(c) for c in credit.split(' ')], reverse=True)[0]
prerequisites = [li.text.strip() for li in course.select('td:nth-child(4) li')]
course_map[course_code] = {'course_name': course_name, 'credit': credit, 'prerequisites': prerequisites}
return course_map
async def process_semester(session: aiohttp.ClientSession, target: Tag):
semester = {}
match = re.search(r'q=(.*)', target.attrs['value'])
if match is not None and len(match.groups()) > 0:
rq_url = 'https://portal.aiub.edu/Student/Registration?q=' + match.group(1)
async with session.get(rq_url) as response:
soup = BeautifulSoup(await response.text(), default_parser)
raw_course_elements = soup.select("table")[1].select("td:first-child")
if len(raw_course_elements) == 0:
return semester
courses_obj = {}
for course in raw_course_elements:
if course.text != '':
course_name = course.select_one("a").text
parsed_course = get_course_details(course_name)
course_times = course.select("div > span")
credit = max(int(c.strip()) for c in course.find_next('td').text.strip().split('-'))
course_data = [
parse_time(time.text) for time in course_times if 'Time' in time.text
]
for parsed_time in course_data:
day = parsed_time['day']
if courses_obj.get(day) is None:
courses_obj[day] = {}
courses_obj[day][parsed_time['time']] = {
'course_name': parsed_course['course_name'],
'class_id': parsed_course['class_id'],
'credit': credit,
'section': parsed_course['section'],
'type': parsed_time['type'],
'room': parsed_time['room']
}
semester[target.text] = courses_obj
return semester
def parse_time(time_string: str):
try:
match = re.findall(r'\d{1,2}:\d{1,2}(?:\s?[ap]m|\s?[AP]M)?', time_string)
day_map = {'Sun': 'Sunday', 'Mon': 'Monday', 'Tue': 'Tuesday', 'Wed': 'Wednesday', 'Thu': 'Thursday', 'Fri': 'Friday', 'Sat': 'Saturday'}
class_type = re.search(r'\((.*?)\)', time_string).group(1)
day = day_map[re.search(r'(Sun|Mon|Tue|Wed|Thu|Fri|Sat)', time_string).group()]
room = re.search(r'Room: (.*)', time_string).group(1)
start_time, end_time = match[0], match[1]
start_time_obj = ''
end_time_obj = ''
#if time contains am/pm or AM/PM
AM_TIME_FORMAT = '%I:%M'
PM_TIME_FORMAT = AM_TIME_FORMAT + ' %p'
if "am" in start_time.lower() or "pm" in start_time.lower():
start_time_obj = datetime.strptime(start_time, PM_TIME_FORMAT)
else:
start_time_obj = datetime.strptime(start_time, AM_TIME_FORMAT)
if "am" in end_time.lower() or "pm" in end_time.lower():
end_time_obj = datetime.strptime(end_time, PM_TIME_FORMAT)
else:
end_time_obj = datetime.strptime(end_time, AM_TIME_FORMAT)
start_time_formatted = start_time_obj.strftime(PM_TIME_FORMAT)
end_time_formatted = end_time_obj.strftime(PM_TIME_FORMAT)
final_time = f"{start_time_formatted} - {end_time_formatted}"
data = {
"type": class_type,
"time": final_time,
"day": day,
"room": room
}
return data
except IndexError:
print("Time not found in the string.")
def get_course_details(course: str):
match = re.match(r"^(\d+)-(.+?)\s+\[([A-Z0-9]+)\](?:\s+\[([A-Z0-9]+)\])?$", course)
try:
if match:
class_id = match.group(1)
course_name = match.group(2).title()
section = match.group(4) if match.group(4) else match.group(3)
return {"class_id": class_id, "course_name": course_name, "section": section}
else:
print("Course not found in the string:", course)
return {"class_id": "", "course_name": "", "section": ""}
except IndexError:
print("Course not found in the string:", course)
return {"class_id": "", "course_name": "", "section": ""}