Skip to content

Commit 23b7984

Browse files
committed
feat: changed exam schedule return type
1 parent d2ba858 commit 23b7984

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/exam_schedule.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
from typing import List, Dict
12
from .constants import *
23
import time
34
from .parsers import exam_schedule_parser
45

5-
def get_exam_schedule(session, username: str, semesterSubId: str, csrf_token) -> dict:
6+
def get_exam_schedule(session, username: str, semesterSubId: str, csrf_token) -> List[Dict]:
67
"""
78
Retrieves the exam schedule for a specific user and semester.
89
910
This function sends two HTTP POST requests to the VTOP system. The first request verifies the user's session,
1011
and the second request retrieves the exam schedule for the specified semester. The HTML content is parsed using
11-
a custom parser to extract and organize the exam schedule details into a dictionary.
12+
a custom parser to extract and organize the exam schedule details into a list of dictionaries.
1213
1314
Parameters:
1415
session (requests.Session):
@@ -21,8 +22,8 @@ def get_exam_schedule(session, username: str, semesterSubId: str, csrf_token) ->
2122
The CSRF token required to authenticate the request.
2223
2324
Returns:
24-
dict:
25-
A dictionary containing the parsed exam schedule details, including course codes, exam dates, and timings.
25+
list[dict]:
26+
A list containing the parsed exam schedule details, including course codes, exam dates, and timings.
2627
"""
2728
data = {
2829
"verifyMenu": "true",

src/parsers/exam_schedule_parser.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
from typing import List, Dict
12
from bs4 import BeautifulSoup
3+
from collections import defaultdict
24

35

4-
def parse_exam_schedule(html: str) -> dict:
6+
def parse_exam_schedule(html: str) -> List[Dict]:
57
soup = BeautifulSoup(html, "html.parser")
68
schedule_table = soup.find("table")
79

810
if not schedule_table:
9-
return {"error ": "No timetable found"}
11+
return [{"error": "No timetable found"}]
1012

1113
rows = schedule_table.find_all("tr")
12-
exam_schedule = {}
14+
exam_schedule = defaultdict(list)
1315
current_exam_type = None
1416

1517
for row in rows:
@@ -19,13 +21,13 @@ def parse_exam_schedule(html: str) -> dict:
1921
# Skip rows that don't contain enough values or are headers
2022
if len(values) == 1:
2123
current_exam_type = values[0]
22-
exam_schedule[current_exam_type] = {}
2324
continue
2425
elif len(values) < 13 or "S.No." in values:
2526
continue
2627

2728
# Construct the data dictionary
2829
exam_entry = {
30+
"serial_number": values[0],
2931
"course_code": values[1],
3032
"course_title": values[2],
3133
"type": values[3],
@@ -40,7 +42,17 @@ def parse_exam_schedule(html: str) -> dict:
4042
"seat_number": values[12],
4143
}
4244

43-
# Add the entry to the dictionary under the current exam type
44-
exam_schedule[current_exam_type][values[0]] = exam_entry
45+
# Add the entry to the list under the current exam type
46+
if current_exam_type:
47+
exam_schedule[current_exam_type].append(exam_entry)
4548

46-
return exam_schedule
49+
# Convert defaultdict to a list of dictionaries
50+
result = [
51+
{
52+
"exam_type": exam_type,
53+
"subjects": subjects
54+
}
55+
for exam_type, subjects in exam_schedule.items()
56+
]
57+
58+
return result

0 commit comments

Comments
 (0)