-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
309 lines (254 loc) · 11.9 KB
/
main.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
309
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from dotenv import load_dotenv
import json
from api import CanvasAPIClient
from crawler import CanvasCrawler
from endpoints import COURSE_FRONTPAGE_ENDPOINT, COURSES_ENDPOINT
from functions import (
download_file,
get_page_content,
save_assignment_description,
save_grade_and_comments,
save_page_content,
)
from logger import main_logger, api_logger, crawl_logger
# Load environment variables from .env file
load_dotenv()
# Retrieve environment variables
CANVAS_ACCESS_TOKEN = os.getenv("CANVAS_ACCESS_TOKEN")
CANVAS_DOMAIN = os.getenv("CANVAS_DOMAIN")
CANVAS_API_URL = f"https://{CANVAS_DOMAIN}/api/v1"
CRAWLER_WORKERS = os.getenv("CRAWLER_WORKERS")
download = False
# TODO: Obviously this also needs to be refactored and functions need to be merged
# TODO: Preferably add test files
# Main function to download assignment details, submissions, and save results
def download_assignments_and_submissions(
client: CanvasAPIClient, course_id: str, course_name: str, workers: int = 1
):
# Get all assignments for the course
assignments = client.get_assignments(course_id)
if not assignments:
main_logger.warning(f"No assignments found for course: {course_name}")
return
file_downloads = [] # List to hold all file download tasks
for assignment in assignments:
assignment_name = assignment.get("name", "Unnamed_Assignment").replace("/", "_")
assignment_id = assignment["id"]
main_logger.debug(f" Assignment: {assignment_name} (ID: {assignment_id})")
# Create directory structure for the assignment
course_dir = os.path.join(
"courses", course_name, "cv_assignments", assignment_name
)
os.makedirs(course_dir, exist_ok=True)
# Save assignment description if available
description = assignment.get("description", None)
description_file_path = os.path.join(course_dir, "assignment_description.txt")
save_assignment_description(description_file_path, description)
# Download any attached files in the assignment description
if "attachments" in assignment and assignment["attachments"]:
for attachment in assignment["attachments"]:
file_name = attachment["display_name"].replace("/", "_")
file_url = attachment["url"]
save_path = os.path.join(course_dir, file_name)
file_downloads.append((file_url, save_path))
# Fetch submission details
submission = client.get_submission(course_id, assignment_id)
if submission:
# Download any files attached to the submission
if "attachments" in submission and submission["attachments"]:
for attachment in submission["attachments"]:
file_name = attachment["display_name"].replace("/", "_")
file_url = attachment["url"]
save_path = os.path.join(course_dir, f"submission_{file_name}")
file_downloads.append((file_url, save_path))
# Save grade and comments to a text file
result_file_path = os.path.join(course_dir, "assignment_result_score.txt")
save_grade_and_comments(result_file_path, submission)
else:
main_logger.warning(
f" No submission found for assignment: {assignment_name}"
)
# Parallel downloading of files
if file_downloads:
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = [
executor.submit(download_file, file_info, client.access_token)
for file_info in file_downloads
]
# Wait for all futures to complete
for future in as_completed(futures):
future.result() # This will raise an exception if any download failed
else:
main_logger.warning(f" No files to download for course: {course_name}")
def download_all_files(
client: CanvasAPIClient, course_id: str, course_name: str, workers: int = 1
):
# Get all files for the course
files = client.get_files(course_id)
if files:
main_logger.info(f"Found {len(files)} files for course: {course_name}")
# Create a directory only if there are files
course_dir = os.path.join("courses", course_name)
os.makedirs(course_dir, exist_ok=True)
# Prepare file info for parallel downloading
file_downloads = [
(
file["url"],
os.path.join(course_dir, file["display_name"].replace("/", "_")),
)
for file in files
]
# Use ThreadPoolExecutor to download files in parallel
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = [
executor.submit(download_file, file_info, client.access_token)
for file_info in file_downloads
]
# Wait for all futures to complete
for future in as_completed(futures):
future.result() # This will raise an exception if any download failed
else:
main_logger.warning(f"No files found for course: {course_name}")
# Main function to download files from modules in all courses
def download_files_from_modules(
client: CanvasAPIClient, course_id: str, course_name: str, workers: int = 1
):
# Get all modules for the course
modules = client.get_modules(course_id)
if not modules:
main_logger.warning(f"No modules found for course: {course_name}")
return
# List to hold all files that need to be downloaded
file_downloads = []
external_links = []
for module in modules:
module_name = module.get("name", "Unnamed_Module").replace("/", "_")
module_id = module["id"]
main_logger.debug(f" Module: {module_name} (ID: {module_id})")
# Get all items in the module
module_items = client.get_module_items(course_id, module_id)
if not module_items:
main_logger.warning(f" No items found in module: {module_name}")
continue
for item in module_items:
# Check the type of item
item_type = item["type"]
# TODO: Needs to be same implementation as crawler
if item_type == "File":
# Handle file attachments
file_name = item["title"].replace("/", "_")
file_id = item["content_id"]
file_response = client.get_course_files(course_id, file_id)
file_obj = file_response.json()
# Prepare the download directory for the course and module
course_dir = os.path.join(
"courses", course_name, "cv_modules", module_name
)
os.makedirs(course_dir, exist_ok=True)
url = file_obj["url"]
if url == "":
main_logger.error(f"Can't download {file_obj['display_name']}")
main_logger.error(f" File ID: {file_id}")
main_logger.error(f" File URL: {url}")
main_logger.error(f" f{json.dumps(file_obj)}")
# Also save the file info to a separate file
no_download_links_path = os.path.join(
"courses", course_name, "cv_modules", "cant_download.txt"
)
with open(no_download_links_path, "w") as f:
f.write(json.dumps(file_obj) + "\n")
else:
save_path = os.path.join(course_dir, file_name)
file_downloads.append((url, save_path))
elif item_type == "ExternalUrl":
# Save external links
external_link = item["external_url"]
external_links.append(external_link)
# TODO: Needs to be rechecked
elif item_type == "Page":
# Handle Canvas pages
page_url = item["url"] # Use the page URL provided in the item
page_title = item["title"].replace("/", "_")
page_content = get_page_content(page_url, client.access_token)
# Prepare the page save path
course_dir = os.path.join(
"courses", course_name, "cv_modules", module_name
)
os.makedirs(course_dir, exist_ok=True)
save_path = os.path.join(course_dir, f"{page_title}.txt")
save_page_content(page_content, save_path)
# Download files in parallel
if file_downloads:
main_logger.debug(f"Downloading {len(file_downloads)} files")
with ThreadPoolExecutor(
max_workers=min(workers, len(file_downloads))
) as executor:
futures = [
executor.submit(download_file, file_info, client.access_token)
for file_info in file_downloads
]
# Wait for all futures to complete
for future in as_completed(futures):
future.result() # This will raise an exception if any download failed
else:
main_logger.warning(
f" No files to download in modules for course: {course_name}"
)
# Save external links to a file
if external_links:
external_links_path = os.path.join("courses", course_name, "external_links.txt")
with open(external_links_path, "w") as f:
for link in external_links:
f.write(link + "\n")
main_logger.debug(f"Saved external links to: {external_links_path}")
# Main function to download all files for each course
def download_content_from_course(
client: CanvasAPIClient, crawler: CanvasCrawler, workers: int = 1
):
courses = client.get_courses()
if not courses:
main_logger.warning("No courses found.")
return
# for idx, course in enumerate(courses):
# course_name = course.get("name", "Unnamed_Course").replace("/", "_")
# course_id = course["id"]
# print(f"{course_name} : {course_id}")
for idx, course in enumerate(courses):
course_name = course.get("name", "Unnamed_Course").replace(
"/", "_"
) # Avoid directory issues with slashes
course_id = course["id"]
os.system("cls||clear")
print(f"Processing: {course_name} ({idx+1}/{len(courses)}) ", end="\r")
main_logger.info(f"Fetching \nCourse: {course_name} (ID: {course_id})")
# TODO: User-friendly command line interface with arguments
# Uncomment one of the following if you want to disable downloading of files
download_all_files(client, course_id, course_name, workers)
download_files_from_modules(client, course_id, course_name, workers)
download_assignments_and_submissions(client, course_id, course_name, workers)
# Starting points for crawling
homepage_url = (
f"{CANVAS_API_URL}{COURSE_FRONTPAGE_ENDPOINT.format(course_id=course_id)}"
)
syllabus_url = f"{CANVAS_API_URL}{COURSES_ENDPOINT.format(course_id=course_id)}?include[]=syllabus_body"
# Start crawling from the homepage
visited_links = set() # To avoid re-crawling the same pages
crawler.crawl_page(homepage_url, visited_links)
crawler.crawl_page(syllabus_url, visited_links)
# TODO: User-friendly command line interface with arguments
if __name__ == "__main__":
if not CANVAS_ACCESS_TOKEN or not CANVAS_DOMAIN:
print("NOTICE: Please set the environment variables for Canvas API access.")
exit(1)
if not CRAWLER_WORKERS:
print("NOTICE: Please set the number of workers for the crawler.")
exit(1)
clientAPI = CanvasAPIClient(
access_token=CANVAS_ACCESS_TOKEN, domain_url=CANVAS_DOMAIN, logger=api_logger
)
crawler = CanvasCrawler(clientAPI, logger=crawl_logger)
download_content_from_course(
client=clientAPI, crawler=crawler, workers=CRAWLER_WORKERS
)