Giter VIP home page Giter VIP logo

sjtu-canvas-downloader's Issues

Need to cope with exception "Unauthorized" (just catch and pass it)

Description

Some teachers may upload their files to canvas in advance and lock them for some time. In this case, it would raise exception "Unauthorized"

How to solve

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.

past code

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.")

current code

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.")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.