-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_external_tools.py
54 lines (47 loc) · 1.76 KB
/
get_external_tools.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
# GET external_tool items from a course from the Canvas API, produces a spreadsheet with the external tool ids
# Please note that this does not include support for pagination if there are more than 100 lti tools in a course
import requests
import json
import csv
import operator
#from course_list import course_list
#from secure import oauth_token
from secure import oauth_token_exed
# oauth_token is the oauth token from Canvas generated by a user with privileges for the courses
oauth_token = oauth_token_exed
course_list = []
# INSERT HERE list of courses
#domain = 'https://harvard.instructure.com'
domain = 'https://harvard-catalog-courses.instructure.com'
data = []
# list of data we want from the API call
counter = 0
new_counter = 0
for course_id in course_list:
# loop over all of the courses in the course list
url = '%s/api/v1/courses/%s/external_tools?per_page=100' % (str(domain), str(course_id))
# call the API and raise exceptions as needed
headers = {
'Authorization': 'Bearer {}'.format(oauth_token),
}
try:
resp = requests.get(url, headers=headers )
resp.raise_for_status()
except requests.exceptions.RequestException as e:
print e
raise
# save json response as a list of objects
api_response = resp.json()
# go through each json object
for position in api_response:
external_tool_id = position.get('id')
link = position.get('custom_fields')
# position.get('id') will return none if there none
# looking for all of the ids
data.append([course_id, external_tool_id, link])
counter = counter + 1
print counter
# Writing the information from data to a CSV file
with open('external_tools.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(data)