diff --git a/twitchdl/commands/clips.py b/twitchdl/commands/clips.py index f4862c8..14c642f 100644 --- a/twitchdl/commands/clips.py +++ b/twitchdl/commands/clips.py @@ -8,7 +8,7 @@ import click from twitchdl import twitch, utils from twitchdl.commands.download import get_clip_authenticated_url -from twitchdl.download import download_file +from twitchdl.http import download_file from twitchdl.output import green, print_clip, print_clip_compact, print_json, print_paged, yellow from twitchdl.twitch import Clip, ClipsPeriod diff --git a/twitchdl/commands/download.py b/twitchdl/commands/download.py index 5023702..604d6d9 100644 --- a/twitchdl/commands/download.py +++ b/twitchdl/commands/download.py @@ -12,10 +12,9 @@ import click import httpx from twitchdl import twitch, utils -from twitchdl.download import download_file from twitchdl.entities import DownloadOptions from twitchdl.exceptions import ConsoleError -from twitchdl.http import download_all +from twitchdl.http import download_all, download_file from twitchdl.naming import clip_filename, video_filename from twitchdl.output import blue, bold, green, print_log, yellow from twitchdl.playlists import ( diff --git a/twitchdl/download.py b/twitchdl/download.py deleted file mode 100644 index 3185b41..0000000 --- a/twitchdl/download.py +++ /dev/null @@ -1,40 +0,0 @@ -import os -from pathlib import Path -from typing import Tuple - -import httpx - -from twitchdl.exceptions import ConsoleError - -CHUNK_SIZE = 1024 -CONNECT_TIMEOUT = 5 -RETRY_COUNT = 5 - - -def _download(url: str, target: Path): - tmp_path = Path(str(target) + ".tmp") - size = 0 - with httpx.stream("GET", url, timeout=CONNECT_TIMEOUT, follow_redirects=True) as response: - response.raise_for_status() - with open(tmp_path, "wb") as f: - for chunk in response.iter_bytes(chunk_size=CHUNK_SIZE): - f.write(chunk) - size += len(chunk) - - os.rename(tmp_path, target) - return size - - -def download_file(url: str, target: Path, retries: int = RETRY_COUNT) -> Tuple[int, bool]: - if target.exists(): - from_disk = True - return os.path.getsize(target), from_disk - - from_disk = False - for _ in range(retries): - try: - return _download(url, target), from_disk - except httpx.RequestError: - pass - - raise ConsoleError(f"Failed downloading after {retries} attempts: {url}") diff --git a/twitchdl/http.py b/twitchdl/http.py index 22e9144..51e12a0 100644 --- a/twitchdl/http.py +++ b/twitchdl/http.py @@ -4,10 +4,11 @@ import os import time from abc import ABC, abstractmethod from pathlib import Path -from typing import List, Optional +from typing import List, Optional, Tuple import httpx +from twitchdl.exceptions import ConsoleError from twitchdl.progress import Progress logger = logging.getLogger(__name__) @@ -143,3 +144,32 @@ async def download_all( for task_id, (source, target) in enumerate(zip(sources, targets)) ] await asyncio.gather(*tasks) + + +def download_file(url: str, target: Path, retries: int = RETRY_COUNT) -> Tuple[int, bool]: + if target.exists(): + from_disk = True + return os.path.getsize(target), from_disk + + from_disk = False + for _ in range(retries): + try: + return _do_download_file(url, target), from_disk + except httpx.RequestError: + pass + + raise ConsoleError(f"Failed downloading after {retries} attempts: {url}") + + +def _do_download_file(url: str, target: Path): + tmp_path = Path(str(target) + ".tmp") + size = 0 + with httpx.stream("GET", url, timeout=TIMEOUT, follow_redirects=True) as response: + response.raise_for_status() + with open(tmp_path, "wb") as f: + for chunk in response.iter_bytes(chunk_size=CHUNK_SIZE): + f.write(chunk) + size += len(chunk) + + os.rename(tmp_path, target) + return size