home · contact · privacy
Refactor DownloadsDb (now DownloadsManager). master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 01:45:09 +0000 (02:45 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 01:45:09 +0000 (02:45 +0100)
src/serve.py
src/ytplom/misc.py

index a3f4c79efa91d3a0366c3216264ba107d7799a80..368a9f1ec6d0d391048f4136df2d11020b3236fd 100755 (executable)
@@ -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()
index 097c8c0c9ba928c4fba063f5d1577f1143ee32bb..2618fe9cfb444aae09b93126e3fd8313bbb3a6f4 100644 (file)
@@ -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):