Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Sep 13, 2024
1 parent bfc9642 commit f17c53f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions twitchdl/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Iterable, NamedTuple, Optional, Tuple
from typing import Iterable, List, NamedTuple, Optional, Tuple

import httpx

Expand Down Expand Up @@ -82,6 +82,7 @@ async def download(
tmp_target = f"{target}.tmp"
with open(tmp_target, "wb") as f:
async with client.stream("GET", source) as response:
response.raise_for_status()
size = int(response.headers.get("content-length"))
progress.start(task_id, size)
async for chunk in response.aiter_bytes(chunk_size=CHUNK_SIZE):
Expand Down Expand Up @@ -126,7 +127,7 @@ class QueueItem(NamedTuple):

class DownloadAllResult(NamedTuple):
ok: bool
exception: Optional[BaseException] = None
exceptions: Optional[List[Exception]] = None


async def download_worker(
Expand Down Expand Up @@ -182,20 +183,17 @@ async def producer():

# Since the worker tasks run an endless loop, they will only finish if
# an exception is raised.
exception: Optional[BaseException] = None
if not queue_complete.done():
for t in tasks:
if t.done():
exception = t.exception()
break
success = queue_complete.done()

# Cancel tasks and wait until they are cancelled
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)

success = False if exception else True
return DownloadAllResult(success, exception)
# results = await asyncio.gather(*tasks, return_exceptions=True)
results = await asyncio.gather(*tasks)
exceptions = [r for r in results if isinstance(r, Exception)]

return DownloadAllResult(success, exceptions)


def download_file(url: str, target: Path, retries: int = RETRY_COUNT) -> None:
Expand Down

0 comments on commit f17c53f

Please sign in to comment.