-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial_downloader.py
More file actions
362 lines (291 loc) · 12.5 KB
/
Copy pathmaterial_downloader.py
File metadata and controls
362 lines (291 loc) · 12.5 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""
Canvas Course Material Downloader
For each active course:
1. Enumerate all files via Canvas API (preserving folder structure)
2. Estimate total size
- If < 1 GB → download everything
- If >= 1 GB → use Claude AI to identify lecture notes & tutorials only
3. Download selected files into [course_id]/materials/<subfolder>/
4. Maintain a per-course download log so already-downloaded files are skipped
Directory layout:
[project]/[course_id]/materials/<canvas_subfolder>/<filename>
[project]/[course_id]/materials/download_log.json
Usage:
python material_downloader.py # process all active courses
python material_downloader.py -c 85427 # process one course by id
python material_downloader.py --list # list courses + estimated sizes only
"""
import json
import os
import re
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
import requests
from canvasapi import Canvas
# ── Configuration ────────────────────────────────────────────────────────────
CANVAS_URL = "https://canvas.nus.edu.sg"
ANTHROPIC_API_KEY = None # set below or via env
PROJECT_DIR = Path(__file__).parent
_canvas_token_file = PROJECT_DIR / "canvas_token.txt"
CANVAS_TOKEN = (
_canvas_token_file.read_text().strip()
if _canvas_token_file.exists() else
os.environ.get("CANVAS_TOKEN", "")
)
SIZE_LIMIT = 1 * 1024 ** 3 # 1 GB
# Skip training / non-academic courses by name keywords
SKIP_COURSE_KEYWORDS = [
"training", "pdp", "rmcpdp", "osa", "soct", "travel", "essentials",
"respect", "consent",
]
# ── Helpers ───────────────────────────────────────────────────────────────────
def _headers() -> dict:
return {"Authorization": f"Bearer {CANVAS_TOKEN}"}
def sanitize(name: str) -> str:
"""Make a string safe for use as a file/directory name."""
return re.sub(r'[\\/*?:"<>|]', "_", name).strip()
def load_log(log_path: Path) -> dict:
if log_path.exists():
with open(log_path) as f:
return json.load(f)
return {}
def save_log(log_path: Path, log: dict) -> None:
log_path.parent.mkdir(parents=True, exist_ok=True)
with open(log_path, "w") as f:
json.dump(log, f, indent=2)
# ── Canvas file enumeration ───────────────────────────────────────────────────
def get_course_files(course) -> list[dict]:
"""
Return all files in a course as a list of dicts:
id, display_name, size, url, folder_path, mime_class
folder_path is relative (strips leading "course files/").
"""
# Build folder_id → relative path map
folder_map: dict[int, str] = {}
try:
for folder in course.get_folders():
full = folder.full_name # e.g. "course files/LectureNotes"
rel = full.removeprefix("course files").lstrip("/")
folder_map[folder.id] = rel
except Exception as e:
print(f" [warn] folders: {e}")
files = []
try:
for f in course.get_files():
folder_rel = folder_map.get(f.folder_id, "")
files.append({
"id": f.id,
"display_name": f.display_name,
"filename": f.filename,
"size": getattr(f, "size", 0) or 0,
"url": f.url,
"mime_class": getattr(f, "mime_class", ""),
"folder_path": folder_rel, # e.g. "LectureNotes"
"updated_at": getattr(f, "updated_at", ""),
})
except Exception as e:
print(f" [warn] files: {e}")
return files
# ── AI classification ─────────────────────────────────────────────────────────
def classify_with_ai(files: list[dict], course_name: str) -> list[dict]:
"""
Ask Claude to identify which files are lecture notes or tutorial materials.
Returns the filtered list.
"""
import anthropic
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
# Build a compact listing for the prompt
lines = []
for f in files:
path = f["folder_path"]
name = f["display_name"]
mb = f["size"] / 1024 / 1024
lines.append(f' id={f["id"]} folder="{path}" name="{name}" size={mb:.1f}MB')
file_listing = "\n".join(lines)
prompt = f"""You are helping organize university course materials for: {course_name}
Here is a list of all available files:
{file_listing}
Task: identify which files are LECTURE NOTES or TUTORIAL materials.
- Lecture notes: slides, lecture PDFs, notes, handouts, readings
- Tutorials: tutorial sheets, problem sets, worksheets, exercises, lab guides
Return ONLY a JSON array of the integer file IDs to download. No explanation.
Example: [123, 456, 789]"""
response = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=512,
messages=[{"role": "user", "content": prompt}],
)
text = response.content[0].text.strip()
# Extract JSON array from response
match = re.search(r'\[[\d,\s]+\]', text)
if not match:
print(" [warn] AI returned unexpected format; downloading all files")
return files
selected_ids = set(json.loads(match.group()))
selected = [f for f in files if f["id"] in selected_ids]
print(f" AI selected {len(selected)}/{len(files)} files (lecture notes + tutorials)")
return selected
# ── File download ─────────────────────────────────────────────────────────────
def download_file(file_info: dict, course_id: int, log: dict) -> bool:
"""
Download a single file to [course_id]/materials/<folder_path>/<filename>.
Returns True on success (or already-downloaded skip).
Updates log in-place.
"""
fid = str(file_info["id"])
name = file_info["display_name"]
# Skip if already downloaded and file still exists
if fid in log:
existing = Path(log[fid]["path"])
if existing.exists():
print(f" [skip] {name}")
return True
# Build destination path
folder_rel = file_info["folder_path"]
dest_dir = PROJECT_DIR / str(course_id) / "materials"
if folder_rel:
dest_dir = dest_dir / Path(*[sanitize(p) for p in folder_rel.split("/")])
dest_dir.mkdir(parents=True, exist_ok=True)
dest_path = dest_dir / sanitize(name)
# Download with streaming
try:
r = requests.get(
file_info["url"],
headers=_headers(),
stream=True,
timeout=60,
allow_redirects=True,
)
r.raise_for_status()
size = file_info["size"] or 0
written = 0
with open(dest_path, "wb") as fh:
for chunk in r.iter_content(chunk_size=65536):
if chunk:
fh.write(chunk)
written += len(chunk)
if size:
pct = written * 100 // size
print(f"\r Downloading {name[:50]:<50} {pct:3d}%", end="", flush=True)
print(f"\r Downloaded {name[:50]:<50} {written//1024:>6} KB")
log[fid] = {
"display_name": name,
"folder": folder_rel,
"size": written,
"path": str(dest_path),
"downloaded_at": datetime.now(timezone.utc).isoformat(),
}
return True
except Exception as e:
print(f"\n [error] {name}: {e}")
if dest_path.exists():
dest_path.unlink()
return False
# ── Per-course orchestration ──────────────────────────────────────────────────
def process_course(course, force_all: bool = False) -> tuple[int, int]:
"""
Download materials for one course.
Returns (downloaded, skipped) counts.
"""
cid = course.id
cname = course.name
print(f"\n{'='*70}")
print(f"Course: {cname} (id={cid})")
print(f"{'='*70}")
log_path = PROJECT_DIR / str(cid) / "materials" / "download_log.json"
log = load_log(log_path)
print(" Enumerating files...")
files = get_course_files(course)
if not files:
print(" No files found.")
return 0, 0
total_size = sum(f["size"] for f in files)
total_mb = total_size / 1024 / 1024
print(f" {len(files)} file(s) | estimated size: {total_mb:.1f} MB")
# Decide which files to download
if force_all or total_size < SIZE_LIMIT:
if total_size >= SIZE_LIMIT:
print(f" Size {total_mb:.0f} MB >= 1 GB limit — but force_all requested")
else:
print(f" Size < 1 GB — downloading all files")
to_download = files
else:
print(f" Size {total_mb:.0f} MB >= 1 GB — using AI to select lecture notes & tutorials")
to_download = classify_with_ai(files, cname)
downloaded = skipped = errors = 0
for f in to_download:
fid = str(f["id"])
if fid in log and Path(log[fid]["path"]).exists():
skipped += 1
print(f" [skip] {f['display_name']}")
continue
if download_file(f, cid, log):
downloaded += 1
else:
errors += 1
# Save log after every file so crashes don't lose progress
save_log(log_path, log)
save_log(log_path, log)
print(f"\n Done: {downloaded} downloaded, {skipped} skipped, {errors} errors")
return downloaded, skipped
# ── Main entry point ──────────────────────────────────────────────────────────
def is_academic_course(course) -> bool:
"""Filter out training / non-academic Canvas shells."""
try:
name = course.name.lower()
except AttributeError:
return False
return not any(kw in name for kw in SKIP_COURSE_KEYWORDS)
def main() -> None:
import argparse
import os
global ANTHROPIC_API_KEY
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
if not ANTHROPIC_API_KEY:
# Try to read from project dir
key_file = PROJECT_DIR / "anthropic_key.txt"
if key_file.exists():
ANTHROPIC_API_KEY = key_file.read_text().strip()
parser = argparse.ArgumentParser(description="Canvas Course Material Downloader")
parser.add_argument("-c", "--course", type=int, metavar="ID",
help="Process a single course by Canvas course ID")
parser.add_argument("--list", action="store_true",
help="List courses and estimated sizes, then exit")
parser.add_argument("--all", dest="force_all", action="store_true",
help="Download all files regardless of size limit")
args = parser.parse_args()
canvas = Canvas(CANVAS_URL, CANVAS_TOKEN)
courses = [c for c in canvas.get_courses(enrollment_state="active")
if is_academic_course(c)]
if args.course:
courses = [c for c in courses if c.id == args.course]
if not courses:
# Fetch directly
try:
courses = [canvas.get_course(args.course)]
except Exception as e:
print(f"[error] Course {args.course} not found: {e}")
sys.exit(1)
if args.list:
print(f"{'ID':<10} {'Size':>10} Course")
print("-" * 70)
for c in courses:
try:
files = get_course_files(c)
sz = sum(f["size"] for f in files)
flag = ">" if sz >= SIZE_LIMIT else " "
print(f"{c.id:<10} {flag}{sz/1024/1024:>8.1f} MB {c.name}")
except Exception as e:
print(f"{c.id:<10} {'?':>10} {c.name} ({e})")
return
total_dl = total_sk = 0
for course in courses:
dl, sk = process_course(course, force_all=args.force_all)
total_dl += dl
total_sk += sk
print(f"\n{'='*70}")
print(f"All done: {total_dl} downloaded, {total_sk} skipped across {len(courses)} course(s)")
if __name__ == "__main__":
main()