Move download time elapsed code to utilities

This commit is contained in:
rlaphoenix 2023-03-01 16:06:20 +00:00
parent 7b7be47f7d
commit 7ee5e71075
2 changed files with 22 additions and 5 deletions

View File

@ -48,7 +48,7 @@ from devine.core.services import Services
from devine.core.titles import Movie, Song, Title_T from devine.core.titles import Movie, Song, Title_T
from devine.core.titles.episode import Episode from devine.core.titles.episode import Episode
from devine.core.tracks import Audio, Subtitle, Video 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.click_types import LANGUAGE_RANGE, QUALITY, SEASON_RANGE, ContextData
from devine.core.utils.collections import merge_dict from devine.core.utils.collections import merge_dict
from devine.core.utils.subprocess import ffprobe from devine.core.utils.subprocess import ffprobe
@ -578,12 +578,10 @@ class dl:
cookie_jar.set_cookie(cookie) cookie_jar.set_cookie(cookie)
cookie_jar.save(ignore_discard=True) cookie_jar.save(ignore_discard=True)
minutes, seconds = divmod(int(time.time() - start_time), 60) dl_time = time_elapsed_since(start_time)
hours, minutes = divmod(minutes, 60)
time_string = (f"{hours:d}h" if hours else "") + f"{minutes:d}m{seconds:d}s"
console.print(Padding( 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) (0, 5, 1, 5)
)) ))

View File

@ -4,6 +4,8 @@ import importlib.util
import re import re
import shutil import shutil
import sys import sys
import time
import unicodedata import unicodedata
from collections import defaultdict from collections import defaultdict
from datetime import datetime 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() 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 @contextlib.asynccontextmanager
async def start_pproxy(proxy: str) -> AsyncIterator[str]: async def start_pproxy(proxy: str) -> AsyncIterator[str]:
proxy = urlparse(proxy) proxy = urlparse(proxy)