Beispiel #1
0
def watch(filename, download_thread):
    sleep_time = 2
    real_sleep_time = sleep_time - 0.1
    start_time = time.time()
    last_downloaded_number = 0
    last_output_len = 0

    # Ensure that the tasks information is not empty.
    tasks_info = downloader_handle.get_tasks_info(filename)
    while download_thread.is_alive() and not tasks_info:
        tasks_info = downloader_handle.get_tasks_info(filename)
    # Extract the total size of the file
    total_size = 0
    while download_thread.is_alive():
        tasks_info = downloader_handle.get_tasks_info(filename)
        # If doesn't determine the download manner, continue.
        if tasks_info[filename][0] is None:
            continue
        if tasks_info[filename][0]:
            # Ensure no KeyError
            while True:
                try:
                    total_size = tasks_info[filename][3]
                except KeyError:
                    pass
                else:
                    if total_size:
                        break
                    tasks_info = downloader_handle.get_tasks_info(filename)
        break

    while download_thread.is_alive(): 
        end_time = time.time()
        total_time = int(end_time - start_time)
        if not total_time:
            continue
        tasks_info = downloader_handle.get_tasks_info(filename)
        
        if total_size:
            downloaded_number = sum(map(lambda v: v[4], tasks_info[filename][4].values()))
            is_finish = False
            for v in tasks_info[filename][4].values():
                if v[3] == v[4]:
                    is_finish = True
                    break
        else:
            downloaded_number = tasks_info[filename][3]
            is_finish = False
            
        number = downloaded_number - last_downloaded_number
        last_downloaded_number = downloaded_number

        print('\r', ' ' * last_output_len, end='')
        rtn = _print_watch_result(filename, downloaded_number, number, sleep_time, total_time, total_size, is_finish)
        last_output_len = len(rtn) + 25
        print(rtn, end='')
        time.sleep(real_sleep_time)
Beispiel #2
0
def watch_files(filename, filenames, download_thread, total_size):
    sleep_time = 2
    real_sleep_time = sleep_time - 0.1
    start_time = time.time()
    last_downloaded_number = 0
    last_output_len = 0

    # Ensure that the tasks information is not empty.
    tasks_info = downloader_handle.get_tasks_info(filenames)
    while download_thread.is_alive() and not tasks_info:
        tasks_info = downloader_handle.get_tasks_info(filenames)
    
    while download_thread.is_alive():
        total_time = int(time.time() - start_time)
        if not total_time:
            continue
        tasks_info = downloader_handle.get_tasks_info(filenames)

        downloaded_number = 0
        for f in filenames:
            if tasks_info[f][0]:
                downloaded_number += sum(map(lambda v: v[4], tasks_info[f][4].values()))
                is_finish = False
                for v in tasks_info[filename][4].values():
                    if v[3] == v[4]:
                        is_finish = True
                        break
            else:
                downloaded_number += tasks_info[f][3]
                is_finish = False

        number = downloaded_number - last_downloaded_number
        last_downloaded_number = downloaded_number

        print('\r', ' ' * last_output_len, end='')
        rtn = _print_watch_result(filename, downloaded_number, number, sleep_time, total_time, total_size, is_finish)
        last_output_len = len(rtn) + 25
        print(rtn, end='')
        time.sleep(real_sleep_time)