mirror of
https://github.com/ihabunek/twitch-dl
synced 2024-08-30 18:32:25 +00:00
Move download_file to http
This commit is contained in:
parent
5679e66270
commit
7184feacee
@ -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
|
||||
|
||||
|
@ -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 (
|
||||
|
@ -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}")
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user