def use_aria2(url, course_folder, cookie_path, driver): ''' user aria2 to download exercise files ''' # jump to course_folder os.chdir(course_folder) # To be filled with all exercise file links /ajax/.... exercise_file_urls = [] files = driver.find_elements_by_css_selector('.course-file') for file in files: url = file.get_attribute('href') exercise_file_urls.append(url) driver.find_element_by_css_selector('#exercise-tab').click() exercises = driver.find_elements_by_css_selector('a > .exercise-name') for exercise in exercises: exercise_message = message.return_colored_message(Fore.LIGHTYELLOW_EX, exercise.text) print(f"To be Downloaded: {exercise_message}") total_ex_files = len(exercise_file_urls) counter = 1 for url in exercise_file_urls: print(message.return_colored_message(Fore.LIGHTYELLOW_EX, f"\nDownloading {counter} of {total_ex_files}")) counter += 1 os.system("aria2c --load-cookie='{}' {}".format(cookie_path, url))
def find_cookie(desktop_folder, download_folder): ''' Find the latest cookie file ''' down_files = glob.glob(download_folder + '/*.txt') desk_files = glob.glob(desktop_folder + '/*.txt') files = down_files + desk_files cookies = [s for s in files if 'cookies' in s] if read.course_download_pref == 'cookies' or read.exfile_download_method == 'aria2': forgot_cookie = "Oops!! Did you forget to put 🍪 cookies.txt inside Downloads or Desktop folder ??\n" if not cookies: cookie_not_found = message.return_colored_message( Fore.LIGHTRED_EX, forgot_cookie) message.carriage_return_animate(cookie_not_found) sys.exit( message.colored_message( Fore.LIGHTRED_EX, '\nNote:\n\nIf you wish to download course using username & password combination,\ \nyou should set -> "course_download_pref": regular-login in settings.json\n' )) else: latest_cookie = max(cookies, key=os.path.getctime) latest_cookie_file = message.return_colored_message( Fore.LIGHTGREEN_EX, '🍪 Using latest cookie file: ' + latest_cookie + '\n') message.carriage_return_animate(latest_cookie_file) return latest_cookie
def use_selenium(url, course_folder, driver): ''' use just selenium to download files ''' # injecting jquery jquery = requests.get(url="https://code.jquery.com/jquery-3.3.1.min.js") driver.execute_script(jquery.text) # delete .exercise-tab max-height:320px so that all the ex_files can be seen driver.execute_script("$('.exercise-tab .content').css('max-height', 'none');") # Maximize Window if exercise-tab element not visible WebDriverWait(driver, 15).until( EC.presence_of_element_located((By.CSS_SELECTOR, "#exercise-tab"))) driver.find_element_by_css_selector('#exercise-tab').click() # Make sure page is more fully loaded before finding the element WebDriverWait(driver, 15).until( EC.presence_of_element_located((By.CSS_SELECTOR, "html.no-touch.member.loaded"))) exercises = driver.find_elements_by_css_selector('a > .exercise-name') try: for exercise in exercises: print('Downloading: ' + exercise.text) exercise.click() except Exception as e: sys.exit(e) time.sleep(4) # Give some heads up time to downloads downloads_folder = install.get_path("Downloads") os.chdir(downloads_folder) while len(exercises) > 0: # until exercises[] gets empty message.spinning_cursor() for folder in os.listdir(downloads_folder): try: folder = folder.decode('utf-8') # python 2.x except AttributeError: pass # python 3.x sys.stdout.write("\033[K") # Clear to the end of line sys.stdout.write("\rFinding Ex_file in Downloads folder ---> " + message.return_colored_message(Fore.LIGHTYELLOW_EX,folder)) sys.stdout.flush() # Force Python to write data into terminal. for exercise in exercises: if folder == exercise.text: if os.path.getsize(folder) > 0: # if file downloaded completely. try: shutil.move(exercise.text, course_folder) print('\nMoved to course folder: ' + exercise.text) except: print('\nMoving error: File already exists.') exercises.remove(exercise) # pop out moved exercise file from exercises list break # break inner for-loop when ex-file downloaded if(len(exercises) == 0): # if all exercises downloaded successfully break # break outer for-loop and stop scanning Downloads folder time.sleep(0.02) # delay to print which file is being scanned driver.close() # close web browser
def download_course(url): ''' download course ''' # Check for a valid url if url.find('.html') == -1: sys.exit( message.animate_characters(Fore.LIGHTRED_EX, draw.ANONYMOUS, 0.02)) url = url[:url.find(".html") + 5] #strip any extra text after .html in the url # Folder/File paths lynda_folder_path = read.location + '/' course_folder_path = save.course_path(url, lynda_folder_path) desktop_folder_path = install.get_path("Desktop") download_folder_path = install.get_path("Downloads") # Read preferences use_cookie_for_download = read.course_download_pref if use_cookie_for_download in ['cookies', 'cookie' ] or read.exfile_download_method == 'aria2': cookie_path = cookies.find_cookie(desktop_folder_path, download_folder_path) downloading_from_cookie = message.return_colored_message( Fore.LIGHTBLUE_EX, '🍪 Downloading videos using cookies.txt') message.carriage_return_animate(downloading_from_cookie) else: cookie_path = '' usr_pass_message = message.return_colored_message( Fore.LIGHTGREEN_EX, '⛺ Using username and password combination for download\n') message.carriage_return_animate(usr_pass_message) try: # main operations -> save.course(url, lynda_folder_path) # Create course folder save.info_file(url, course_folder_path) # Gather information save.chapters(url, course_folder_path) # Create chapter folders save.contentmd(url) # Create content.md save.videos(url, cookie_path, course_folder_path) # Download videos rename.videos(course_folder_path) # rename videos rename.subtitles(course_folder_path) # rename subtitles move.vid_srt_to_chapter( url, course_folder_path) # Move videos and subtitles to chapter folders # Download exercise files if save.check_exercise_file(url): print('\nExercise file is available to download') if not read.download_exercise_file: # if user do not want to download ex-file print( "settings.json says you do not want to download ex-file -> 'download_exercise_file': false" ) else: # if user wants to download ex-file if read.course_download_pref == 'regular-login': exercise_file.download(url, course_folder_path, cookie_path) elif read.exfile_download_pref == 'library-login': if read.card_number == '': print( '\nTo download ex-file via library login -> Please save library card details in settings.json' ) else: exercise_file.download(url, course_folder_path, cookie_path) else: print( '\nThe exercise file can only be downloaded through one of the below combinations:' ) print('~ Regular login: username + password or') print('~ Library login: card number, pin and org. url\n') else: # if exercise file not present print('This course does not include Exercise files.') except KeyboardInterrupt: sys.exit( message.colored_message(Fore.LIGHTRED_EX, "\n- Program Interrupted!!\n"))