From cc3516d30598f77d46f3a00ee94d6d5e47cfa362 Mon Sep 17 00:00:00 2001 From: Drake Panzer Date: Sat, 21 Jan 2023 15:37:48 -0700 Subject: [PATCH] c --- process/threads.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/process/threads.py b/process/threads.py index e8232c2..68e9a29 100644 --- a/process/threads.py +++ b/process/threads.py @@ -26,12 +26,13 @@ def download_video(args) -> dict: def progress_hook(d): # downloaded_bytes and total_bytes can be None if the download hasn't started yet. - if d['status'] == 'downloading' and d.get('downloaded_bytes') and d.get('total_bytes'): - downloaded_bytes = int(d['downloaded_bytes']) - total_bytes = int(d['total_bytes']) - if total_bytes > 0: - percent = (downloaded_bytes / total_bytes) * 100 - bar.update(int(np.round(percent - bar.n))) # If the progress bar doesn't end at 100% then round to 1 decimal place + if d['status'] == 'downloading': + if d.get('downloaded_bytes') and d.get('total_bytes'): + downloaded_bytes = int(d['downloaded_bytes']) + total_bytes = int(d['total_bytes']) + if total_bytes > 0: + percent = (downloaded_bytes / total_bytes) * 100 + bar.update(int(np.round(percent - bar.n))) # If the progress bar doesn't end at 100% then round to 1 decimal place bar.set_postfix({ 'speed': d['_speed_str'], 'size': f"{d['_downloaded_bytes_str'].strip()}/{d['_total_bytes_str'].strip()}", @@ -39,25 +40,23 @@ def download_video(args) -> dict: video = args[0] kwargs = args[1] - bars = kwargs['bars'] - ydl_opts = kwargs['ydl_opts'] # Get a bar locked = False - if len(bars): + if len(kwargs['bars']): # We're going to wait until a bar is available for us to use. while not locked: - for item in bars: + for item in kwargs['bars']: if not is_manager_lock_locked(item[1]): locked = item[1].acquire(timeout=0.1) # get the lock ASAP and don't wait if we didn't get it. offset = item[0] bar_lock = item[1] break - ydl_opts['progress_hooks'] = [progress_hook] + kwargs['ydl_opts']['progress_hooks'] = [progress_hook] desc_with = int(np.round(os.get_terminal_size()[0] * (1 / 4))) bar = tqdm(total=100, position=(offset if locked else None), desc=video['title'].ljust(desc_with)[:desc_with], bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}{postfix}]', leave=False, disable=not len(bars)) - yt_dlp = ydl.YDL(ydl_opts) + yt_dlp = ydl.YDL(kwargs['ydl_opts']) output_dict = {'downloaded_video_id': None, 'blacklist_video_id': None, 'video_error_logger_msg': [], 'status_msg': [], 'logger_msg': []} # empty object start_time = time.time()