This repository was archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcanvas-files-downloader.py
executable file
·185 lines (163 loc) · 6.88 KB
/
canvas-files-downloader.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
from decouple import config
import os
import requests
import urllib.request
ACCESS_TOKEN = config('ACCESS_TOKEN')
headers = {'Authorization': 'Bearer ' + ACCESS_TOKEN}
# TODO make psu.instructure.com changeable
# TODO refactor code
root_directory = os.getcwd()
files_directory = os.path.join(root_directory, 'files')
courses_directory = os.path.join(files_directory, 'courses')
if not os.path.exists(courses_directory):
os.makedirs(courses_directory)
print('\n\n\n')
courses = []
# get courses
next_url = 'https://psu.instructure.com/api/v1/courses'
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
current_courses = response.json()
for current_course in current_courses:
courses.append(current_course)
print(current_course['name'], current_course['id'])
print('\n\n\n')
# download files for a course
for course in courses:
next_url = 'https://psu.instructure.com/api/v1/courses/{}/files'.format(course['id'])
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
files = response.json()
course_directory = os.path.join(courses_directory, course['name'])
if not os.path.exists(course_directory):
os.makedirs(course_directory)
for f in files:
print(f['display_name'], f['url'], f['id'])
f_path = os.path.join(course_directory, f['display_name'])
if not os.path.exists(f_path):
urllib.request.urlretrieve(f['url'], f_path)
groups_directory = os.path.join(files_directory, 'groups')
if not os.path.exists(groups_directory):
os.makedirs(groups_directory)
print('\n\n\n')
groups = []
# get groups
next_url = 'https://psu.instructure.com/api/v1/users/self/groups'
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
current_groups = response.json()
for current_group in current_groups:
groups.append(current_group)
print(current_group['name'], current_group['id'])
print('\n\n\n')
# download files for a group
for group in groups:
next_url = 'https://psu.instructure.com/api/v1/groups/{}/files'.format(group['id'])
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
files = response.json()
group_directory = os.path.join(groups_directory, '{} {}'.format(group['name'].replace('/', '-'), group['id']))
if not os.path.exists(group_directory):
os.makedirs(group_directory)
for f in files:
print(f['display_name'], f['url'], f['id'])
f_path = os.path.join(group_directory, f['display_name'])
if not os.path.exists(f_path):
urllib.request.urlretrieve(f['url'], f_path)
group_users = []
next_group_users_url = 'https://psu.instructure.com/api/v1/groups/{}/users'.format(group['id'])
while next_group_users_url:
response = requests.get(next_group_users_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_group_users_url = None
for link in links:
if link['rel'] == 'next':
next_group_users_url = link['url']
break
current_group_users = response.json()
for current_group_user in current_group_users:
group_users.append(current_group_user)
group_users_file_path = os.path.join(group_directory, '{} {} users.txt'.format(group['name'].replace('/', '-'), group['id']))
if not os.path.exists(group_users_file_path):
group_users_file = open(group_users_file_path, 'w')
for group_user in group_users:
group_users_file.write('{} {}\n'.format(group_user['name'], group_user['id']))
group_users_file.close()
submissions_directory = os.path.join(files_directory, 'submissions')
if not os.path.exists(submissions_directory):
os.makedirs(submissions_directory)
print('\n\n\n')
submissions = []
# get course submissions
next_url = 'https://psu.instructure.com/api/v1/users/self/folders'
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
current_submissions = response.json()
for current_submission in current_submissions:
submissions.append(current_submission)
print(current_submission['name'], current_submission['id'])
print('\n\n\n')
# download course submissions
for submission in submissions:
next_url = 'https://psu.instructure.com/api/v1/folders/{}/files'.format(submission['id'])
while next_url:
response = requests.get(next_url, headers=headers)
if not 'Link' in response.headers:
break
links = requests.utils.parse_header_links(response.headers['Link'].rstrip('>').replace('>,<', ',<'))
next_url = None
for link in links:
if link['rel'] == 'next':
next_url = link['url']
break
files = response.json()
submission_directory = os.path.join(submissions_directory, submission['name'])
if not os.path.exists(submission_directory):
os.makedirs(submission_directory)
for f in files:
print(f['display_name'], f['url'], f['id'])
f_path = os.path.join(submission_directory, f['display_name'])
if not os.path.exists(f_path):
urllib.request.urlretrieve(f['url'], f_path)