def organize_by_module( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): for module in course.get_modules(): module_item_count = module.items_count module_item_position = module.position - 1 # it begins with 1 module_name = config.MODULE_FOLDER_TEMPLATE module_name = module_name.replace( "{NAME}", re.sub(file_regex, "_", module.name.replace("(", "(").replace(")", ")"))) if config.CONSOLIDATE_MODULE_SPACE: module_name = " ".join(module_name.split()) module_name = module_name.replace( "{IDX}", str(module_item_position + config.MODULE_FOLDER_IDX_BEGIN_WITH)) print( f" Module {Fore.CYAN}{module_name} ({module_item_count} items){Style.RESET_ALL}" ) for item in module.get_module_items(): if item.type == "File": yield get_file_in_course(course, item.content_id), module_name elif item.type in ["Page", "Discussion", "Assignment"]: page_url = item.html_url elif item.type == "ExternalUrl": page_url = item.external_url elif item.type == "SubHeader": pass else: if config.VERBOSE_MODE: print( f" {Fore.YELLOW}Unsupported item type: {item.type}{Style.RESET_ALL}" )
def extract_modules(c: canvasapi.canvas.Course) -> [canvasapi.canvas.File]: # Some courses put files on the home page, but the Course.show_front_page endpoint seems to be broken yield from file_extractor(c, extract_file_ids(getattr(c, 'syllabus_body', ""))) for mod in c.get_modules(): for mod_item in mod.get_module_items(): mod_type = getattr(mod_item, "type", None) if mod_type == 'File': yield c.get_file(mod_item.content_id) elif mod_type == 'Page': page_html = c.get_page(mod_item.page_url).body yield from file_extractor(c, extract_file_ids(page_html)) elif mod_type in ['ExternalUrl', 'ExternalTool']: # Not sure what to do with these. Ignoring for now pass elif mod_type in ['Assignment']: # These can be picked up by the AssignmentBuilder, so ignoring here pass elif mod_type in ['SubHeader']: # Useless pass else: logger.warning(f"Unknown module type: {mod_type}") logger.warning(mod_item)
def organize_by_module( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): for m_idx, module in enumerate(course.get_modules()): print(f" Module {Fore.CYAN}{module.name}{Style.RESET_ALL}") for item in module.get_module_items(): if item.type == "File": yield (course.get_file(item.content_id), '%d ' % m_idx + re.sub(file_regex, "_", module.name.replace("(", "(").replace(")", ")")))
def organize_by_module( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): for module in course.get_modules(): print(module.name) for item in module.get_module_items(): if item.type == "File": yield (course.get_file(item.content_id), re.sub(file_regex, "_", module.name.replace("(", "(").replace(")", ")")))
def organize_by_module( course: canvasapi.canvas.Course) -> (canvasapi.canvas.File, str): for m_idx, module in enumerate(course.get_modules()): print(f" Module {Fore.CYAN}{module.name}{Style.RESET_ALL}") for item in module.get_module_items(): if item.type == "File": module_name = MODULE_FOLDER_TEMPLATE module_name = module_name.replace( "{NAME}", re.sub(file_regex, "_", module.name.replace("(", "(").replace(")", ")"))) if CONSOLIDATE_MODULE_SPACE: module_name = " ".join(module_name.split()) module_name = module_name.replace( "{IDX}", str(m_idx + MODULE_FOLDER_IDX_BEGIN_WITH)) yield (course.get_file(item.content_id), module_name)