Advertisement
mhanes

Pull Grades

Mar 30th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. # This script is only a draft
  2. import requests
  3. import json
  4. import pprint
  5.  
  6. # API reference https://canvas.instructure.com/doc/api/submissions.html
  7.  
  8. BASE_URL = "https://institution.instructure.com/api/v1"
  9. access_token = 'token goes here'
  10. course_id = 'course id goes here'  # not the sis_id but the canvas internal id
  11.  
  12. REQUEST_HEADERS = {'Authorization':'Bearer %s' % access_token}
  13.  
  14. # First, get the list of students in the course
  15.  
  16. students_endpoint = BASE_URL + '/courses/%s/students' % (course_id)
  17.  
  18. # Create a request, adding the REQUEST_HEADERS to it for authentication
  19. not_done = True
  20. students = []
  21. url = students_endpoint
  22. while not_done:
  23.   student_request = requests.get(url,headers=REQUEST_HEADERS)
  24.   students+=student_request.json()
  25.   if 'next' in student_request.links.keys():
  26.     url = student.request.links['next']['href']
  27.   else:
  28.     not_done = False
  29.  
  30. print 'done getting students',len(students),'students'
  31. # Load the response as JSON
  32. response_data = student_request.json()
  33.  
  34. # Exit if there were no students in the returned data
  35. if not response_data:
  36.   print 'Sorry, there were no students registered in the course.'
  37.   exit(0)
  38.  
  39. # Loop through the students, populating the student_ids list with their canvas ids
  40. student_ids = [s['id'] for s in response_data]
  41.  
  42.  
  43. # Build the endpoint for requesting submissions
  44. submissions_endpoint = BASE_URL + '/courses/%s/students/submissions' % (course_id)
  45.  
  46. # Build the GET request parameters that are needed to fetch the submissions along with the
  47. # total scores (grades)
  48. submission_params = {'include[]':'total_scores','grouped':1}
  49. submission_params['student_ids[]'] = student_ids
  50.  
  51. # Build a request, adding the REQUEST_HEADERS to it for authentication
  52. req = requests.get(submissions_endpoint, params=submission_params, headers=REQUEST_HEADERS)
  53.  
  54. # Load the response as JSON
  55. grades = json.loads(req.text)
  56.  
  57. # Print every final score in the list
  58. for i in range(len(grades)):
  59.     print "Grade " + str(i+1) + ": "+  str(grades[i]['computed_final_score'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement