-
Notifications
You must be signed in to change notification settings - Fork 3
/
canvas_download.py
executable file
·196 lines (146 loc) · 5.03 KB
/
canvas_download.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
#!/usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor
import requests
import subprocess
import dataclasses
import sys
import os
from pathlib import Path
import logging
cred = os.environ.get('CANVAS_API_KEY') or subprocess.check_output([
'kwallet-query', '-r', 'apikey', 'kdewallet', '-f', 'canvas'
]).strip().decode()
api_base = os.environ.get('CANVAS_API_BASE') or subprocess.check_output([
'kwallet-query', '-r', 'api_base', 'kdewallet', '-f', 'canvas'
]).strip().decode()
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
fmt = logging.Formatter('{asctime} {levelname} {name}: {message}',
datefmt='%b %d %H:%M:%S',
style='{')
hand = logging.StreamHandler()
hand.setFormatter(fmt)
log.addHandler(hand)
def api(method, endpoint: str, json=True, **kwargs):
log.info('http %s %s', method, endpoint)
if not endpoint.startswith('https'):
endpoint = api_base + endpoint
resp = requests.request(method,
endpoint,
headers={'Authorization': f'Bearer {cred}'},
**kwargs)
resp.raise_for_status()
if json:
return resp.json()
else:
return resp
def paginate(method: str, url: str):
while True:
resp = api(method, url, json=False)
yield from resp.json()
next_one = resp.links.get('next')
if not next_one:
return
url = next_one.get('url')
if not url:
return
def course_modules(course_id):
return paginate('GET', f'/api/v1/courses/{course_id}/modules')
def module_items(course_id, module_id):
return paginate('GET',
f'/api/v1/courses/{course_id}/modules/{module_id}/items')
def course_file(course_id, file_id):
return api('GET', f'/api/v1/courses/{course_id}/files/{file_id}')
class DataClassUnpack:
"""Taken from: https://stackoverflow.com/a/72164665"""
classFieldCache = {}
@classmethod
def instantiate(cls, classToInstantiate, argDict):
if classToInstantiate not in cls.classFieldCache:
cls.classFieldCache[classToInstantiate] = {
f.name
for f in dataclasses.fields(classToInstantiate) if f.init
}
fieldSet = cls.classFieldCache[classToInstantiate]
filteredArgDict = {k: v for k, v in argDict.items() if k in fieldSet}
return classToInstantiate(**filteredArgDict)
@dataclasses.dataclass
class FileObj:
url: str
display_name: str
filename: str
def download(self, to_dir):
to_dir = Path(to_dir)
name = to_dir / self.display_name.replace('/', '%')
if name.exists():
return
# streaming download
with api('GET', self.url, json=False, stream=True) as resp:
with open(name, 'wb') as fh:
for chunk in resp.iter_content(chunk_size=32 * 1024):
fh.write(chunk)
@dataclasses.dataclass
class ModuleItem:
id: int
title: str
url: str
@dataclasses.dataclass
class Module:
id: int
name: str
state: str
def do_file(url, to_dir):
file = api('GET', url)
fobj = DataClassUnpack.instantiate(FileObj, file)
fobj.download(to_dir)
def download_all_from_module(tpe: ThreadPoolExecutor, to_dir, course_id,
module_id):
futs = []
for item in module_items(course_id, module_id):
item = DataClassUnpack.instantiate(ModuleItem, item)
futs.append(tpe.submit(do_file, item.url, to_dir))
for fut in futs:
try:
fut.result()
except Exception:
log.exception('downloading file')
def cli_module(args):
with ThreadPoolExecutor(max_workers=5) as tpe:
download_all_from_module(tpe, args.to_dir, args.course_id,
args.module_id)
def cli_list_modules(args):
for mod in course_modules(args.course_id):
mod = DataClassUnpack.instantiate(Module, mod)
print(mod)
def cli_download_file(args):
file = course_file(args.course_id, args.file_id)
fobj = DataClassUnpack.instantiate(FileObj, file)
fobj.download(args.to_dir)
def main():
import argparse
ap = argparse.ArgumentParser()
sps = ap.add_subparsers()
def fail(*args):
ap.print_help()
sys.exit(1)
ap.set_defaults(cmd=fail)
mod = sps.add_parser('module')
mod.set_defaults(cmd=cli_module)
mod.add_argument('course_id')
mod.add_argument('module_id')
mod.add_argument('--to-dir', '-o')
mod = sps.add_parser('list-modules')
mod.set_defaults(cmd=cli_list_modules)
mod.add_argument('course_id')
file = sps.add_parser('download-file')
file.set_defaults(cmd=cli_download_file)
file.add_argument('course_id')
file.add_argument('file_id')
file.add_argument('--to-dir',
'-o',
help='Directory to download the file to',
required=True)
args = ap.parse_args()
args.cmd(args)
if __name__ == '__main__':
main()