Skip to content

Commit 3c31f2e

Browse files
committed
Add get_reverse_subject_map()
This was sparked by an issue in which STSS 2520 appears as a prerequisite for some other course in Fall 2025, but STSS no longer exists. However, we still want to convert the subject name into its respective code, so a function that could create a "master" subject reverse lookup map across multiple terms was needed.
1 parent 8ddc77c commit 3c31f2e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

app/scrapers/sis_scraper.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import re
44
import time
5+
from datetime import datetime
56
from enum import Enum
67
from pathlib import Path
78
from typing import Any
@@ -28,6 +29,32 @@ class ClassColumn(str, Enum):
2829
TERM = "term"
2930

3031

32+
async def get_reverse_subject_map(
33+
session: aiohttp.ClientSession,
34+
start_year: int = 1998,
35+
end_year: int = datetime.now().year,
36+
seasons: list[str] = None,
37+
) -> dict[str, str]:
38+
"""
39+
Fetches the list of subjects from the specific range of years and seasons, and
40+
returns a mapping of subject names to subject codes.
41+
42+
Defaults to a range from 1998 to the current year, and Spring, Summer, and Fall
43+
seasons. SIS data begins in Summer 1998.
44+
"""
45+
subject_name_code_map = {}
46+
if seasons is None:
47+
seasons = ["spring", "summer", "fall"]
48+
async with aiohttp.ClientSession() as session:
49+
for year in range(start_year, end_year + 1):
50+
for season in seasons:
51+
term = get_term_code(year, season)
52+
subjects = await get_term_subjects(session, term)
53+
for subject in subjects:
54+
subject_name_code_map[subject["description"]] = subject["code"]
55+
return subject_name_code_map
56+
57+
3158
async def get_term_subjects(
3259
session: aiohttp.ClientSession, term: str
3360
) -> list[dict[str, str]]:

0 commit comments

Comments
 (0)