Last active
August 29, 2015 13:56
-
-
Save rickcnagy/9101794 to your computer and use it in GitHub Desktop.
Download gradebook data from assignments spanning a certain date range and/or from a certain teacher. Data can then be uploaded via upload_gradebook_data.py.
This file contains hidden or 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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python | |
''' | |
Directions: | |
- make Q1 the active semester | |
- run this script to download all assignments from Q1 in the time range | |
- activate Q2 | |
- run upload_gradebook_data.py to upload to Q2 | |
- reactivate Q1 | |
- run delete_assignments.py to delete the assignments posted to Q2 | |
currently dumps all logs to a JSON file upon completion. | |
TODO: | |
Make logs dump in real time | |
''' | |
import requests | |
from tqdm import * | |
from datetime import datetime | |
import pickle | |
import random | |
import sys | |
import qs | |
""" | |
filters for all assignments in a date range, inclusive of start + end date | |
format like 2013-10-28 | |
""" | |
start_date = '2014-02-15' | |
end_date = '2015-01-01' | |
teacher_id = None | |
def main(): | |
global api_key | |
global base_url | |
# ========= | |
# = Setup = | |
# ========= | |
# filename like "rwcap_23.p" | |
qs.set_filename('{}_{}.p'.format(qs.schoolcode(), str(random.randint(1, 100)))) | |
base_url = qs.get_base_url() | |
qs.check(start_date) | |
# get all sections | |
sections = qs.get_all_sections() | |
# structured data like {section id: assignment data, etc} | |
late_assignments = {} | |
success_assignment = [] | |
error_assignment = [] | |
success_grade = [] | |
error_grade = [] | |
grade_count = 0 | |
assignment_count = 0 | |
# ============================= | |
# = Loop through each section = | |
# ============================= | |
for i in trange(0, len(sections), desc='GET', leave=True): | |
section_id = sections[i]['id'] | |
# use this to filter for a specific section for testing | |
# if not section_id == '624516': continue | |
late_assignments[section_id] = [] | |
s = requests.get("{}sections/{}/assignments".format(base_url, | |
section_id), | |
params=qs.param()) | |
json = s.json() | |
log = {'section_id': section_id, 'response': json} | |
if (json['success']): | |
success_assignment.append(qs.ser(json, {'sectionId': section_id})) | |
# ================================ | |
# = Loop through each assignment = | |
# ================================ | |
for assignment in json['list']: | |
# ===================================== | |
# = Get grades from valid assignments = | |
# ===================================== | |
if valid_assignment(assignment, sections[i]): | |
g, data = get_grades(section_id, assignment['id']) | |
log = qs.ser(g.json(), data) | |
if 'false' in g.text: | |
error_grade.append(log) | |
else: | |
success_grade.append(log) | |
# ignore assignments with no grades | |
if len(g.json()) != 0: | |
total_marks = assignment['totalMarksPossible'] | |
category_id = assignment['categoryId'] | |
assignment_dict = {'name': assignment['name'], | |
'date': assignment['date'], | |
'totalMarksPossible': total_marks, | |
'columnCategoryId': category_id, | |
'grades': g.json(), | |
'sectionId': section_id, | |
'assignmentId': assignment['id']} | |
late_assignments[section_id].append(assignment_dict) | |
assignment_count += 1 | |
grade_count += len(g.json()) | |
else: | |
error_assignment.append(log) | |
# remove the empty array if found no assignments | |
if len(late_assignments[section_id]) == 0: | |
del late_assignments[section_id] | |
# =========================== | |
# = Output results and logs = | |
# =========================== | |
print ("found {} assignments and {} grades between {} and {}, " | |
"spanning {} sections. {} errors, {} successeful assignments, " | |
"{} successful grades." | |
"".format( | |
assignment_count, grade_count, start_date, end_date, | |
len(late_assignments), len(error_assignment) + len(error_grade), | |
len(success_assignment), len(success_grade), len(success_grade))) | |
pickle.dump(late_assignments, open(qs.get_filename(), 'w')) | |
print 'dumped late assignments to file: {}'.format(qs.get_filename()) | |
qs.dump_logs({'grades': success_grade, 'assignments': success_assignment}, | |
{'grades': error_grade, 'assignments': error_assignment}) | |
# filter for after compare date and not a formula (which have blank category ID) | |
def valid_assignment(assignment, section): | |
valid = True | |
start = datetime.strptime(start_date, '%Y-%m-%d') | |
end = datetime.strptime(end_date, '%Y-%m-%d') | |
assignment_date = datetime.strptime(assignment['date'], '%Y-%m-%d') | |
first_teacher_id = section['teachers'][0]['id'] | |
if assignment_date < start: | |
return False | |
elif assignment_date > end: | |
return False | |
elif not 'categoryId' in assignment.keys(): | |
return False | |
elif teacher_id and not first_teacher_id == teacher_id: | |
return False | |
return True | |
def get_grades(section_id, assignment_id): | |
data = {'sectionId': section_id, | |
'assignmentId': assignment_id, | |
'apiKey': qs.api_key, | |
'itemsPerPage': 1000} | |
g = requests.get(base_url + 'grades', params=data) | |
return (g, data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment