From 7ee5e710754e9b283925f551b00ba8fc8df2d4fc Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Wed, 1 Mar 2023 16:06:20 +0000 Subject: [PATCH] Move download time elapsed code to utilities --- devine/commands/dl.py | 8 +++----- devine/core/utilities.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/devine/commands/dl.py b/devine/commands/dl.py index 6f70eec..0dd69f0 100644 --- a/devine/commands/dl.py +++ b/devine/commands/dl.py @@ -48,7 +48,7 @@ from devine.core.services import Services from devine.core.titles import Movie, Song, Title_T from devine.core.titles.episode import Episode from devine.core.tracks import Audio, Subtitle, Video -from devine.core.utilities import get_binary_path, is_close_match +from devine.core.utilities import get_binary_path, is_close_match, time_elapsed_since from devine.core.utils.click_types import LANGUAGE_RANGE, QUALITY, SEASON_RANGE, ContextData from devine.core.utils.collections import merge_dict from devine.core.utils.subprocess import ffprobe @@ -578,12 +578,10 @@ class dl: cookie_jar.set_cookie(cookie) cookie_jar.save(ignore_discard=True) - minutes, seconds = divmod(int(time.time() - start_time), 60) - hours, minutes = divmod(minutes, 60) - time_string = (f"{hours:d}h" if hours else "") + f"{minutes:d}m{seconds:d}s" + dl_time = time_elapsed_since(start_time) console.print(Padding( - f"Processed all titles in [progress.elapsed]{time_string}", + f"Processed all titles in [progress.elapsed]{dl_time}", (0, 5, 1, 5) )) diff --git a/devine/core/utilities.py b/devine/core/utilities.py index 39486b3..9c5a236 100644 --- a/devine/core/utilities.py +++ b/devine/core/utilities.py @@ -4,6 +4,8 @@ import importlib.util import re import shutil import sys +import time + import unicodedata from collections import defaultdict from datetime import datetime @@ -189,6 +191,23 @@ def get_ip_info(session: Optional[requests.Session] = None) -> dict: return (session or requests.Session()).get("https://ipinfo.io/json").json() +def time_elapsed_since(start: float) -> str: + """ + Get time elapsed since a timestamp as a string. + E.g., `1h56m2s`, `15m12s`, `0m55s`, e.t.c. + """ + elapsed = int(time.time() - start) + + minutes, seconds = divmod(elapsed, 60) + hours, minutes = divmod(minutes, 60) + + time_string = f"{minutes:d}m{seconds:d}s" + if hours: + time_string = f"{hours:d}h{time_string}" + + return time_string + + @contextlib.asynccontextmanager async def start_pproxy(proxy: str) -> AsyncIterator[str]: proxy = urlparse(proxy)