Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeslot parsing #82

Merged
merged 4 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions scrapers/fireroad.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
schedule for the latest term.

Functions:
* parse_timeslot(day, slot)
* parse_timeslot(day, slot, pm)
* parse_section(section)
* parse_schedule(course)
* parse_attributes(course)
Expand All @@ -26,48 +26,64 @@
URL = "https://fireroad.mit.edu/courses/all?full=true"


def parse_timeslot(day, slot):
"""Parses a timeslot. Example: parse_timeslot("M", "10-11.30") -> [4, 3]
def parse_timeslot(day, slot, pm):
psvenk marked this conversation as resolved.
Show resolved Hide resolved
"""Parses a timeslot. Example: parse_timeslot("M", "10-11.30", False) -> [4, 3]

Args:
* day (str): The day as a string
* slot (str): The slot as a string
* pm (bool): Whether the timeslot is in the evening

Returns:
* list[int]: The parsed day and timeslot

Raises AssertionError if pm and slot disagree on whether the slot is in the
evening, or if the start slot is later than the end slot.

Raises KeyError if no matching timeslot could be found.
"""
pm, slot = slot.endswith(" PM"), slot.rstrip(" PM")
assert pm == slot.endswith(" PM")
psvenk marked this conversation as resolved.
Show resolved Hide resolved
slot = slot.rstrip(" PM")

if "-" in slot:
start, end = slot.split("-")
start_slot = utils.find_timeslot(day, start, pm)
end_slot = utils.find_timeslot(day, end, pm)
try:
start_slot = utils.find_timeslot(day, start, pm)
end_slot = utils.find_timeslot(day, end, pm)
except KeyError:
# Maybe the start time is AM but the end time is PM
start_slot = utils.find_timeslot(day, start, False)
end_slot = utils.find_timeslot(day, end, True)
else:
start_slot = utils.find_timeslot(day, slot, pm)
# Slot is one hour long, so length is 2.
end_slot = start_slot + 2

assert end_slot > start_slot

return [start_slot, end_slot - start_slot]


def parse_section(section):
"""Parses a section string.
Example: "32-123/TR/0/11/F/0/2" -> [[[36, 2], [96, 2], [132, 2]], '32-123']

Args:
* section (str): The section given as a string

Returns:
* list[Union[list[str], str]]: The parsed section.

Raises AssertionError or KeyError if parse_timeslot does.
"""
place, *infos = section.split("/")
slots = []

for weekdays, _, slot in utils.grouper(infos, 3):
for weekdays, pm, slot in utils.grouper(infos, 3):
for day in weekdays:
if day == "S":
continue # TODO: handle saturday
slots.append(parse_timeslot(day, slot))
slots.append(parse_timeslot(day, slot, bool(int(pm))))

return [slots, place]

Expand All @@ -82,6 +98,8 @@ def parse_schedule(course):

Returns:
* dict[str, union[list, bool]: The parsed schedule

Raises AssertionError or KeyError if parse_section does.
"""
schedule = course["schedule"]
section_tba = False
Expand Down Expand Up @@ -222,7 +240,7 @@ def get_course_data(courses, course):
raw_class.update(parse_schedule(course))
except Exception as e:
# if we can't parse the schedule, warn
print(f"Can't parse schedule {course_code}: {e}")
print(f"Can't parse schedule {course_code}: {e!r}")
return False

# hh, ha, hs, he, ci, cw, re, la, pl
Expand Down
6 changes: 2 additions & 4 deletions scrapers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
"4.30": 17,
"5": 18,
"5.30": 19,
"6": 20,
"6.30": 21,
"7": 22,
"7.30": 23,
}

EVE_TIMES = {
Expand Down Expand Up @@ -103,6 +99,8 @@ def find_timeslot(day, slot, pm):

Returns:
* int: A numeric code for the timeslot

Raises KeyError if no matching timeslot could be found.
"""
if pm:
return DAYS[day] + EVE_TIMES[slot]
Expand Down