Move download_file to http

This commit is contained in:
Ivan Habunek 2024-08-28 11:07:25 +02:00
parent 5679e66270
commit 7184feacee
No known key found for this signature in database
GPG Key ID: 01DB3DD0D824504C
4 changed files with 33 additions and 44 deletions

View File

@ -8,7 +8,7 @@ import click
from twitchdl import twitch, utils from twitchdl import twitch, utils
from twitchdl.commands.download import get_clip_authenticated_url 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.output import green, print_clip, print_clip_compact, print_json, print_paged, yellow
from twitchdl.twitch import Clip, ClipsPeriod from twitchdl.twitch import Clip, ClipsPeriod

View File

@ -12,10 +12,9 @@ import click
import httpx import httpx
from twitchdl import twitch, utils from twitchdl import twitch, utils
from twitchdl.download import download_file
from twitchdl.entities import DownloadOptions from twitchdl.entities import DownloadOptions
from twitchdl.exceptions import ConsoleError 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.naming import clip_filename, video_filename
from twitchdl.output import blue, bold, green, print_log, yellow from twitchdl.output import blue, bold, green, print_log, yellow
from twitchdl.playlists import ( from twitchdl.playlists import (

View File

@ -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}")

View File

@ -4,10 +4,11 @@ import os
import time import time
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from typing import List, Optional from typing import List, Optional, Tuple
import httpx import httpx
from twitchdl.exceptions import ConsoleError
from twitchdl.progress import Progress from twitchdl.progress import Progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -143,3 +144,32 @@ async def download_all(
for task_id, (source, target) in enumerate(zip(sources, targets)) for task_id, (source, target) in enumerate(zip(sources, targets))
] ]
await asyncio.gather(*tasks) 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