-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_fetcher.py
More file actions
776 lines (681 loc) · 35.8 KB
/
Copy pathcourse_fetcher.py
File metadata and controls
776 lines (681 loc) · 35.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
import logging
import os
import threading
from datetime import datetime, timezone
from typing import Optional, List, Dict
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from rapidfuzz import fuzz
from utils.constants import CAMPUS_ID_TO_NAME
from utils.name_utils import normalize_instructor_name_variants
from utils.fuzzy_utils import get_best_fuzzy_score
logger = logging.getLogger(__name__)
class CourseFetcher:
# Mapping weekday codes to full names
WEEKDAY_MAP = {
"M": "Monday",
"T": "Tuesday",
"W": "Wednesday",
"H": "Thursday",
"F": "Friday",
"S": "Saturday",
"Su": "Sunday"
}
def __init__(self):
# Keep the cache in-process. This avoids a network call while Vercel
# imports the module and lets warm serverless instances reuse data.
self.courses_by_params: Dict[str, List[Dict]] = {}
self.updated_at_by_params: Dict[str, datetime] = {}
self.last_update = None
self.base_url = os.environ.get(
"RUTGERS_COURSES_URL",
"https://classes.rutgers.edu/soc/api/courses.json",
)
self.cache_ttl_seconds = max(
0, int(os.environ.get("COURSE_CACHE_TTL_SECONDS", "900"))
)
self._locks: Dict[str, threading.Lock] = {}
# Configure requests session with retries
self.session = requests.Session()
retry_strategy = Retry(
total=1,
backoff_factor=0.25,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=frozenset({"GET"}),
)
adapter = HTTPAdapter(max_retries=retry_strategy)
self.session.mount("https://", adapter)
def convert_to_am_pm(self, military_time: str) -> str:
"""Convert military time to AM/PM format"""
if not military_time or military_time == "N/A":
return "N/A"
try:
return datetime.strptime(military_time,
"%H%M").strftime("%I:%M %p").lstrip("0")
except ValueError:
logger.warning(f"Invalid military time format: {military_time}")
return "N/A"
def _mark_refresh_failed(self, param_key: str) -> None:
"""Avoid hammering the upstream API when a stale cache refresh fails."""
if param_key in self.courses_by_params:
self.updated_at_by_params[param_key] = datetime.now(timezone.utc)
def format_weekday(self, day: str) -> str:
"""Convert weekday code to full name"""
return self.WEEKDAY_MAP.get(day, day)
def format_campus(self, campus_id: str) -> str:
"""Convert campus ID to campus name"""
return CAMPUS_ID_TO_NAME.get(campus_id, campus_id)
def format_meeting_time(self, meeting: Dict) -> Dict:
"""Format meeting time information with proper weekday and campus names"""
try:
start_time = meeting.get("startTimeMilitary", "N/A")
end_time = meeting.get("endTimeMilitary", "N/A")
day_code = meeting.get("meetingDay", "")
campus_id = meeting.get("campusLocation", "N/A")
return {
"day": self.format_weekday(day_code),
"start_time": {
"military": start_time,
"formatted": self.convert_to_am_pm(start_time)
},
"end_time": {
"military": end_time,
"formatted": self.convert_to_am_pm(end_time)
},
"building": meeting.get("buildingCode", "N/A"),
"room": meeting.get("roomNumber", "N/A"),
"mode": meeting.get("meetingModeDesc", "N/A"),
"campus": self.format_campus(campus_id),
"campus_name": self.format_campus(campus_id),
}
except Exception as e:
logger.error(f"Error formatting meeting time: {str(e)}")
return {
"day": "N/A",
"start_time": {
"military": "N/A",
"formatted": "N/A"
},
"end_time": {
"military": "N/A",
"formatted": "N/A"
},
"building": "N/A",
"room": "N/A",
"mode": "N/A",
"campus": "N/A"
}
def format_section(self, section: Dict) -> Dict:
"""Format section information with detailed meeting times"""
try:
return {
"number":
section.get("number", ""),
"index":
section.get("index", ""),
"instructors": [
instr.get("name", "") if isinstance(instr, dict) else str(instr)
for instr in section.get("instructors", [])
],
"status":
section.get("openStatusText", ""),
"comments":
section.get("commentsText", ""),
"meeting_times": [
self.format_meeting_time(meeting)
for meeting in section.get("meetingTimes", [])
]
}
except Exception as e:
logger.error(f"Error formatting section: {str(e)}")
return {
"number": "Error",
"index": "",
"instructors": [],
"status": "Error loading section",
"comments": "",
"meeting_times": []
}
def update_courses(self, year="2026", term="1", campus="NB") -> bool:
"""Fetch fresh course data from Rutgers API"""
# Define param_key before the try block to make it available in exception handlers
param_key = f"{year}_{term}_{campus}"
try:
params = {"year": year, "term": term, "campus": campus}
logger.info(f"Fetching courses with parameters: {params}")
response = self.session.get(
self.base_url,
params=params,
timeout=(3.05, 10),
)
response.raise_for_status()
courses = response.json()
if not isinstance(courses, list):
raise ValueError("Rutgers courses API returned a non-list payload")
logger.info("Retrieved %s courses from Rutgers API", len(courses))
if not courses:
logger.warning("Received empty course list from API")
return False
self.courses_by_params[param_key] = sorted(
courses, key=lambda c: c.get("courseString", ""))
now = datetime.now(timezone.utc)
self.updated_at_by_params[param_key] = now
self.last_update = now.isoformat()
logger.info("Successfully updated courses at %s", self.last_update)
return True
except requests.exceptions.Timeout:
logger.error("Timeout while fetching courses from API")
self._mark_refresh_failed(param_key)
return False
except requests.exceptions.RequestException as e:
logger.error("Failed to fetch courses from API: %s", e)
self._mark_refresh_failed(param_key)
return False
except (ValueError, TypeError) as e:
logger.error("Failed to parse courses API response: %s", e)
self._mark_refresh_failed(param_key)
return False
except Exception as e:
logger.exception("Unexpected error updating courses: %s", e)
self._mark_refresh_failed(param_key)
return False
def fuzzy_search_courses(self,
courses: List[Dict],
query: str,
threshold: int = 70) -> List[Dict]:
"""Filter and rank courses using fuzzy matching on key fields."""
results = []
query = query.lower().strip()
# Check if query matches common patterns for course codes
is_specific_course_query = False
subject_part = None
number_part = None
# Pattern matching for queries like "cs 111", "198:111", "computer science 111"
# Common department abbreviations
dept_abbrevs = {
"cs": "198", # Computer Science
"math": "640", # Mathematics
"bio": "119", # Biology
"chem": "160", # Chemistry
"phys": "750", # Physics
"stat": "960", # Statistics
"econ": "220" # Economics
}
# Try to parse common query formats
query_parts = query.split()
if len(query_parts) == 2:
# Format like "cs 111" or "math 152"
potential_dept, potential_number = query_parts
if potential_number.isdigit():
is_specific_course_query = True
subject_part = dept_abbrevs.get(potential_dept, potential_dept)
number_part = potential_number
elif ":" in query:
# Format like "198:111"
parts = query.split(":")
if len(parts) == 2 and parts[1].isdigit():
is_specific_course_query = True
subject_part = parts[0]
number_part = parts[1]
elif query.isdigit():
# Just a course number like "111"
is_specific_course_query = True
number_part = query
# Group courses by their course_string for consistent matching
course_groups = {}
exact_matches = []
high_relevance_matches = []
# Process all courses
for course in courses:
course_string = course.get("courseString", "").lower()
subject = course.get("subject", "").lower()
course_number = course.get("courseNumber", "").lower()
title = course.get("title", "").lower()
subject_description = course.get("subjectDescription", "").lower()
# Aggregate instructor names for the course from its sections
instructor_names = set()
try:
for section in course.get("sections", []) or []:
for instr in section.get("instructors", []) or []:
raw_name = (instr.get("name", "") or "").strip()
if not raw_name:
continue
# Use utility function to generate all name variants
name_variants = normalize_instructor_name_variants(raw_name)
instructor_names.update(name_variants)
except Exception:
# If structure differs for some rows, skip instructor aggregation silently
pass
# Store courses by their courseString for grouping
if course_string not in course_groups:
course_groups[course_string] = []
course_groups[course_string].append(course)
# Exact match on course title (Tier 1 - highest priority)
if query == title:
exact_matches.append((100, course_string))
continue
# Special handling for CS (Computer Science) department
# Common mistake: "cs" searches matching many unrelated courses
if query == "cs" and subject != "198":
continue
# If the query exactly matches an instructor full name or a normalized variant, prioritize this course
if instructor_names and query in instructor_names:
high_relevance_matches.append((92, course_string))
# Do not continue; allow other exact matches to contribute as well
# Fuzzy matching for instructor names (handles typos)
if instructor_names:
instructor_fuzzy_scores = []
for instructor_name in instructor_names:
# Use utility function to get best fuzzy score
best_score = get_best_fuzzy_score(query, instructor_name)
instructor_fuzzy_scores.append(best_score)
# If any instructor name has a high fuzzy match, include this course
max_instructor_score = max(instructor_fuzzy_scores) if instructor_fuzzy_scores else 0
if max_instructor_score >= 75: # Lower threshold for instructor fuzzy matching
# Add to results with score based on fuzzy match quality
results.append((max_instructor_score, course_string))
# Handle specific course query patterns
if is_specific_course_query:
# Perfect match on subject and course number
if (subject_part and number_part and
subject == subject_part and course_number == number_part):
exact_matches.append((100, course_string))
continue
# Match on just the course number if that's all we have
elif (not subject_part and number_part and
course_number == number_part):
exact_matches.append((95, course_string))
continue
# For dept abbreviation matches like "cs" -> "Computer Science"
elif (subject_part in dept_abbrevs.keys() and
number_part and
subject == "198" and # CS department code
course_number == number_part):
exact_matches.append((98, course_string))
continue
# For matches on subject description like "computer science"
elif (subject_part and number_part and
subject_part in subject_description and
course_number == number_part):
high_relevance_matches.append((90, course_string))
continue
# Case 1: Exact match on course code
if query == course_string or query == f"{subject}:{course_number}":
exact_matches.append((100, course_string))
continue
# Case 2: Exact match on just course number or subject
if query == course_number or query == subject:
high_relevance_matches.append((85, course_string))
continue
# Skip fuzzy matching if we're doing a specific course search
# This prevents unrelated courses from showing up
if is_specific_course_query:
continue
# Case 3: Fuzzy matching for general searches
score_course_string = fuzz.token_set_ratio(query, course_string)
score_title = fuzz.token_set_ratio(query, title)
score_subject = fuzz.token_set_ratio(query, subject)
score_course_number = fuzz.token_set_ratio(query, course_number)
score_subject_desc = fuzz.token_set_ratio(query, subject_description)
# Fuzzy score against instructor names (combined)
score_instructors = 0
if instructor_names:
score_instructors = fuzz.token_set_ratio(query, " ".join(instructor_names))
max_score = max(score_course_string, score_title, score_subject,
score_course_number, score_subject_desc, score_instructors)
if max_score >= threshold:
results.append((max_score, course_string))
# Create a unique set of course strings that matched
unique_results = {}
# First add exact matches with highest priority
for score, course_string in exact_matches:
unique_results[course_string] = score
# Then add high relevance matches
for score, course_string in high_relevance_matches:
if course_string not in unique_results or score > unique_results[course_string]:
unique_results[course_string] = score
# Then add fuzzy matches with lowest priority
if not is_specific_course_query or len(unique_results) == 0:
for score, course_string in results:
if course_string not in unique_results or score > unique_results[course_string]:
unique_results[course_string] = score
# Convert back to list and sort by score
sorted_results = sorted([(score, cs) for cs, score in unique_results.items()],
key=lambda x: x[0], reverse=True)
# Get all courses for each matched course string
matched_courses = []
for _, course_string in sorted_results:
matched_courses.extend(course_groups.get(course_string, []))
logger.info(f"Search for '{query}' found {len(matched_courses)} courses from {len(unique_results)} unique course strings")
return matched_courses
def _is_time_in_range(self, military_time: str, time_range: str) -> bool:
"""Check if military time falls within a time range."""
if not military_time or military_time == "N/A":
return False
try:
time_int = int(military_time)
hour = time_int // 100
minute = time_int % 100
total_minutes = hour * 60 + minute
if time_range == 'morning':
# 8:00 AM (800) to 11:00 AM (1100)
return 480 <= total_minutes < 660
elif time_range == 'afternoon':
# 11:00 AM (1100) to 4:00 PM (1600)
return 660 <= total_minutes < 960
elif time_range == 'evening':
# 4:00 PM (1600) to 10:00 PM (2200)
return 960 <= total_minutes < 1320
return False
except (ValueError, TypeError):
return False
def _is_weekend_day(self, day_code: str) -> bool:
"""Check if day code represents a weekend day."""
return day_code in ['S', 'Su']
def _matches_course_type(self, meeting_times: List[Dict], course_type: str) -> bool:
"""Check if course matches the specified course type based on meeting times."""
if not meeting_times:
return False
for meeting_time in meeting_times:
mode = meeting_time.get('mode', '').lower()
if course_type == 'traditional':
# Traditional/Face-to-Face: not online, not hybrid, not remote
if 'online' not in mode and 'hybrid' not in mode and 'remote' not in mode and 'asynchronous' not in mode:
return True
elif course_type == 'hybrid':
if 'hybrid' in mode:
return True
elif course_type == 'online':
# Online & Remote Instruction: online, remote, or asynchronous
if 'online' in mode or 'remote' in mode or 'asynchronous' in mode:
return True
return False
def apply_filters(self, courses: List[Dict], filters: Dict) -> List[Dict]:
"""Apply filters to a list of courses."""
if not filters:
return courses
filtered_courses = []
subject_filter_applied = False
for course in courses:
# Filter by subject - normalize both values to strings and strip whitespace for comparison
if 'subject' in filters:
subject_filter_applied = True
course_subject = str(course.get('subject', '')).strip()
filter_subject = str(filters['subject']).strip()
# Only apply filter if filter_subject is not empty
if filter_subject and course_subject != filter_subject:
continue
if 'course_number' in filters:
course_number = str(course.get('courseNumber', '')).strip()
requested_number = str(filters['course_number']).strip()
if requested_number and course_number != requested_number:
continue
# Filter by school/unit
if 'school' in filters:
school_data = course.get('school', {})
school_code = school_data.get('code', '') if isinstance(school_data, dict) else ''
school_desc = school_data.get('description', '') if isinstance(school_data, dict) else str(school_data)
if school_code != filters['school'] and filters['school'] not in school_desc:
continue
# Filter by core code
if 'core_code' in filters:
core_codes = course.get('coreCodes', [])
has_core_code = False
for core in core_codes:
if isinstance(core, dict):
if core.get('coreCode', core.get('code', '')) == filters['core_code']:
has_core_code = True
break
elif str(core) == filters['core_code']:
has_core_code = True
break
if not has_core_code:
continue
# Get sections for this course
sections = course.get('sections', [])
if not sections:
# If no sections, skip if we're filtering by status or meeting times
if 'status' in filters or 'days' in filters or 'time_range' in filters or 'course_type' in filters or 'campus' in filters:
continue
filtered_courses.append(course)
continue
# Filter by section status - course matches if ANY section matches
if 'status' in filters:
status_filters = filters['status']
has_matching_status = False
for section in sections:
section_status = section.get('openStatusText', '').lower()
if 'open' in status_filters and 'open' in section_status:
has_matching_status = True
break
if 'closed' in status_filters and 'closed' in section_status:
has_matching_status = True
break
if not has_matching_status:
continue
# Filter by course type, days, time, and campus - need to check meeting times
# Special handling: if course_type filter is applied, filter sections to only include matching ones
filtered_sections = []
course_type_filter_applied = 'course_type' in filters
if 'course_type' in filters or 'days' in filters or 'time_range' in filters or 'campus' in filters:
course_type_match = 'course_type' not in filters
days_match = 'days' not in filters
time_match = 'time_range' not in filters
campus_match = 'campus' not in filters
# Check all sections' meeting times
for section in sections:
meeting_times_raw = section.get('meetingTimes', [])
if not meeting_times_raw:
# If no meeting times, only include section if course_type filter is not applied
if not course_type_filter_applied:
filtered_sections.append(section)
continue
# Format meeting times
formatted_meeting_times = [self.format_meeting_time(mt) for mt in meeting_times_raw]
# Check course type - if filter is applied, only include sections that match
section_matches_course_type = False
if course_type_filter_applied:
for course_type in filters['course_type']:
if self._matches_course_type(formatted_meeting_times, course_type):
section_matches_course_type = True
course_type_match = True
break
else:
# No course type filter, section matches by default
section_matches_course_type = True
course_type_match = True
# Check days
section_days_match = 'days' not in filters
if 'days' in filters and not days_match:
for day_filter in filters['days']:
if day_filter == 'weekend':
# Check if any meeting time is on weekend
for mt_raw, mt_formatted in zip(meeting_times_raw, formatted_meeting_times):
day_code_raw = mt_raw.get('meetingDay', '')
day_name = mt_formatted.get('day', '')
if self._is_weekend_day(day_code_raw) or day_name in ['Saturday', 'Sunday']:
section_days_match = True
days_match = True
break
else:
# Map day code to full name
day_name_expected = self.WEEKDAY_MAP.get(day_filter, day_filter)
for mt_raw, mt_formatted in zip(meeting_times_raw, formatted_meeting_times):
day_code_raw = mt_raw.get('meetingDay', '')
day_name = mt_formatted.get('day', '')
if day_code_raw == day_filter or day_name == day_name_expected or day_name == day_filter:
section_days_match = True
days_match = True
break
if section_days_match:
break
# Check time range
section_time_match = 'time_range' not in filters
if 'time_range' in filters and not time_match:
for time_range in filters['time_range']:
for mt in formatted_meeting_times:
start_time = mt.get('start_time', {}).get('military', '')
if self._is_time_in_range(start_time, time_range):
section_time_match = True
time_match = True
break
if section_time_match:
break
# Check campus
section_campus_match = 'campus' not in filters
if 'campus' in filters and not campus_match:
for campus_filter in filters['campus']:
for mt_raw, mt_formatted in zip(meeting_times_raw, formatted_meeting_times):
mt_campus_formatted = mt_formatted.get('campus', '')
mt_campus_id = mt_raw.get('campusLocation', '')
# Check formatted campus name
if campus_filter.lower() in mt_campus_formatted.lower() or mt_campus_formatted.lower() in campus_filter.lower():
section_campus_match = True
campus_match = True
break
# Also check if campus ID maps to the filter
campus_id_name = self.format_campus(mt_campus_id)
if campus_filter.lower() in campus_id_name.lower():
section_campus_match = True
campus_match = True
break
if section_campus_match:
break
# If course type filter is applied, only add sections that match the course type
# Other filters (days, time, campus) determine if course should be included,
# but don't filter individual sections
if course_type_filter_applied:
if section_matches_course_type:
filtered_sections.append(section)
else:
# No course type filter, include all sections
filtered_sections.append(section)
if course_type_match and days_match and time_match and campus_match:
break
# If any filter didn't match, skip this course
if not (course_type_match and days_match and time_match and campus_match):
continue
# If course type filter is applied, only include course if it has matching sections
if course_type_filter_applied and not filtered_sections:
continue
else:
# No filters applied, include all sections
filtered_sections = sections.copy()
# Create a copy of the course with filtered sections
course_copy = course.copy()
course_copy['sections'] = filtered_sections
# Course passed all filters
filtered_courses.append(course_copy)
if subject_filter_applied and 'subject' in filters:
logger.info(f"Subject filter '{filters['subject']}' applied: {len(courses)} courses -> {len(filtered_courses)} courses")
else:
logger.info(f"Applied filters to {len(courses)} courses, {len(filtered_courses)} courses remain")
return filtered_courses
def get_courses(self,
search: Optional[str] = None,
year="2026",
term="1",
campus="NB",
filters: Optional[Dict] = None) -> List[Dict]:
"""Get filtered course data with enriched information and fuzzy search."""
try:
param_key = f"{year}_{term}_{campus}"
# Refresh lazily so importing the Flask app is fast and safe on
# serverless platforms. A per-parameter lock prevents duplicate
# upstream requests when two requests arrive on a warm instance.
cached_at = self.updated_at_by_params.get(param_key)
is_stale = (
cached_at is None
or self.cache_ttl_seconds == 0
or (datetime.now(timezone.utc) - cached_at).total_seconds()
>= self.cache_ttl_seconds
)
if param_key not in self.courses_by_params or is_stale:
lock = self._locks.setdefault(param_key, threading.Lock())
with lock:
cached_at = self.updated_at_by_params.get(param_key)
is_stale = (
cached_at is None
or self.cache_ttl_seconds == 0
or (datetime.now(timezone.utc) - cached_at).total_seconds()
>= self.cache_ttl_seconds
)
if param_key not in self.courses_by_params or is_stale:
self.update_courses(year, term, campus)
if not self.courses_by_params.get(param_key):
logger.warning(
f"No courses available for parameters: year={year}, term={term}, campus={campus}"
)
return []
# Get a copy of the cached courses to avoid modifying the original
filtered_courses = self.courses_by_params[param_key].copy()
# Apply filters FIRST to narrow down the dataset before searching
# This ensures that if a subject filter is set, we only search within that subject
if filters:
logger.info(f"Applying filters: {filters}")
filtered_courses = self.apply_filters(filtered_courses, filters)
logger.info(f"After filters: {len(filtered_courses)} courses remain")
# Then apply search on the filtered results
if search:
# Use fuzzy search to filter courses
filtered_courses = self.fuzzy_search_courses(
filtered_courses, search)
# Enrich course data with detailed information
enriched_courses = []
for course in filtered_courses:
try:
school_data = course.get("school") or {}
school = (
school_data.get("description", "")
if isinstance(school_data, dict)
else str(school_data)
)
credits_data = course.get("creditsObject") or {}
campus_locations = []
for location in course.get("campusLocations", []) or []:
if isinstance(location, dict):
campus_locations.append(location.get("description", ""))
elif location:
campus_locations.append(str(location))
core_requirements = []
for core in course.get("coreCodes", []) or []:
if isinstance(core, dict):
core_requirements.append({
"code": core.get("coreCode", core.get("code", "")),
"description": core.get(
"coreCodeDescription", core.get("description", "")
),
})
elif core:
core_requirements.append({"code": str(core), "description": ""})
enriched_course = {
"courseString": course.get("courseString", ""),
"title": course.get("title", ""),
"subject": course.get("subject", ""),
"subjectDescription": course.get("subjectDescription", ""),
"course_number": course.get("courseNumber", ""),
"courseNumber": course.get("courseNumber", ""),
"description": course.get("courseDescription", ""),
"credits": course.get("credits", ""),
"creditsDescription": credits_data.get("description", "") if isinstance(credits_data, dict) else str(credits_data),
"school": school,
"campusLocations": campus_locations,
"prerequisites": course.get("preReqNotes", ""),
"coreRequirements": core_requirements,
"coreCodes": core_requirements,
"sections": [
self.format_section(section) for section in course.get("sections", [])
]
}
enriched_courses.append(enriched_course)
except Exception as e:
logger.error(f"Error enriching course data: {str(e)}")
continue
logger.info(
f"Returning {len(enriched_courses)} enriched courses for search: '{search}'"
)
return enriched_courses
except Exception as e:
logger.error(f"Error getting courses: {str(e)}")
return []