Another downloader - now from own library

This commit is contained in:
BarsTiger
2022-05-06 12:50:08 +03:00
parent 71c629f17f
commit a81b9709d6
3 changed files with 3 additions and 68 deletions

View File

@@ -1,66 +0,0 @@
# Idea from rich examples
# https://github.com/Textualize/rich/blob/master/examples/downloader.py
import os.path
from concurrent.futures import ThreadPoolExecutor
import signal
from functools import partial
from threading import Event
from urllib.request import urlopen
from urllib.parse import unquote
from rich.progress import (
BarColumn,
DownloadColumn,
Progress,
TaskID,
TextColumn,
TimeRemainingColumn,
TransferSpeedColumn,
)
progress = Progress(
TextColumn("[bold blue]{task.fields[filename]}", justify="right"),
BarColumn(bar_width=None),
"[progress.percentage]{task.percentage:>3.1f}%",
"",
DownloadColumn(),
"",
TransferSpeedColumn(),
"",
TimeRemainingColumn(),
)
done_event = Event()
def handle_sigint(signum, frame):
done_event.set()
signal.signal(signal.SIGINT, handle_sigint)
def copy_url(task_id: TaskID, url: str, path: str) -> None:
progress.console.log(f"Requesting {url}")
response = urlopen(url)
progress.update(task_id, total=int(response.info()["Content-length"]))
with open(path, "wb") as dest_file:
progress.start_task(task_id)
for data in iter(partial(response.read, 32768), b""):
dest_file.write(data)
progress.update(task_id, advance=len(data))
if done_event.is_set():
return
progress.console.log(f"Downloaded {path}")
def dl(urls, dest_dir: str):
with progress:
with ThreadPoolExecutor(max_workers=len(urls)) as pool:
for url in urls:
filename = unquote(url.split("/")[-1])
dest_path = os.path.join(dest_dir, filename)
task_id = progress.add_task("download", filename=filename, start=False)
pool.submit(copy_url, task_id, url, dest_path)

View File

@@ -7,7 +7,7 @@ import os
import zipfile
from modules.virustotal import scan_to_cli
from modules.http_status import handle
from modules.download import dl
from ezzdl import dl
def install(package):

View File

@@ -7,3 +7,4 @@ algoliasearch
PyQt5
easygui
winshell
ezzdl