def course_download( self, path="", quality="", language="en", dl_assets=True, dl_lecture=True, dl_subtitles=True, chapter_number=None, chapter_start=None, chapter_end=None, lecture_number=None, lecture_start=None, lecture_end=None, keep_vtt=False, skip_hls_stream=False, ): """This function will download the course contents ..""" if not self.cookies: logger.info(msg="Trying to login as", status=self.username) if self.cookies: logger.info(msg="Trying to login using session cookie", new_line=True) for url in self.url_or_courses: course = udemy.course( url=url, username=self.username, password=self.password, cookies=self.cookies, skip_hls_stream=skip_hls_stream, cache_session=self._cache_session, ) course_name = course.title if path: if "~" in path: path = os.path.expanduser(path) course_path = os.path.join(path, course_name) chapters = course.get_chapters( chapter_number=chapter_number, chapter_start=chapter_start, chapter_end=chapter_end, ) total_lectures = course.lectures total_chapters = course.chapters logger.success(msg=course_name, course=True) logger.info(msg=f"Chapter(s) ({total_chapters})", new_line=True) logger.info(msg=f"Lecture(s) ({total_lectures})", new_line=True) for chapter in chapters: chapter_index = chapter.index chapter_title = chapter.title lectures = chapter.get_lectures( lecture_number=lecture_number, lecture_start=lecture_start, lecture_end=lecture_end, ) lectures_count = chapter.lectures filepath = to_filepath(course_path, chapter_title) logger.set_log_filepath(course_path) chapter_progress = (chapter_index if chapter_number else f"{chapter_index} of {total_chapters}") logger.info( msg=f"Downloading chapter : ({chapter_progress})", new_line=True, before=True, cc=80, cc_msg=80, ) logger.info(msg=f"Chapter ({chapter_title})", new_line=True, cc=15, cc_msg=60) logger.info( msg=f"Found ({lectures_count}) lecture(s).", new_line=True, ) lecture_index = 0 if lecture_number: lecture_index = lecture_number - 1 if lecture_start: lecture_index = lecture_start - 1 if lecture_index < 0: lecture_index = 0 for lecture in lectures: lecture_assets = lecture.assets lecture_subtitles = lecture.subtitles lecture_best = lecture.getbest() if dl_lecture: lecture_index = lecture_index + 1 if lecture.html: retval = lecture.dump(filepath=filepath) msg = retval.get("msg") if msg not in ["download", "already downloaded"]: msg = f"Lecture: '{lecture.title}.{lecture.extension}' failed to dump, reason: {msg}" logger.warning(msg=msg, silent=True) self.download_lecture( lecture_best, filepath, lecture_index, lectures_count, quality, ) if dl_assets: self.download_assets(lecture_assets, filepath) if dl_subtitles: self.download_subtitles( lecture_subtitles, filepath, language=language, keep_vtt=keep_vtt, ) print("")
def course_listdown( self, chapter_number=None, chapter_start=None, chapter_end=None, lecture_number=None, lecture_start=None, lecture_end=None, skip_hls_stream=False, ): """This function will listdown the course contents ..""" if not self.cookies: logger.info(msg="Trying to login as", status=self.username) if self.cookies: logger.info(msg="Trying to login using session cookie", new_line=True) for url in self.url_or_courses: course = udemy.course( url=url, username=self.username, password=self.password, cookies=self.cookies, skip_hls_stream=skip_hls_stream, cache_session=self._cache_session, ) course_name = course.title chapters = course.get_chapters( chapter_number=chapter_number, chapter_start=chapter_start, chapter_end=chapter_end, ) total_lectures = course.lectures total_chapters = course.chapters logger.success(msg=course_name, course=True) logger.info(msg=f"Chapter(s) ({total_chapters})", new_line=True) logger.info(msg=f"Lecture(s) ({total_lectures})", new_line=True) for chapter in chapters: chapter_id = chapter.id chapter_title = chapter.title lectures = chapter.get_lectures( lecture_number=lecture_number, lecture_start=lecture_start, lecture_end=lecture_end, ) lectures_count = chapter.lectures logger.info( msg=f"Chapter ({chapter_title}-{chapter_id})", new_line=True, before=True, cc=15, cc_msg=15, ) logger.info(msg=f"Lecture(s) ({lectures_count})", new_line=True) for lecture in lectures: lecture_id = lecture.id lecture_streams = lecture.streams lecture_best = lecture.getbest() lecture_assets = lecture.assets lecture_subtitles = lecture.subtitles if not lecture_streams: continue logger.info( indent=" - ", msg="duration : ", new_line=True, cc=80, cc_msg=10, post_msg=f"{lecture.duration}.", cc_pmsg=80, ) logger.info( indent=" - ", msg="Lecture id : ", new_line=True, cc=80, cc_msg=10, post_msg=f"{lecture_id}.", cc_pmsg=80, ) indent = "\t- " for stream in lecture_streams: post_msg = None if stream.is_hls: human_readable = "" if not stream.is_hls: content_length = stream.get_filesize() if content_length == 0: continue human_readable = to_human_readable(content_length) if lecture_best.quality == stream.quality: post_msg = "(Best)" msg = "{:<22} {:<8}{}".format(f"{stream}", f"{stream.quality}p", human_readable) logger.info( indent=indent, msg=msg, new_line=True, cc=15, post_msg=post_msg, cc_pmsg=30, ) if lecture_assets: for asset in lecture_assets: if asset.mediatype == "external_link": continue content_length = asset.get_filesize() if content_length == 0: continue human_readable = to_human_readable(content_length) msg = "{:<22} {:<8}{}".format( f"{asset}", asset.extension, human_readable) logger.info( indent=indent, msg=msg, new_line=True, cc=15, ) if lecture_subtitles: for sub in lecture_subtitles: content_length = sub.get_filesize() if content_length == 0: continue human_readable = to_human_readable(content_length) msg = "{:<22} {:<8}{}".format( f"{sub}", sub.extension, human_readable) logger.info( indent=indent, msg=msg, new_line=True, cc=15, ) print("")