Created
June 2, 2016 00:52
-
-
Save ryanlovett/f79ff4da786ae8eae504e1201d14b700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import os | |
import json | |
import requests | |
import argparse | |
def get_courses(term_id, uid, sa_filter=''): | |
'''Fetch a UID's enrollments in a specified term. Optionally filter by | |
a subject area (e.g. STAT).''' | |
uri = BASE_URI + "/{}/students/{}".format(term_id, uid) | |
r = requests.get(uri, params=PARAMS, headers=HEADERS) | |
data = r.json() | |
courses = [] | |
# Return if the UID has no enrollments | |
if 'enrollments' not in data['apiResponse']['response']['any']: | |
return(courses) | |
for e in data['apiResponse']['response']['any']['enrollments']: | |
course = e['classSection']['clazz']['course'] | |
# We skip courses in other units | |
subject_area = course['subjectArea']['code'] | |
if sa_filter and sa_filter != subject_area: | |
continue | |
# The API gives us data on both the lecture and section. We | |
# only care about the lecture. | |
if e['classSection']['component']['code'] != 'LEC': continue | |
if sa_filter: | |
# If we're filtering by subject area, we don't need the prefix. | |
# Courses are of the form 123 | |
course_fmt = course['catalogNumber']['number'] | |
else: | |
# Courses are of the form SUBJECTAREA123 | |
course_fmt = subject_area + course['catalogNumber']['number'] | |
courses.append(course_fmt) | |
return(courses) | |
## MAIN | |
TERM_FALL_2016 = 2168 | |
# Headers file contains requests json. Example: | |
# { "Accept": "application/json", | |
# "app_id": "YOUR_SIS_APP_ID", | |
# "app_key": "YOUR_SIS_APP_KEY" } | |
HEADERS_FILE = '/var/something/enrollment_creds.json' | |
parser = argparse.ArgumentParser(description='Print student enrollments.') | |
parser.add_argument('-t', dest='term_id', type=int, default=TERM_FALL_2016, | |
help='SIS Term ID (default=%(default)s)') | |
parser.add_argument('-s', dest='sa_filter', default='', | |
help='subject area filter') | |
parser.add_argument('-H', dest='headers_file', default=HEADERS_FILE, | |
help='JSON file containing requests headers') | |
parser.add_argument('uids', nargs=argparse.REMAINDER, help='CalNet UIDs') | |
args = parser.parse_args() | |
# Details for SIS Enrollment API | |
BASE_URI = "https://apis.berkeley.edu/uat/sis/v1/enrollments/terms" | |
PARAMS = { | |
"id-type": "campus-uid", | |
"enrolled-only": "true" | |
} | |
HEADERS = json.loads(open(args.headers_file).read()) | |
for uid in args.uids: | |
courses = get_courses(args.term_id, uid, args.sa_filter) | |
if len(courses) == 0: continue | |
print('{}: {}'.format(uid, ' '.join(courses))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment