leeteng2001 / sjtu-canvas-downloader Goto Github PK
View Code? Open in Web Editor NEWA canvas downloader for SJTU students with a reasonable GUI, efficient and hassle free workflow
A canvas downloader for SJTU students with a reasonable GUI, efficient and hassle free workflow
Some teachers may upload their files to canvas in advance and lock them for some time. In this case, it would raise exception "Unauthorized"
I figured out how to solve it (just catch the exception and pass it) , but do not have Qt in my computer. So I choose to report the issue.
def download_canvas(secret_token, course_num, save_to_path, print_output, sync_on: bool, canvas_struct: bool):
try:
canvas = Canvas("https://oc.sjtu.edu.cn/", secret_token) # Init
course = canvas.get_course(course_num)
except exceptions.InvalidAccessToken:
print_output("FAILED. Check your secret token!")
return
except exceptions.ResourceDoesNotExist:
print_output("FAILED. Check your course number!")
return
except:
print_output("FAILED. Unknown error occur while trying to connect with canvas!")
return
# variables to get all the files in the current save path first for a LOOKUP table
current_files = set() # print this to debug
new_files_idx = 0
for dir_path, dir_names, filenames in os.walk(save_to_path):
current_files.update(filenames)
for folder in course.get_folders(): # Get all folders in a course, it contains all sub-folders as well !
# Extract the top folder path
folder_top = "" if str(folder) == 'course files' else str(folder).split('/', 1)[1]
print_output(f"Currently at: {folder_top if folder_top else 'Canvas Home'}")
folder_top = "" if not canvas_struct else folder_top # Check download directory structure
# Download files
for file in folder.get_files():
if not sync_on or str(file) not in current_files: # Download checking condition
try:
print_output(f"Downloading file {new_files_idx + 1} inside {folder_top if folder_top else 'Canvas Home'}... -> {file}")
# Create folder to replicate canvas structure
os.makedirs(os.path.join(save_to_path, folder_top), exist_ok=True)
file.download(os.path.join(save_to_path, folder_top, str(file)))
new_files_idx += 1 # only increment after download because it might have error during download
except exceptions.ResourceDoesNotExist:
print_output(f"Download error: You might not have permission to access this file.")
# Status report
print_output(f"Finished, there's {new_files_idx} newly added files.")
def download_canvas(secret_token, course_num, save_to_path, print_output, sync_on: bool, canvas_struct: bool):
try:
canvas = Canvas("https://oc.sjtu.edu.cn/", secret_token) # Init
course = canvas.get_course(course_num)
except exceptions.InvalidAccessToken:
print_output("FAILED. Check your secret token!")
return
except exceptions.ResourceDoesNotExist:
print_output("FAILED. Check your course number!")
return
except:
print_output("FAILED. Unknown error occur while trying to connect with canvas!")
return
# variables to get all the files in the current save path first for a LOOKUP table
current_files = set() # print this to debug
new_files_idx = 0
for dir_path, dir_names, filenames in os.walk(save_to_path):
current_files.update(filenames)
for folder in course.get_folders(): # Get all folders in a course, it contains all sub-folders as well !
# Extract the top folder path
folder_top = "" if str(folder) == 'course files' else str(folder).split('/', 1)[1]
print_output(f"Currently at: {folder_top if folder_top else 'Canvas Home'}")
folder_top = "" if not canvas_struct else folder_top # Check download directory structure
# Download files
try:
for file in folder.get_files():
if not sync_on or str(file) not in current_files: # Download checking condition
try:
print_output(f"Downloading file {new_files_idx + 1} inside {folder_top if folder_top else 'Canvas Home'}... -> {file}")
# Create folder to replicate canvas structure
os.makedirs(os.path.join(save_to_path, folder_top), exist_ok=True)
file.download(os.path.join(save_to_path, folder_top, str(file)))
new_files_idx += 1 # only increment after download because it might have error during download
except exceptions.ResourceDoesNotExist:
print_output(f"Download error: You might not have permission to access this file.")
except exceptions.Unauthorized:
pass
# Status report
print_output(f"Finished, there's {new_files_idx} newly added files.")
rt
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.