From: Christian Heller Date: Wed, 27 Nov 2024 01:45:09 +0000 (+0100) Subject: Refactor DownloadsDb (now DownloadsManager). X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%22https:/validator.w3.org/ledger?a=commitdiff_plain;h=15390131aecf090a3d6a94bae15e7e4295e012e7;p=ytplom Refactor DownloadsDb (now DownloadsManager). --- diff --git a/src/serve.py b/src/serve.py index a3f4c79..368a9f1 100755 --- a/src/serve.py +++ b/src/serve.py @@ -1,14 +1,10 @@ #!/usr/bin/env python3 """Minimalistic download-focused YouTube interface.""" -from threading import Thread -from ytplom.misc import DownloadsDb, HTTP_PORT, Server, TaskHandler +from ytplom.misc import HTTP_PORT, Server, TaskHandler if __name__ == '__main__': - downloads_db = DownloadsDb() - downloads_db.clean_unfinished() - Thread(target=downloads_db.download_loop, daemon=False).start() - server = Server(downloads_db, ('0.0.0.0', HTTP_PORT), TaskHandler) + server = Server(('0.0.0.0', HTTP_PORT), TaskHandler) print(f'running at port {HTTP_PORT}') try: server.serve_forever() diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 097c8c0..2618fe9 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -15,6 +15,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs from urllib.request import urlretrieve from urllib.error import HTTPError +from threading import Thread # non-included libs from jinja2 import ( # type: ignore Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) @@ -546,8 +547,8 @@ class Player: self._signal_update() -class DownloadsDb: - """Collections downloading-related stuff.""" +class DownloadsManager: + """Manages downloading and downloads access.""" def __init__(self) -> None: self._to_download: list[YoutubeId] = [] @@ -622,21 +623,25 @@ class DownloadsDb: ydl.download([f'{YOUTUBE_URL_PREFIX}{video_id}']) self._sync_db() - def download_loop(self) -> None: + def start_thread(self) -> None: """Keep iterating through download queue for new download tasks.""" - while True: - sleep(0.5) - self._download_next() + def loop(): + while True: + sleep(0.5) + self._download_next() + Thread(target=loop, daemon=False).start() class Server(HTTPServer): - """Extension of HTTPServer providing for Player and DownloadsDb.""" + """Extension of HTTPServer providing for Player and DownloadsManager.""" - def __init__(self, downloads_db: DownloadsDb, *args, **kwargs) -> None: + def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.jinja = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES)) self.player = Player() - self.downloads = downloads_db + self.downloads = DownloadsManager() + self.downloads.clean_unfinished() + self.downloads.start_thread() class TaskHandler(BaseHTTPRequestHandler):