-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfetch_grades.py
More file actions
528 lines (424 loc) · 15.8 KB
/
Copy pathfetch_grades.py
File metadata and controls
528 lines (424 loc) · 15.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
#!/usr/bin/env python3
"""
Fetch Grade Data from UVA FOIA Grade Distribution Website
This script scrapes grade distribution data from UVA's FOIA Qlik application
and outputs a CSV file compatible with theCourseForum's load_grades command.
INSTALLATION:
uv sync # installs selenium (dev group) and pandas from uv.lock
REQUIREMENTS:
- Chrome browser installed
- ChromeDriver (auto-managed by selenium 4.6+)
USAGE:
uv run python fetch_grades.py 2024_fall # Scrape all sections
uv run python fetch_grades.py 2024_fall --limit 10 # Scrape only 10 sections (testing)
uv run python fetch_grades.py 2024_fall --resume # Resume interrupted scrape
OUTPUT:
Creates: tcf_website/management/commands/grade_data/csv/<year>_<season>.csv
"""
import argparse
import os
import re
import sys
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# ============================================================================
# CONFIGURATION
# ============================================================================
TARGET_URL = "https://qlksnpn-apprd01.eservices.virginia.edu/extensions/FOIAGradeDistribution/FOIAGradeDistribution.html"
# CSS selectors for Qlik filter interface
BTN_TERM = "div[data-testid='collapsed-title-Term']"
BTN_CLASS_NUM = "div[data-testid='collapsed-title-Class Num']"
INPUT_SEARCH = "input[data-testid='search-input-field']"
BTN_CONFIRM = "button[data-testid='actions-toolbar-confirm']"
BTN_CLEAR = "button[data-testid='actions-toolbar-clear']"
# Directory paths (relative to repo root)
SEMESTER_DATA_DIR = "tcf_website/management/commands/semester_data/csv"
GRADE_DATA_DIR = "tcf_website/management/commands/grade_data/csv"
# Season mapping
SEASON_NAMES = {
"fall": "Fall",
"spring": "Spring",
"summer": "Summer",
"january": "January",
}
# Output CSV columns (must match load_grades.py expectations)
OUTPUT_COLUMNS = [
"Term Desc",
"Subject",
"Catalog Number",
"Class Title",
"Course ID",
"Primary Instructor Name",
"Class Section",
"Class Num",
"Class Academic Group",
"Course GPA",
"# of Students",
"A+",
"A",
"A-",
"B+",
"B",
"B-",
"C+",
"C",
"C-",
"DFW",
]
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
def clean_text(text: str) -> str:
"""Normalize whitespace in text."""
if not text:
return ""
return re.sub(r"\s+", " ", text).strip()
def format_instructor_name(name: str) -> str:
"""
Takes a string of names (potentially multiple) and returns only
the first name in "Last,First" format.
Example: "John Smith, Jane Doe" -> "Smith,John"
"""
if not name or name == "To Be Announced" or pd.isna(name):
return "..."
# 1. Split by comma to handle multiple instructors
# "First1 Last1, First2 Last2" -> ["First1 Last1", " First2 Last2"]
name_parts = str(name).split(",")
# 2. Take only the first name in the list
first_instructor = name_parts[0].strip()
# 3. Apply the "Last,First" logic to that single name
parts = first_instructor.split()
if len(parts) < 2:
# Still returns placeholder if the first name is incomplete
return "..."
last_name = parts[-1]
first_middle = " ".join(parts[:-1])
return f"{last_name},{first_middle}"
def init_driver() -> webdriver.Chrome:
"""Initialize a headless Chrome WebDriver."""
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
return driver
def set_filter(driver, btn_selector: str, value: str, use_search: bool = False) -> bool:
"""
Set a filter value in the Qlik interface.
Args:
driver: Selenium WebDriver
btn_selector: CSS selector for the filter button
value: Value to select
use_search: Whether to use the search box (for Class Num)
Returns:
True if successful, False otherwise
"""
try:
# Click the filter button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, btn_selector))
).click()
# If searchable, type in the search box
if use_search:
search_box = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, INPUT_SEARCH))
)
search_box.clear()
search_box.send_keys(value)
time.sleep(1.5) # Wait for search results
# Click the matching item
item_xpath = (
f"//div[@role='presentation']//span[normalize-space(text())='{value}']"
)
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, item_xpath))
).click()
# Confirm selection
try:
driver.find_element(By.CSS_SELECTOR, BTN_CONFIRM).click()
except Exception:
ActionChains(driver).send_keys(Keys.ESCAPE).perform()
time.sleep(1)
return True
except Exception:
ActionChains(driver).send_keys(Keys.ESCAPE).perform()
return False
def clear_class_filter(driver) -> None:
"""Clear the Class Num filter."""
try:
driver.find_element(By.CSS_SELECTOR, BTN_CLASS_NUM).click()
WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, BTN_CLEAR))
).click()
ActionChains(driver).send_keys(Keys.ESCAPE).perform()
time.sleep(0.5)
except Exception:
pass
def clear_term_filter(driver) -> None:
"""Clear the Term filter."""
try:
driver.find_element(By.CSS_SELECTOR, BTN_TERM).click()
WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, BTN_CLEAR))
).click()
ActionChains(driver).send_keys(Keys.ESCAPE).perform()
time.sleep(0.5)
except Exception:
pass
def clear_all_filters(driver) -> None:
"""Clear both Class Num and Term filters."""
clear_class_filter(driver)
clear_term_filter(driver)
def scrape_section(driver, term: str, class_num: str, instructor: str) -> dict:
"""
Scrape grade data for a single section.
Args:
driver: Selenium WebDriver (already filtered to the section)
term: Term description (e.g., "2024 Fall")
class_num: Class number
instructor: Instructor name in "Last,First" format
Returns:
Dictionary with all grade data fields
"""
data = {
"Term Desc": term,
"Class Num": class_num,
"Subject": "",
"Catalog Number": "",
"Class Title": "",
"Class Section": "",
"Class Academic Group": "",
"Course ID": "", # Not available from FOIA; left empty
"Primary Instructor Name": instructor,
"Course GPA": "",
"# of Students": "",
"A+": 0,
"A": 0,
"A-": 0,
"B+": 0,
"B": 0,
"B-": 0,
"C+": 0,
"C": 0,
"C-": 0,
"DFW": 0,
}
try:
# 1. Scrape table data
cells = driver.find_elements(By.CSS_SELECTOR, "tr.qv-st-data-row td")
cell_texts = [clean_text(c.get_attribute("textContent")) for c in cells]
if len(cell_texts) >= 7:
data["Class Academic Group"] = cell_texts[0]
data["Subject"] = cell_texts[1]
data["Catalog Number"] = cell_texts[2]
data["Class Section"] = cell_texts[3]
data["Class Title"] = cell_texts[5]
data["# of Students"] = cell_texts[6]
# 2. Scrape GPA from KPI element
try:
kpi_label = driver.find_element(
By.XPATH, "//*[contains(text(), 'Average Course GPA')]"
)
kpi_box = kpi_label.find_element(By.XPATH, "./../../..")
kpi_text = kpi_box.get_attribute("textContent")
match = re.search(r"(\d\.\d{2})", kpi_text)
data["Course GPA"] = match.group(1) if match else ""
except Exception:
pass
# 3. Scrape grade distribution from bar chart
grades_elements = driver.find_elements(
By.CSS_SELECTOR, "svg[data-key='bar-axis'] text"
)
grades_list = [
clean_text(el.get_attribute("textContent")) for el in grades_elements
]
counts_elements = driver.find_elements(
By.CSS_SELECTOR, "svg[data-key='bar-labels'] text"
)
counts_list = [
clean_text(el.get_attribute("textContent")) for el in counts_elements
]
# Pair grades with counts
for grade, count in zip(grades_list, counts_list, strict=False):
if grade in data:
try:
data[grade] = int(count) if count else 0
except ValueError:
data[grade] = 0
return data
except Exception as e:
print(f" Error scraping section: {e}")
return None
# ============================================================================
# MAIN LOGIC
# ============================================================================
def load_semester_data(semester_csv: str) -> list[dict]:
"""
Load section data from semester CSV.
Skips Independent Study (IND) and Discussion sections, plus
any zero-credit section.
Also skips sections the FOIA grade site suppresses and therefore
has no data for: undergraduate (catalog number < 5000) sections
with fewer than 10 students, and non-undergraduate sections with
fewer than 5 students.
Returns list of dicts with 'class_num' and 'instructor' keys.
"""
df = pd.read_csv(semester_csv)
sections = []
for _, row in df.iterrows():
section_type = str(row.get("Type", "")).strip()
units = str(row.get("Units", "")).strip()
if section_type in ("IND", "Discussion"):
continue
if units == "0":
continue
try:
number = int(str(row.get("Number", "")).strip())
except ValueError:
number = 0
try:
enrollment = int(row.get("Enrollment", 0))
except (ValueError, TypeError):
enrollment = 0
is_undergrad = number < 5000
if is_undergrad and enrollment < 10:
continue
if not is_undergrad and enrollment < 5:
continue
class_num = str(row["ClassNumber"])
instructor_raw = row.get("Instructor1", "")
instructor = format_instructor_name(instructor_raw) if instructor_raw else ""
sections.append({"class_num": class_num, "instructor": instructor})
return sections
def main():
parser = argparse.ArgumentParser(
description="Fetch grade data from UVA FOIA Grade Distribution website"
)
parser.add_argument(
"semester", help="Semester in format: <year>_<season> (e.g., 2024_fall)"
)
parser.add_argument(
"--limit",
type=int,
default=None,
help="Limit number of sections to scrape (for testing)",
)
parser.add_argument(
"--resume",
action="store_true",
help="Resume scraping, skipping already-scraped Class Nums",
)
args = parser.parse_args()
# Parse and validate semester
parts = args.semester.split("_")
if len(parts) != 2:
print(f"Error: Invalid semester format '{args.semester}'")
print("Expected format: <year>_<season> (e.g., 2024_fall)")
sys.exit(1)
year, season = parts
season = season.lower()
if not year.isdigit() or len(year) != 4:
print(f"Error: Invalid year '{year}'")
sys.exit(1)
if season not in SEASON_NAMES:
print(f"Error: Invalid season '{season}'")
print(f"Valid options: {', '.join(SEASON_NAMES.keys())}")
sys.exit(1)
term = f"{year} {SEASON_NAMES[season]}"
# Check semester data file exists
semester_csv = os.path.join(SEMESTER_DATA_DIR, f"{year}_{season}.csv")
if not os.path.exists(semester_csv):
print(f"Error: Semester data file not found: {semester_csv}")
sys.exit(1)
# Load sections from semester data
print(f"Loading sections from {semester_csv}...")
sections = load_semester_data(semester_csv)
print(f"Found {len(sections)} sections")
# Handle resume mode
output_csv = os.path.join(GRADE_DATA_DIR, f"{year}_{season}.csv")
existing_class_nums = set()
if args.resume and os.path.exists(output_csv):
existing_df = pd.read_csv(output_csv)
existing_class_nums = set(existing_df["Class Num"].astype(str).tolist())
print(f"Resume mode: {len(existing_class_nums)} sections already scraped")
# Filter sections
if args.resume:
sections = [s for s in sections if s["class_num"] not in existing_class_nums]
if args.limit:
sections = sections[: args.limit]
print(f"Limiting to {args.limit} sections")
print(f"{len(sections)} sections to scrape")
if not sections:
print("No sections to scrape. Done.")
return
# Initialize browser
print("\nInitializing Chrome driver...")
driver = init_driver()
results = []
try:
print(f"Navigating to {TARGET_URL}...")
driver.get(TARGET_URL)
time.sleep(5) # Wait for Qlik app to load
for i, section in enumerate(sections):
class_num = section["class_num"]
instructor = section["instructor"]
print(
f"[{i + 1}/{len(sections)}] Class Num {class_num}...",
end=" ",
flush=True,
)
# Reset all filters before each section
clear_all_filters(driver)
# Set class number filter
if not set_filter(driver, BTN_CLASS_NUM, class_num, use_search=True):
print("Class Num NOT FOUND")
continue
# Set term filter
if not set_filter(driver, BTN_TERM, term, use_search=False):
print("Class Num NOT FOUND for term")
continue
time.sleep(2) # Wait for data to load
# Scrape data
row_data = scrape_section(driver, term, class_num, instructor)
if row_data and row_data.get("Subject"):
results.append(row_data)
print(
f"OK - {row_data['Subject']} {row_data['Catalog Number']} (GPA: {row_data['Course GPA']})"
)
else:
print("NO DATA")
except KeyboardInterrupt:
print("\n\nInterrupted by user. Saving partial results...")
except Exception as e:
print(f"\n\nCRITICAL ERROR: {e}")
print("Saving partial results...")
finally:
driver.quit()
# Write results
if results:
df = pd.DataFrame(results)
# Ensure all columns exist and are in correct order
for col in OUTPUT_COLUMNS:
if col not in df.columns:
df[col] = ""
df = df[OUTPUT_COLUMNS]
# Create output directory if needed
os.makedirs(os.path.dirname(output_csv), exist_ok=True)
# Write (append if resuming)
if args.resume and os.path.exists(output_csv):
df.to_csv(output_csv, mode="a", header=False, index=False)
else:
df.to_csv(output_csv, index=False)
print(f"\nSaved {len(results)} rows to {output_csv}")
else:
print("\nNo results to save.")
if __name__ == "__main__":
main()