Exemple #1
0
def main():
    ''' Main function '''
    init()
    message.animate_characters(Fore.LIGHTYELLOW_EX, draw.ROCKET, 0.02)
    message.spinning_cursor()
    message.print_line('\r1. Paste course url or\n' +
                       '2. Press enter for Bulk Download')

    url = six.moves.input()

    print('')
    start_time = time.time()  #start time counter begins
    if url == "":
        # If user press Enter (i.e. url empty), get urls from Bulkdownload.txt
        urls = read.bulk_download()
        if not urls:
            sys.exit(
                message.colored_message(
                    Fore.LIGHTRED_EX,
                    'Please paste urls in Bulk Download.txt\n'))
        for url in urls:
            schedule_download(url)
    else:
        # begin regular download
        schedule_download(url)
    try:
        end_time = time.time()
        message.animate_characters(Fore.LIGHTGREEN_EX, draw.COW, 0.02)
        message.colored_message(
            Fore.LIGHTGREEN_EX, "\nThe whole process took {}\n".format(
                move.hms_string(end_time - start_time)))
    except KeyboardInterrupt:
        sys.exit(
            message.colored_message(Fore.LIGHTRED_EX,
                                    "\n- Program Interrupted!!\n"))
Exemple #2
0
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(f"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)

    message.spinning_cursor()    
    while len(exercises) > 0:                           # until exercises[] gets empty
        sys.stdout.write('\033[F\033[F')
        sys.stdout.flush()                              # Force Python to write data into terminal.
        
        for folder in os.listdir(downloads_folder):
            sys.stdout.write("\033[K")                  # Clear to the end of line
            sys.stdout.write(f"\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(f'\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