Created
July 29, 2019 14:18
-
-
Save ttimbers/9a2d6bd0d1289e88df32bd7bbda8b04e to your computer and use it in GitHub Desktop.
Example of how to get a homework assignment (name worksheet 10) from Canvas API and put it in the correct directory for grading with nbgrader
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
import os | |
import shutil | |
import requests | |
import urllib | |
from urllib.request import urlretrieve | |
canvas_token = os.environ["CANVAS_TOKEN"] | |
resp = requests.get( | |
url=f"https://canvas.ubc.ca/api/v1/courses/19078/assignments/313587/gradeable_students", | |
headers={ | |
"Authorization": f"Bearer {canvas_token}", | |
"Accept": "application/json+canvas-string-ids" | |
}, | |
json={ | |
"per_page": "100" | |
}, | |
) | |
students = resp.json() | |
student_list = [] | |
for student in students: | |
student_list.append(student['id']) | |
for student in student_list: | |
try: | |
os.mkdir("submitted") | |
except: | |
pass | |
try: | |
os.mkdir(os.path.join("submitted", student)) | |
except: | |
pass | |
try: | |
os.mkdir(os.path.join("submitted", student, "worksheet_10")) | |
except: | |
pass | |
for student in student_list: | |
resp = requests.get( | |
url=f"https://canvas.ubc.ca/api/v1/courses/19078/assignments/313587/submissions/{student}", | |
headers={ | |
"Authorization": f"Bearer {canvas_token}", | |
"Accept": "application/json+canvas-string-ids" | |
}, | |
json={ | |
"per_page": "100" | |
}, | |
) | |
assignments = resp.json() | |
try: | |
attachment = assignments['attachments'] | |
urlretrieve (attachment[0]['url'], os.path.join("submitted", student, "worksheet_10/worksheet_10.ipynb")) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment