def download_file_parallel(url, target_path, show_progress=False, num_threads=1): """ Download the file from the given `url` and store it at `target_path`. Return a tuple x (url, bool, str). x[0] contains the url. If download failed x[1] is ``False`` and x[2] contains some error message. If download was fine x[1] is ``True`` and x[2] contains the target-path. """ downloader = Downloader(url, target_path, num_threads) downloader.start() if show_progress: # # Wait until we know file size # while downloader.total_length == 0: pass pbar = tqdm(total=downloader.total_length, desc='Download File', unit_scale=True) def update_pbar(x): pbar.update(x.total_downloaded - pbar.n) downloader.subscribe(update_pbar, 10) downloader.wait_for_finish() if show_progress: pbar.close() return (url, True, target_path)
def download_file_parallel(url, target_path, num_threads=1): """ Download the file from the given `url` and store it at `target_path`. Return a tuple x (url, bool, str). x[0] contains the url. If download failed x[1] is ``False`` and x[2] contains some error message. If download was fine x[1] is ``True`` and x[2] contains the target-path. """ downloader = Downloader(url, target_path, num_threads) downloader.start() # Wait until we know file size while downloader.total_length == 0: pass file_size = downloader.total_length logger.info('Download file from "%s" with size: %d B', url, file_size) bytes_at_last_log = 0 def callback(x): nonlocal bytes_at_last_log if x.total_downloaded - bytes_at_last_log >= PROGRESS_LOGGER_BYTE_DELAY: logger.info('Download [%06.2f%%]', x.total_downloaded / file_size * 100) bytes_at_last_log = x.total_downloaded downloader.subscribe(callback, 10) downloader.wait_for_finish() logger.info('Finished download') return (url, True, target_path)
def download_file(url, target_path): downloader = Downloader(url, target_path, 8) downloader.start() while downloader.total_length == 0: pass pbar = tqdm(total=downloader.total_length, desc='Download File', unit_scale=True) def update_pbar(x): pbar.update(x.total_downloaded - pbar.n) downloader.subscribe(update_pbar, 10) downloader.wait_for_finish() pbar.close()
def start_update(): '''Withdraw main window and start update download''' nonlocal button_frame nonlocal cancel_button nonlocal update_button nonlocal debug nonlocal sv_version master.withdraw() try: os.remove('update.zip') except OSError: pass install = True # For development purposes if not bundle and debug: if not msgbox.askyesno('', 'Install update?'): install = False cancel_button.destroy() update_button.destroy() def cancel(): if msgbox.askokcancel(_('Cancel'), _('Are you sure to cancel?')): os._exit(0) # There's no cancel button so we use close button as one instead updatewindow.protocol("WM_DELETE_WINDOW", cancel) # Define progress variables dl_p = tk.IntVar() dl_p.set(0) dl_pbar = ttk.Progressbar(button_frame, length=150, orient=tk.HORIZONTAL, variable=dl_p) dl_pbar.pack(side='left', padx=5) dl_prog_var = tk.StringVar() dl_prog_var.set('-------- / --------') dl_prog = tk.Label(button_frame, textvariable=dl_prog_var) dl_speed_var = tk.StringVar() dl_speed_var.set('---------') dl_speed = tk.Label(button_frame, textvariable=dl_speed_var) dl_prog.pack(side='right', padx=5) dl_speed.pack(side='right', padx=5) master.update() dl_url = f'https://github.com/sw2719/steam-account-switcher/releases/download/v{sv_version}/Steam_Account_Switcher_v{sv_version}.zip' try: r = req.get(dl_url, stream=True) r.raise_for_status() total_size = int(r.headers.get('content-length')) total_in_MB = round(total_size / 1048576, 1) except req.RequestException: msgbox.showerror(_('Error'), _('Error occured while downloading update.')) os._exit(1) if round(total_in_MB, 1).is_integer(): total_in_MB = int(total_in_MB) def launch_updater(): if not install: return while not os.path.isfile('update.zip'): sleep(1) try: archive = os.path.join(os.getcwd(), 'update.zip') f = zf.ZipFile(archive, mode='r') f.extractall(members=(member for member in f.namelist() if 'updater' in member)) os.execv('updater/updater.exe', sys.argv) except (FileNotFoundError, zf.BadZipfile, OSError): error_msg(_('Error'), _("Couldn't perform automatic update.") + '\n' + _('Update manually by extracting update.zip file.')) def dl_callback(downloader): nonlocal total_in_MB current_size = downloader.total_downloaded current_in_MB = round(current_size / 1048576, 1) if round(current_in_MB, 1).is_integer(): current_in_MB = int(current_in_MB) perc = int(current_size / total_size * 100) prog = f'{current_in_MB}MB / {total_in_MB}MB' dl_p.set(perc) dl_prog_var.set(prog) dl_speed_var.set(downloader.readable_speed + '/s') master.update() if perc == 100 and downloader.total_merged == downloader.total_length: launch_updater() downloader = Downloader(dl_url, 'update.zip', 8) downloader.subscribe(dl_callback) downloader.start()