Skip to content

Commit

Permalink
chore(api): seperate out core files
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Sep 8, 2023
1 parent 39ccd7f commit 9ff2dae
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 52 deletions.
File renamed without changes.
44 changes: 44 additions & 0 deletions apps/api/app/core/managers.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions apps/api/app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down
4 changes: 2 additions & 2 deletions apps/api/app/routers/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/routers/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
47 changes: 3 additions & 44 deletions apps/api/app/utils/managers.py → apps/api/app/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

1 comment on commit 9ff2dae

@vercel
Copy link

@vercel vercel bot commented on 9ff2dae Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.