Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

3 changed files with 57 additions and 9 deletions

View File

@ -1,7 +0,0 @@
Copyright 2024 hatkidchan <hatkidchan at gmail dot com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

BIN
logo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 132 KiB

59
make.py
View File

@ -1,10 +1,11 @@
#!/usr/bin/env python3
import json
from pathlib import Path
from typing import Union
from typing import Optional, Union
import asyncio
from httpx import AsyncClient
from rich.progress import Progress
from asyncio.queues import Queue
from rich.progress import TaskID, Progress
from zipfile import ZipFile
from PIL import Image
@ -42,6 +43,60 @@ meta_url: str = (
meta_filename = INPUT_PATH / f"meta-{meta_url.split('/')[-1]}"
class AsyncDownloader:
def __init__(
self, files: Optional[list[tuple[str, Path]]] = None, maxfiles: int = 8
):
self._queue: Queue[tuple[str, Path]] = Queue(maxfiles)
self._client = AsyncClient()
self._progress: Optional[Progress] = None
self._tasks: list[TaskID] = []
for url, path in files or []:
self._queue.put_nowait((url, path))
def add_file(self, url: str, path: Union[Path, str]):
self._queue.put_nowait((url, Path(path)))
async def __aenter__(self):
await self._client.__aenter__()
self._progress = Progress()
self._progress.__enter__()
for i in range(self._queue.maxsize):
self._tasks.append(
self._progress.add_task(f"Downloader {i + 1}", start=False)
)
return self
async def _worker(self, task: TaskID):
if not self._progress:
return
while not self._queue.empty():
url, path = await self._queue.get()
with path.open("wb") as fout:
async with self._client.stream("GET", url) as req:
size = int(req.headers.get("Content-Length", 0))
self._progress.start_task(task)
self._progress.update(
task,
total=size,
completed=0,
description=f"{path.name}",
)
async for chunk in req.aiter_bytes(8192):
self._progress.advance(task, fout.write(chunk))
self._progress.stop_task(task)
async def run(self):
await asyncio.gather(*[self._worker(task) for task in self._tasks])
async def __aexit__(self, a, b, c):
if self._progress is None:
raise ValueError("how did that happen?")
self._progress.__exit__(a, b, c)
await self._client.__aexit__()
async def download_if_missing(url: str, path: Union[Path, str]):
path = Path(path)
if path.exists():