Created
March 30, 2017 18:47
-
-
Save cmurtaugh/07e9b59c695898315497a603178dde69 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
# This script is only a draft | |
import requests | |
import json | |
import pprint | |
# Based on https://pastebin.com/CWRu70T6 | |
# API reference https://canvas.instructure.com/doc/api/submissions.html | |
BASE_URL = "https://institution.instructure.com/api/v1" | |
access_token = 'token goes here' | |
course_id = 'course id goes here' # not the sis_id but the canvas internal id | |
REQUEST_HEADERS = {'Authorization':'Bearer %s' % access_token} | |
# First, get the list of students in the course | |
students_endpoint = BASE_URL + '/courses/%s/students' % (course_id) | |
# Create a request, adding the REQUEST_HEADERS to it for authentication | |
not_done = True | |
students = [] | |
url = students_endpoint | |
while not_done: | |
student_request = requests.get(url,headers=REQUEST_HEADERS) | |
students+=student_request.json() | |
if 'next' in student_request.links.keys(): | |
url = student.request.links['next']['href'] | |
else: | |
not_done = False | |
print 'done getting students',len(students),'students' | |
# Load the response as JSON | |
response_data = student_request.json() | |
# Exit if there were no students in the returned data | |
if not response_data: | |
print 'Sorry, there were no students registered in the course.' | |
exit(0) | |
# Loop through the students, populating the student_ids list with their canvas ids | |
student_ids = [s['id'] for s in response_data] | |
# break the list of student_ids into chunks of no more than 50 | |
ids_per_chunk = 50 | |
chunked_student_ids = [student_ids[i:i+ids_per_chunk] for i in range(0, len(student_ids), ids_per_chunk)] | |
# Build the endpoint for requesting submissions | |
submissions_endpoint = BASE_URL + '/courses/%s/students/submissions' % (course_id) | |
# Build the GET request parameters that are needed to fetch the submissions along with the | |
# total scores (grades) | |
submission_params = {'include[]':'total_scores','grouped':1} | |
all_grades = [] | |
for chunk_ids in chunked_student_ids: | |
submission_params['student_ids[]'] = chunk_ids | |
# Build a request, adding the REQUEST_HEADERS to it for authentication | |
req = requests.get(submissions_endpoint, params=submission_params, headers=REQUEST_HEADERS) | |
# Load the response as JSON | |
grades = json.loads(req.text) | |
# add these grades to the full list | |
all_grades.extend(grades) | |
# Print every final score in the list | |
for i in range(len(all_grades)): | |
print "Grade " + str(i+1) + ": "+ str(grades[i]['computed_final_score']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment