-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcanvas.py
More file actions
66 lines (51 loc) · 2.01 KB
/
canvas.py
File metadata and controls
66 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Canvas CLI
Usage:
canvas.py generate --token=<access_token> [--output=<file>]
Options:
-h --help Shows this screen
--token=<access_token> Canvas access token (generate in Account>Settings>New Access Token)
-o --output=<file> Path to output the generated file [default: ./output.csv]
"""
import requests
import json
import csv
from docopt import docopt
from tqdm import tqdm
BASE_URL = "https://kingalfred.instructure.com/api/v1/"
def get_courses(token):
"""GET /api/v1/courses"""
COURSES_PATH = "courses"
r = requests.get(BASE_URL + COURSES_PATH,
headers={"Authorization": "Bearer " + token})
r.raise_for_status()
return json.loads(r.text)
def get_assignments(course_id, token):
"""GET /api/v1/courses/:course_id/assignments"""
ASSIGNMENTS_PATH = "courses/" + str(course_id) + "/assignments"
r = requests.get(BASE_URL + ASSIGNMENTS_PATH,
headers={"Authorization": "Bearer " + token})
r.raise_for_status()
return json.loads(r.text)
def write_assignment(writer, assignment, course):
writer.writerow({
'course_id': course['id'],
'course_name': course['name'],
'assignment_id': assignment['id'],
'assignment_name': assignment['name'],
'assignment_created': assignment['created_at'],
'assignment_due': assignment['due_at']
})
if __name__ == '__main__':
arguments = docopt(__doc__)
token = arguments['--token']
output = arguments['--output']
courses = get_courses(token)
with open(output, "w") as file:
fieldnames = ["course_id", "course_name",
"assignment_id", "assignment_name", "assignment_created", "assignment_due"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for course in tqdm(courses):
assignments = get_assignments(course['id'], token=token)
for assignment in assignments:
write_assignment(writer, assignment, course)