From 9ff2dae6464b46ff6d5d338fef085f5fe3ce49f4 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Fri, 8 Sep 2023 15:16:03 +1000 Subject: [PATCH] chore(api): seperate out core files Signed-off-by: Jordan Shatford --- apps/api/app/{utils => core}/__init__.py | 0 apps/api/app/core/managers.py | 44 +++++++++++++++++ apps/api/app/{utils => core}/threads.py | 0 apps/api/app/{utils => core}/youtube.py | 0 apps/api/app/dependencies.py | 6 +-- apps/api/app/main.py | 2 +- apps/api/app/routers/downloads.py | 4 +- apps/api/app/routers/search.py | 2 +- apps/api/app/routers/session.py | 2 +- .../api/app/{utils/managers.py => session.py} | 47 ++----------------- 10 files changed, 55 insertions(+), 52 deletions(-) rename apps/api/app/{utils => core}/__init__.py (100%) create mode 100644 apps/api/app/core/managers.py rename apps/api/app/{utils => core}/threads.py (100%) rename apps/api/app/{utils => core}/youtube.py (100%) rename apps/api/app/{utils/managers.py => session.py} (61%) diff --git a/apps/api/app/utils/__init__.py b/apps/api/app/core/__init__.py similarity index 100% rename from apps/api/app/utils/__init__.py rename to apps/api/app/core/__init__.py diff --git a/apps/api/app/core/managers.py b/apps/api/app/core/managers.py new file mode 100644 index 00000000..6967e812 --- /dev/null +++ b/apps/api/app/core/managers.py @@ -0,0 +1,44 @@ +from typing import Callable + +from ..models import DownloadStatusUpdate +from ..models import VideoWithOptions +from ..models import VideoWithOptionsAndStatus +from .threads import YoutubeDownloadThread + + +class AudioDownloadManager: + def __init__( + self, output_dir: str, + status_hook: Callable[[DownloadStatusUpdate], None], + ): + self._status_hook = status_hook + self._downloads: dict[str, YoutubeDownloadThread] = {} + self._output_dir = output_dir + + def __contains__(self, video_id: str) -> bool: + return video_id in self._downloads + + def add(self, video: VideoWithOptions) -> None: + if video.id not in self._downloads: + download = YoutubeDownloadThread( + video, self._output_dir, self.send_status_update, + ) + self._downloads[video.id] = download + download.start() + + def remove(self, video_id: str) -> None: + if video_id in self._downloads: + self._downloads[video_id].remove() + self._downloads.pop(video_id, None) + + def get(self, video_id: str) -> YoutubeDownloadThread | None: + return self._downloads.get(video_id, None) + + def get_all_videos(self) -> list[VideoWithOptionsAndStatus]: + return [ + VideoWithOptionsAndStatus(**d.video.dict(), status=d.status) + for d in self._downloads.values() + ] + + def send_status_update(self, update: DownloadStatusUpdate) -> None: + self._status_hook(update) diff --git a/apps/api/app/utils/threads.py b/apps/api/app/core/threads.py similarity index 100% rename from apps/api/app/utils/threads.py rename to apps/api/app/core/threads.py diff --git a/apps/api/app/utils/youtube.py b/apps/api/app/core/youtube.py similarity index 100% rename from apps/api/app/utils/youtube.py rename to apps/api/app/core/youtube.py diff --git a/apps/api/app/dependencies.py b/apps/api/app/dependencies.py index 718b06d6..ce20aabc 100644 --- a/apps/api/app/dependencies.py +++ b/apps/api/app/dependencies.py @@ -8,9 +8,9 @@ from fastapi.security import HTTPAuthorizationCredentials from fastapi.security import HTTPBearer -from .utils.managers import Session -from .utils.managers import session_manager -from .utils.threads import YoutubeDownloadThread +from .core.threads import YoutubeDownloadThread +from .session import Session +from .session import session_manager AdditionResponses: TypeAlias = dict[int | str, dict[str, Any]] diff --git a/apps/api/app/main.py b/apps/api/app/main.py index 6736a6b5..3cc81668 100644 --- a/apps/api/app/main.py +++ b/apps/api/app/main.py @@ -9,7 +9,7 @@ from .routers import downloads from .routers import search from .routers import session -from .utils.managers import session_manager +from .session import session_manager # Read data from pyproject.toml file pyproject_data: dict[str, Any] = {} diff --git a/apps/api/app/routers/downloads.py b/apps/api/app/routers/downloads.py index 8aa13c1d..ca3cf0b5 100644 --- a/apps/api/app/routers/downloads.py +++ b/apps/api/app/routers/downloads.py @@ -16,8 +16,8 @@ from ..models import DownloadStatusUpdate from ..models import VideoWithOptions from ..models import VideoWithOptionsAndStatus -from ..utils.managers import Session -from ..utils.managers import session_manager +from ..session import Session +from ..session import session_manager router = APIRouter( prefix='/downloads', diff --git a/apps/api/app/routers/search.py b/apps/api/app/routers/search.py index 875ed367..74e97b64 100644 --- a/apps/api/app/routers/search.py +++ b/apps/api/app/routers/search.py @@ -2,10 +2,10 @@ from fastapi import HTTPException from fastapi import status +from ..core.youtube import search_youtube from ..dependencies import depends_session_responses from ..dependencies import DependsSession from ..models import Video -from ..utils.youtube import search_youtube router = APIRouter( prefix='/search', diff --git a/apps/api/app/routers/session.py b/apps/api/app/routers/session.py index 8a52dcfc..6d25db8b 100644 --- a/apps/api/app/routers/session.py +++ b/apps/api/app/routers/session.py @@ -4,7 +4,7 @@ from ..dependencies import depends_session_responses from ..dependencies import DependsSession from ..models import Session -from ..utils.managers import session_manager +from ..session import session_manager router = APIRouter( prefix='/session', diff --git a/apps/api/app/utils/managers.py b/apps/api/app/session.py similarity index 61% rename from apps/api/app/utils/managers.py rename to apps/api/app/session.py index cf2a0de6..b16370cc 100644 --- a/apps/api/app/utils/managers.py +++ b/apps/api/app/session.py @@ -4,51 +4,10 @@ from datetime import datetime from datetime import timedelta from queue import Queue -from typing import Callable -from ..models import DownloadStatusUpdate -from ..models import VideoWithOptions -from ..models import VideoWithOptionsAndStatus -from .threads import RepeatedTimer -from .threads import YoutubeDownloadThread - - -class AudioDownloadManager: - def __init__( - self, output_dir: str, - status_hook: Callable[[DownloadStatusUpdate], None], - ): - self._status_hook = status_hook - self._downloads: dict[str, YoutubeDownloadThread] = {} - self._output_dir = output_dir - - def __contains__(self, video_id: str) -> bool: - return video_id in self._downloads - - def add(self, video: VideoWithOptions) -> None: - if video.id not in self._downloads: - download = YoutubeDownloadThread( - video, self._output_dir, self.send_status_update, - ) - self._downloads[video.id] = download - download.start() - - def remove(self, video_id: str) -> None: - if video_id in self._downloads: - self._downloads[video_id].remove() - self._downloads.pop(video_id, None) - - def get(self, video_id: str) -> YoutubeDownloadThread | None: - return self._downloads.get(video_id, None) - - def get_all_videos(self) -> list[VideoWithOptionsAndStatus]: - return [ - VideoWithOptionsAndStatus(**d.video.dict(), status=d.status) - for d in self._downloads.values() - ] - - def send_status_update(self, update: DownloadStatusUpdate) -> None: - self._status_hook(update) +from .core.managers import AudioDownloadManager +from .core.threads import RepeatedTimer +from .models import DownloadStatusUpdate class Session: