Skip to content

Commit 663ede0

Browse files
dtemkin1psvenk
andauthored
Check term for quarter information (#310)
Resolves #282. See also: sipb/fireroad-server#56 --------- Co-authored-by: Pratyush Venkatakrishnan <contact@psvenk.com>
1 parent 94fa2dd commit 663ede0

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

scrapers/fireroad.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def decode_quarter_date(date: str) -> tuple[int, int] | None:
189189

190190

191191
def parse_quarter_info(
192-
course: Mapping[str, CourseValues],
192+
course: Mapping[str, CourseValues], term: Term
193193
) -> dict[str, dict[str, tuple[int, int]]]:
194194
"""
195195
Parses quarter info from the course.
@@ -205,12 +205,23 @@ def parse_quarter_info(
205205
206206
Args:
207207
course (Mapping[str, CourseValues]): The course object.
208+
term (Term): The current term (fall, IAP, spring, or summer).
208209
209210
Returns:
210211
dict[str, dict[str, tuple[int, int]]]: The parsed quarter info.
211212
"""
212213

213-
quarter_info: str = course.get("quarter_information", "") # type: ignore
214+
quarter_info: str
215+
216+
if any(f"quarter_information_{t.value}" in course for t in Term):
217+
# This course has quarter information by term, so look up the one for this term
218+
quarter_info = course.get(
219+
f"quarter_information_{term.value}", "" # type: ignore
220+
)
221+
else:
222+
# Fall back to general quarter information
223+
quarter_info = course.get("quarter_information", "") # type: ignore
224+
214225
if quarter_info:
215226
quarter_info_list = quarter_info.split(",")
216227

@@ -348,30 +359,29 @@ def get_course_data(
348359
if term.name not in raw_class["terms"]: # type: ignore
349360
return False
350361

351-
has_schedule = "schedule" in course
362+
has_schedule = True
352363

353364
# tba, sectionKinds, lectureSections, recitationSections, labSections,
354365
# designSections, lectureRawSections, recitationRawSections, labRawSections,
355366
# designRawSections
356-
if has_schedule:
357-
try:
358-
if term == Term.FA and "schedule_fall" in course:
359-
raw_class.update(
360-
parse_schedule(course["schedule_fall"]) # type: ignore
361-
)
362-
elif term == Term.JA and "schedule_IAP" in course:
363-
raw_class.update(parse_schedule(course["schedule_IAP"])) # type: ignore
364-
elif term == Term.SP and "schedule_spring" in course:
365-
raw_class.update(
366-
parse_schedule(course["schedule_spring"]) # type: ignore
367-
)
368-
else:
369-
raw_class.update(parse_schedule(course["schedule"])) # type: ignore
370-
except ValueError as val_err:
371-
# if we can't parse the schedule, warn
372-
# NOTE: parse_schedule will raise a ValueError
373-
print(f"Can't parse schedule {course_code}: {val_err!r}")
374-
has_schedule = False
367+
try:
368+
if any(f"schedule_{t.value}" in course for t in Term):
369+
# This course has schedule information by term,
370+
# so look up the one for this term
371+
raw_class.update(
372+
parse_schedule(course[f"schedule_{term.value}"]) # type: ignore
373+
)
374+
else:
375+
# Fall back to general quarter information
376+
raw_class.update(parse_schedule(course["schedule"])) # type: ignore
377+
except KeyError:
378+
has_schedule = False
379+
except ValueError as val_err:
380+
# if we can't parse the schedule, warn
381+
# NOTE: parse_schedule will raise a ValueError
382+
print(f"Can't parse schedule {course_code}: {val_err!r}")
383+
has_schedule = False
384+
375385
if not has_schedule:
376386
raw_class.update(
377387
{
@@ -415,7 +425,7 @@ def get_course_data(
415425
assert raw_class["preparationUnits"] == 0
416426

417427
# Get quarter info if available
418-
raw_class.update(parse_quarter_info(course))
428+
raw_class.update(parse_quarter_info(course, term))
419429

420430
raw_class.update(
421431
{

0 commit comments

Comments
 (0)