#!/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()
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)
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] = []
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):