Skip to content

Commit 8069b43

Browse files
authored
Simpler and bug free loop for parsing erp response (#141)
* checking for emptyness of a cell before parsing it * rewrote a simpler and bug free loop for parsing the erp response. It was not handling end of day courses before * deleted extra files * rewrote a simpler and bug free loop for parsing the erp response. It was not handling end of day courses before * deleted extra files * synced branch with metakgp-master * removed redundant if condition while checking for empty cells
1 parent f493c02 commit 8069b43

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

gyft.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from timetable import delete_calendar, create_calendar, build_courses, generate_ics
55
from utils.dates import SEM_BEGIN
66

7-
87
headers = {
98
"timeout": "20",
109
"User-Agent":

timetable/extractor.py

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,60 +73,43 @@ def build_courses(html: str, course_names: dict) -> list[Course]:
7373

7474
timings = create_timings(table)
7575
rows = table.find_all('tr')
76+
7677
for row in rows:
7778
day_cell: PageElement = row.find('td', {'valign': 'top'})
7879
if not day_cell:
7980
continue
80-
day = DAYS_MAP.get(day_cell.get_text(), day_cell.get_text())
81-
8281
# This is the previously parsed course/time slot. Used to merge timeslots occurring adjacent to each other and initialize course objects
82+
day = DAYS_MAP.get(day_cell.get_text(), day_cell.get_text())
83+
8384
prev: Course | None = None
8485
cells = [cell for cell in row.find_all('td') if cell.attrs.get('valign') != 'top']
85-
86+
8687
for index, cell in enumerate(cells):
87-
# Empty cell means either previous class just ended or no class is scheduled
88-
if cell.get_text().strip() == "":
88+
text = cell.get_text().strip()
89+
if not text: # encountered empty cell: either commit the prev course or skip
8990
if prev:
9091
# Previous slot ended
9192
courses.append(prev)
9293
prev = None
9394
continue
94-
95-
code = cell.get_text()[:7].strip()
96-
if not code: continue # continue if cell has no course in it
97-
98-
# Special exception: CS10003 is the actual code, but it is written as CS10001 in the timetable
99-
if code == "CS10001":
100-
code = "CS10003"
101-
102-
location = cell.get_text()[7:]
103-
cell_duration = int(cell.attrs.get('colspan'))
104-
105-
# Either new class started just after previous one or previous class is continued
106-
if prev is not None:
107-
if code == prev.code:
108-
# Previous class is same as current class, add the duration to the previous class
109-
prev.duration += cell_duration
110-
else:
111-
# The previous class is different from the current one, meaning previous class ended. Clean up, add to
112-
# list and initialize a new course object for the current class
95+
96+
code = text[:7] if text[:7] != "CS10001" else "CS10003" # original code is CS10003 but the timetable has CS10001
97+
location = text[7:]
98+
cell_duration = int(cell.attrs.get('colspan', 1))
99+
100+
if prev and code == prev.code: # encounterd a continuation of the previous course
101+
prev.duration += cell_duration
102+
else:
103+
if prev: # encountered a new course, commit the previous course
113104
courses.append(prev)
114-
prev = None
115-
116-
# Either new class started after break or after a different previous course
117-
if prev is None:
118-
prev = Course(
119-
code=code,
120-
name=course_names[code],
121-
day=day,
122-
start_time=timings[index],
123-
location=location,
124-
duration=cell_duration
125-
)
126-
127-
# Day ended, add the last class
128-
if prev is not None:
105+
# reinstantiate the prev course
106+
prev = Course(code=code, name=course_names.get(code, ""), day=day,
107+
start_time=timings[index],
108+
location=location)
109+
prev.duration = cell_duration
110+
111+
# end of the day: commit the last course
112+
if prev:
129113
courses.append(prev)
130-
prev = None
131-
114+
132115
return courses

0 commit comments

Comments
 (0)