def organize_by_file( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): folders = {folder.id: folder.full_name for folder in course.get_folders()} for file in course.get_files(): folder = folders[file.folder_id] + "/" folder = folder.lstrip("course files/") yield (file, folder)
def organize_by_file( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): folders = {folder.id: folder.full_name for folder in course.get_folders()} for file in get_files_in_course(course): folder = folders[file.folder_id] + "/" if folder.startswith("course files/"): folder = folder[len("course files/"):] yield (file, folder)
def process_course(course: canvasapi.canvas.Course) -> [(str, str)]: name = course.name.replace("(", "(").replace(")", ")") print(f"{Fore.CYAN}Course {name} {course.course_code}{Style.RESET_ALL}") folders = {folder.id: folder.full_name for folder in course.get_folders()} for file in course.get_files(): folder = folders[file.folder_id] + "/" if folder.startswith("course files/"): folder = folder[len("course files/"):] directory = f"{BASE_DIR}/{name}/{folder}" path = f"{directory}{file.display_name}" json_key = f"{name}/{folder}{file}" d, reason = do_download(file) if pathlib.Path(path).exists(): if json_key in checkpoint: if checkpoint[json_key]["updated_at"] == file.updated_at: d = False reason = "already downloaded" else: if json_key in checkpoint: del checkpoint[json_key] do_checkpoint() if file.url == "": d = False reason = "file not available" if d: pathlib.Path(directory).mkdir(parents=True, exist_ok=True) try: pathlib.Path(path).unlink() except: pass print( f" {Fore.GREEN}Download {file.display_name} ({file.size // 1024 / 1000}MB){Style.RESET_ALL}" ) download_file(file.url, " Downloading", path) checkpoint[json_key] = {"updated_at": file.updated_at} new_files_list.append(path) else: print( f" {Style.DIM}Ignore {file.display_name}: {reason}{Style.RESET_ALL}" ) do_checkpoint()
def process_course(course: canvasapi.canvas.Course) -> [(str, str)]: name = parse_course_folder_name(course) print(f"{Fore.CYAN}Course {course.course_code}{Style.RESET_ALL}") folders = {folder.id: folder.full_name for folder in course.get_folders()} reasons_of_not_download = {} for file in course.get_files(): folder = folders[file.folder_id] + "/" folder = folder.lstrip("course files/") directory = os.path.join(BASE_DIR, name, folder) path = os.path.join(directory, file.display_name) json_key = f"{name}/{folder}{file}" d, reason = do_download(file) update_flag = False if pathlib.Path(path).exists(): if json_key in checkpoint: if checkpoint[json_key]["updated_at"] == file.updated_at: d = False reason = "already downloaded" else: update_flag = True else: if json_key in checkpoint: del checkpoint[json_key] do_checkpoint() if file.url == "": d = False reason = "file not available" if d: pathlib.Path(directory).mkdir(parents=True, exist_ok=True) try: pathlib.Path(path).unlink() except: pass print( f" {Fore.GREEN}{'Update' if update_flag else 'New'}: {file.display_name} ({file.size // 1024 / 1000}MB){Style.RESET_ALL}") download_file(file.url, " Downloading", path) if OVERRIDE_FILE_TIME: c_time = datetime.strptime( file.created_at, '%Y-%m-%dT%H:%M:%S%z').timestamp() m_time = datetime.strptime( file.updated_at, '%Y-%m-%dT%H:%M:%S%z').timestamp() a_time = time.time() if is_windows(): setctime(path, c_time) os.utime(path, (a_time, m_time)) checkpoint[json_key] = {"updated_at": file.updated_at} new_files_list.append(path) else: if VERBOSE_MODE: print( f" {Style.DIM}Ignore {file.display_name}: {reason}{Style.RESET_ALL}") else: prev_cnt = reasons_of_not_download.get(reason, 0) reasons_of_not_download[reason] = prev_cnt + 1 do_checkpoint() for (reason, cnt) in reasons_of_not_download.items(): print(f" {Style.DIM}{cnt} files ignored: {reason}{Style.RESET_ALL}")