-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasAPI.py
308 lines (245 loc) · 10.2 KB
/
canvasAPI.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import requests
import pprint
import os
API_KEY = "11224~UdjGS1igH5TPNiXmZv91UU5mDvu5k8h6b2vJqMHwPUPWTbYBSdiJa2wTMh1tOhrz"
# req = requests.get(API_URL, headers = {"Authorization": "Bearer 11224~xe1g377tlaOPI0lx5SBBVrmgpKZ9jH2WmUPoLn0NapADNNTkz6ufwmxbn2y5wfhT"})
# #print("Bearer {}".format(API_KEY))
# json_req = req.json()
# self_id = json_req["id"]
# print(self_id)
#To run file in the interpreter run ->
# ----------------- exec(open("canvasAPI.py").read()) ------------------------------#
'''
Student Class:
Instance of class, created from an Auth_Token specifically Bearer Token
Will up front request for information initially to create a "profile"
Profile will generate attributes for the instance as seen below
- Courses
- Assignments for each course
- Quizes for Courses
- Announcements
- Notifications
'''
'''
For paginated responses
while r.links['current']['url'] != r.links['last']['url']:
r = requests.get(r.links['next']['url'], headers=header)
page_response = r.json()
for p_response in page_response:
assignments_found_thus_far.append(p_response)
'''
def create_request_URL(init, resource_ep = [] , params = None):
ret = init
for r in resource_ep:
assert(type(r) == type(""))
ret += '/'
ret += r
if params:
assert (type(params) == type(dict()))
ret += "?"
for p in params:
assert(type(p) == type(""))
assert(type(params[p]) == type(""))
ret += p
ret += "="
ret += params[p]
ret += "&"
ret = ret[:-1]
return ret
class Student():
gateway = "https://canvas.ubc.ca/api/v1"
init_self_gateway = "{}/users/self".format(gateway)
def __init__(self,bearer_token):
assert(type(bearer_token) == type(""))
self.courses = {}
self.bearer_token = bearer_token
self.pp = pprint.PrettyPrinter(indent=4)
self.auth_header ={
'Authorization': 'Bearer {}'.format(self.bearer_token),
}
json_profile = requests.request("GET", self.init_self_gateway, headers = self.auth_header).json()
self.id = json_profile["id"]
self.name = json_profile["name"]
self.extra_links = {}
self.get_courses()
def add_extra_link(self, short_hand, link):
self.extra_links[short_hand] = link
def remove_extra_link(self, short_hand, link):
if short_hand in self.extra_links:
self.extra_links.pop(short_hand)
# will retrieve the
def get_courses(self, enrollment_type = "student", enrollment_state = "active"):
params = {"enrollment_type": "student",
"enrollment_state" : "active"
}
url = create_request_URL(init = self.gateway, resource_ep = ["courses"], params = params)
payload = {}
response = requests.request("GET", url, headers=self.auth_header, data = payload)
courses = response.json()
for c in courses:
Course_obj = Course(c, self.bearer_token)
self.courses[Course_obj.course_code] = Course_obj
# on all registerd courses for this student
# retrieve upcoming assignemnts
def update_assignments(self):
for c in self.courses:
Course_obj = self.courses[c]
Course_obj.update_assignments()
def remove_course(self, course_code):
if course_code not in self.courses.keys():
print("course_code not in courses")
return
self.courses.pop(course_code)
def get_account_notifications(self):
r_ep = ["accounts", "self", "account_notifications"]
url = create_request_URL(init = self.gateway, resource_ep = r_ep)
response = requests.request("GET", url, headers=self.auth_header)
r_ep = ["accounts" "self", "account_notifications"]
if response.status_code == 200:
modules = response.json()
return modules
else:
print(response.status_code)
class Course():
# single Course used within a Courses obj
# course_code e.g. "CPSC 320 101/102 2020W"
# calendar -> dictionary
# e.g.
# calendar": {
# "ics": "https://canvas.ubc.ca/feeds/calendars/course_hp0YU0SjrBMJgSWheRFlUCKJjUsMqK1IrtQtfEug.ics"
# }
# id, specific to access api resources related to particular course
# name (full name) e.g. "CPSC 320 101/102 2020W Intermediate Algorithm Design and Analysis"
gateway = "https://canvas.ubc.ca/api/v1/courses"
def __init__(self, Course, bearer_token):
keys = ["course_code", "calendar", "id", "name"]
values = []
self.bearer_token = bearer_token
for k in keys:
values.append(Course[k])
self.course_code = values[0]
self.calendar = values[1]
self.id = values[2]
self.name = values[3] # verbose
self.assignments = []
self.resource_ep = [str(self.id)]
self.auth_headers = {
'Authorization': 'Bearer {}'.format(self.bearer_token),
}
# extra_links
self.extra_links = {}
self.update_assignments()
# assumed course_code is <COURSE LETTER> <COURSE NUM>
# not neccessarily the case for non UBC courses, quite niave
def get_pretty_name(self):
x = self.course_code.split()
return (x[0], x[1])
# kind of a throw up method
def update_assignments(self):
response = self.get_assignments()
raw = response.json()
if response.status_code == 200:
self.assignments = []
for a in raw:
ass = Assignments(a, self.bearer_token)
self.assignments.append(ass)
while response.links['current']['url'] != response.links['last']['url']:
response = requests.request("GET", response.links["next"]["url"], headers=self.auth_headers)
raw = response.json()
for a in raw:
ass = Assignments(a, self.bearer_token)
self.assignments.append(ass)
else:
print(response.status_code)
def get_assignments(self, upcoming = True):
params = {"bucket" : "upcoming"} if upcoming else {}
r_ep = self.resource_ep.copy()
r_ep.append("assignments")
response = self.get_resource(params = params, r_ep = r_ep, header = self.auth_headers)
if response.status_code == 200:
return response
else:
print(response.status_code)
return None
def add_link(self, short_hand,link):
self.extra_links[short_hand] = link
def remove_link(self, short_hand):
if short_hand in self.extra_links:
self.extra_links.pop()
# kind of a throw up of everything just in case implemented
def get_activity_stream(self):
r_ep = self.resource_ep.copy()
r_ep += ["activity_stream"]
response = self.get_resource(r_ep = r_ep, header = self.auth_headers)
return_obj = []
if response.status_code == 200:
return response.json()
else:
print(response.status_code)
return None
def get_quizzes(self):
r_ep = self.resource_ep.copy()
r_ep += ["quizzes"]
response = self.get_resource(r_ep = r_ep, header = self.auth_headers)
def get_resource(self, params = {}, r_ep = [], payload = {}, header = None):
headers = self.auth_headers if header == None else header
url = create_request_URL(init = self.gateway, resource_ep = r_ep, params=params)
response = requests.request("GET", url, headers=headers, data = payload)
return response
# https://canvas.instructure.com/doc/api/modules.html
# module build
# lack of consistency between professors use of modules
# some use it to hold by topic
# others based on type of work e.g. reading
#
def get_modules(self):
r_ep = self.resource_ep.copy()
r_ep += ['modules']
response = self.get_resource(r_ep = r_ep, header = self.auth_headers)
if response.status_code == 200:
modules = response.json()
return modules
else:
print(response.status_code)
def remove_assignment(self, ass_id):
for a in self.assignments:
pass
# this is quite underwhleming at least for my account as I am not
# seeing anything directly related to my courses
def __repr__(self):
return str(vars(self))
class Assignments():
# ATTRIBUTE EXPLAIN
# description is in html, perhaps strip or use when outputting as a bot
# due_at is date formated as such e.g. 2020-10-05T02:00:00Z
# html_url, url directly to resource
# id assignment id, useful for further api calls
# submission_types -> array of strings descrbing submission types
def __init__(self, ass, bearer_token):
keys = ["description", "due_at", "html_url", "id", "name", "submission_types"]
values = []
self.bearer_token = bearer_token
for k in keys:
values.append(ass[k])
self.description = values[0]
self.due_at = values[1]
self.html_url = values[2]
self.id = values[3]
self.name = values[4]
self.submission_types = values[5]
def __repr__(self):
return str(vars(self))
sa = Student(API_KEY)
pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(sa.courses)
# TODO
# Remove a course from the listing
# add a course?
# For a course get the relevant asingments
def print_ass(student):
for c in student.courses:
course = student.courses[c]
assignments = course.assignments
for a in assignments:
print(course.name, a.name)
print_ass(sa)