Updated folder structure (for Docker)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
"""JSON-based repository implementations."""
|
||||
|
||||
from .movie_repository import JsonMovieRepository
|
||||
from .subtitle_repository import JsonSubtitleRepository
|
||||
from .tvshow_repository import JsonTVShowRepository
|
||||
|
||||
__all__ = [
|
||||
"JsonMovieRepository",
|
||||
"JsonTVShowRepository",
|
||||
"JsonSubtitleRepository",
|
||||
]
|
||||
@@ -0,0 +1,144 @@
|
||||
"""JSON-based movie repository implementation."""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from domain.movies.entities import Movie
|
||||
from domain.movies.repositories import MovieRepository
|
||||
from domain.movies.value_objects import MovieTitle, Quality, ReleaseYear
|
||||
from domain.shared.value_objects import FilePath, FileSize, ImdbId
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class JsonMovieRepository(MovieRepository):
|
||||
"""
|
||||
JSON-based implementation of MovieRepository.
|
||||
|
||||
Stores movies in the LTM library using the memory context.
|
||||
"""
|
||||
|
||||
def save(self, movie: Movie) -> None:
|
||||
"""
|
||||
Save a movie to the repository.
|
||||
|
||||
Updates existing movie if IMDb ID matches.
|
||||
|
||||
Args:
|
||||
movie: Movie entity to save.
|
||||
"""
|
||||
memory = get_memory()
|
||||
movies = memory.ltm.library.get("movies", [])
|
||||
|
||||
# Remove existing movie with same IMDb ID
|
||||
movies = [m for m in movies if m.get("imdb_id") != str(movie.imdb_id)]
|
||||
|
||||
movies.append(self._to_dict(movie))
|
||||
|
||||
memory.ltm.library["movies"] = movies
|
||||
memory.save()
|
||||
logger.debug(f"Saved movie: {movie.imdb_id}")
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: ImdbId) -> Movie | None:
|
||||
"""
|
||||
Find a movie by its IMDb ID.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to search for.
|
||||
|
||||
Returns:
|
||||
Movie if found, None otherwise.
|
||||
"""
|
||||
memory = get_memory()
|
||||
movies = memory.ltm.library.get("movies", [])
|
||||
|
||||
for movie_dict in movies:
|
||||
if movie_dict.get("imdb_id") == str(imdb_id):
|
||||
return self._from_dict(movie_dict)
|
||||
|
||||
return None
|
||||
|
||||
def find_all(self) -> list[Movie]:
|
||||
"""
|
||||
Get all movies in the repository.
|
||||
|
||||
Returns:
|
||||
List of all Movie entities.
|
||||
"""
|
||||
memory = get_memory()
|
||||
movies_dict = memory.ltm.library.get("movies", [])
|
||||
return [self._from_dict(m) for m in movies_dict]
|
||||
|
||||
def delete(self, imdb_id: ImdbId) -> bool:
|
||||
"""
|
||||
Delete a movie from the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID of movie to delete.
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found.
|
||||
"""
|
||||
memory = get_memory()
|
||||
movies = memory.ltm.library.get("movies", [])
|
||||
initial_count = len(movies)
|
||||
|
||||
movies = [m for m in movies if m.get("imdb_id") != str(imdb_id)]
|
||||
|
||||
if len(movies) < initial_count:
|
||||
memory.ltm.library["movies"] = movies
|
||||
memory.save()
|
||||
logger.debug(f"Deleted movie: {imdb_id}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def exists(self, imdb_id: ImdbId) -> bool:
|
||||
"""
|
||||
Check if a movie exists in the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to check.
|
||||
|
||||
Returns:
|
||||
True if exists, False otherwise.
|
||||
"""
|
||||
return self.find_by_imdb_id(imdb_id) is not None
|
||||
|
||||
def _to_dict(self, movie: Movie) -> dict[str, Any]:
|
||||
"""Convert Movie entity to dict for storage."""
|
||||
return {
|
||||
"imdb_id": str(movie.imdb_id),
|
||||
"title": movie.title.value,
|
||||
"release_year": movie.release_year.value if movie.release_year else None,
|
||||
"quality": movie.quality.value,
|
||||
"file_path": str(movie.file_path) if movie.file_path else None,
|
||||
"file_size": movie.file_size.bytes if movie.file_size else None,
|
||||
"tmdb_id": movie.tmdb_id,
|
||||
"added_at": movie.added_at.isoformat(),
|
||||
}
|
||||
|
||||
def _from_dict(self, data: dict[str, Any]) -> Movie:
|
||||
"""Convert dict from storage to Movie entity."""
|
||||
# Parse quality string to enum
|
||||
quality_str = data.get("quality", "unknown")
|
||||
quality = Quality.from_string(quality_str)
|
||||
|
||||
return Movie(
|
||||
imdb_id=ImdbId(data["imdb_id"]),
|
||||
title=MovieTitle(data["title"]),
|
||||
release_year=(
|
||||
ReleaseYear(data["release_year"]) if data.get("release_year") else None
|
||||
),
|
||||
quality=quality,
|
||||
file_path=FilePath(data["file_path"]) if data.get("file_path") else None,
|
||||
file_size=FileSize(data["file_size"]) if data.get("file_size") else None,
|
||||
tmdb_id=data.get("tmdb_id"),
|
||||
added_at=(
|
||||
datetime.fromisoformat(data["added_at"])
|
||||
if data.get("added_at")
|
||||
else datetime.now()
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,144 @@
|
||||
"""JSON-based subtitle repository implementation."""
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from domain.shared.value_objects import FilePath, ImdbId
|
||||
from domain.subtitles.entities import Subtitle
|
||||
from domain.subtitles.repositories import SubtitleRepository
|
||||
from domain.subtitles.value_objects import Language, SubtitleFormat, TimingOffset
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class JsonSubtitleRepository(SubtitleRepository):
|
||||
"""
|
||||
JSON-based implementation of SubtitleRepository.
|
||||
|
||||
Stores subtitles in the LTM library using the memory context.
|
||||
"""
|
||||
|
||||
def save(self, subtitle: Subtitle) -> None:
|
||||
"""
|
||||
Save a subtitle to the repository.
|
||||
|
||||
Multiple subtitles can exist for the same media.
|
||||
|
||||
Args:
|
||||
subtitle: Subtitle entity to save.
|
||||
"""
|
||||
memory = get_memory()
|
||||
subtitles = memory.ltm.library.get("subtitles", [])
|
||||
|
||||
subtitles.append(self._to_dict(subtitle))
|
||||
|
||||
if "subtitles" not in memory.ltm.library:
|
||||
memory.ltm.library["subtitles"] = []
|
||||
memory.ltm.library["subtitles"] = subtitles
|
||||
memory.save()
|
||||
logger.debug(f"Saved subtitle for: {subtitle.media_imdb_id}")
|
||||
|
||||
def find_by_media(
|
||||
self,
|
||||
media_imdb_id: ImdbId,
|
||||
language: Language | None = None,
|
||||
season: int | None = None,
|
||||
episode: int | None = None,
|
||||
) -> list[Subtitle]:
|
||||
"""
|
||||
Find subtitles for a media item.
|
||||
|
||||
Args:
|
||||
media_imdb_id: IMDb ID of the media.
|
||||
language: Optional language filter.
|
||||
season: Optional season number filter.
|
||||
episode: Optional episode number filter.
|
||||
|
||||
Returns:
|
||||
List of matching Subtitle entities.
|
||||
"""
|
||||
memory = get_memory()
|
||||
subtitles = memory.ltm.library.get("subtitles", [])
|
||||
results = []
|
||||
|
||||
for sub_dict in subtitles:
|
||||
if sub_dict.get("media_imdb_id") != str(media_imdb_id):
|
||||
continue
|
||||
|
||||
if language and sub_dict.get("language") != language.value:
|
||||
continue
|
||||
|
||||
if season is not None and sub_dict.get("season_number") != season:
|
||||
continue
|
||||
|
||||
if episode is not None and sub_dict.get("episode_number") != episode:
|
||||
continue
|
||||
|
||||
results.append(self._from_dict(sub_dict))
|
||||
|
||||
return results
|
||||
|
||||
def delete(self, subtitle: Subtitle) -> bool:
|
||||
"""
|
||||
Delete a subtitle from the repository.
|
||||
|
||||
Matches by file path.
|
||||
|
||||
Args:
|
||||
subtitle: Subtitle entity to delete.
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found.
|
||||
"""
|
||||
memory = get_memory()
|
||||
subtitles = memory.ltm.library.get("subtitles", [])
|
||||
initial_count = len(subtitles)
|
||||
|
||||
subtitles = [
|
||||
s for s in subtitles if s.get("file_path") != str(subtitle.file_path)
|
||||
]
|
||||
|
||||
if len(subtitles) < initial_count:
|
||||
memory.ltm.library["subtitles"] = subtitles
|
||||
memory.save()
|
||||
logger.debug(f"Deleted subtitle: {subtitle.file_path}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def _to_dict(self, subtitle: Subtitle) -> dict[str, Any]:
|
||||
"""Convert Subtitle entity to dict for storage."""
|
||||
return {
|
||||
"media_imdb_id": str(subtitle.media_imdb_id),
|
||||
"language": subtitle.language.value,
|
||||
"format": subtitle.format.value,
|
||||
"file_path": str(subtitle.file_path),
|
||||
"season_number": subtitle.season_number,
|
||||
"episode_number": subtitle.episode_number,
|
||||
"timing_offset": subtitle.timing_offset.milliseconds,
|
||||
"hearing_impaired": subtitle.hearing_impaired,
|
||||
"forced": subtitle.forced,
|
||||
"source": subtitle.source,
|
||||
"uploader": subtitle.uploader,
|
||||
"download_count": subtitle.download_count,
|
||||
"rating": subtitle.rating,
|
||||
}
|
||||
|
||||
def _from_dict(self, data: dict[str, Any]) -> Subtitle:
|
||||
"""Convert dict from storage to Subtitle entity."""
|
||||
return Subtitle(
|
||||
media_imdb_id=ImdbId(data["media_imdb_id"]),
|
||||
language=Language.from_code(data["language"]),
|
||||
format=SubtitleFormat.from_extension(data["format"]),
|
||||
file_path=FilePath(data["file_path"]),
|
||||
season_number=data.get("season_number"),
|
||||
episode_number=data.get("episode_number"),
|
||||
timing_offset=TimingOffset(data.get("timing_offset", 0)),
|
||||
hearing_impaired=data.get("hearing_impaired", False),
|
||||
forced=data.get("forced", False),
|
||||
source=data.get("source"),
|
||||
uploader=data.get("uploader"),
|
||||
download_count=data.get("download_count"),
|
||||
rating=data.get("rating"),
|
||||
)
|
||||
@@ -0,0 +1,136 @@
|
||||
"""JSON-based TV show repository implementation."""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from domain.shared.value_objects import ImdbId
|
||||
from domain.tv_shows.entities import TVShow
|
||||
from domain.tv_shows.repositories import TVShowRepository
|
||||
from domain.tv_shows.value_objects import ShowStatus
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class JsonTVShowRepository(TVShowRepository):
|
||||
"""
|
||||
JSON-based implementation of TVShowRepository.
|
||||
|
||||
Stores TV shows in the LTM library using the memory context.
|
||||
"""
|
||||
|
||||
def save(self, show: TVShow) -> None:
|
||||
"""
|
||||
Save a TV show to the repository.
|
||||
|
||||
Updates existing show if IMDb ID matches.
|
||||
|
||||
Args:
|
||||
show: TVShow entity to save.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
# Remove existing show with same IMDb ID
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(show.imdb_id)]
|
||||
|
||||
shows.append(self._to_dict(show))
|
||||
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
logger.debug(f"Saved TV show: {show.imdb_id}")
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: ImdbId) -> TVShow | None:
|
||||
"""
|
||||
Find a TV show by its IMDb ID.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to search for.
|
||||
|
||||
Returns:
|
||||
TVShow if found, None otherwise.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
for show_dict in shows:
|
||||
if show_dict.get("imdb_id") == str(imdb_id):
|
||||
return self._from_dict(show_dict)
|
||||
|
||||
return None
|
||||
|
||||
def find_all(self) -> list[TVShow]:
|
||||
"""
|
||||
Get all TV shows in the repository.
|
||||
|
||||
Returns:
|
||||
List of all TVShow entities.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows_dict = memory.ltm.library.get("tv_shows", [])
|
||||
return [self._from_dict(s) for s in shows_dict]
|
||||
|
||||
def delete(self, imdb_id: ImdbId) -> bool:
|
||||
"""
|
||||
Delete a TV show from the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID of show to delete.
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
initial_count = len(shows)
|
||||
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(imdb_id)]
|
||||
|
||||
if len(shows) < initial_count:
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
logger.debug(f"Deleted TV show: {imdb_id}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def exists(self, imdb_id: ImdbId) -> bool:
|
||||
"""
|
||||
Check if a TV show exists in the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to check.
|
||||
|
||||
Returns:
|
||||
True if exists, False otherwise.
|
||||
"""
|
||||
return self.find_by_imdb_id(imdb_id) is not None
|
||||
|
||||
def _to_dict(self, show: TVShow) -> dict[str, Any]:
|
||||
"""Convert TVShow entity to dict for storage."""
|
||||
return {
|
||||
"imdb_id": str(show.imdb_id),
|
||||
"title": show.title,
|
||||
"seasons_count": show.seasons_count,
|
||||
"status": show.status.value,
|
||||
"tmdb_id": show.tmdb_id,
|
||||
"first_air_date": show.first_air_date,
|
||||
"added_at": show.added_at.isoformat(),
|
||||
}
|
||||
|
||||
def _from_dict(self, data: dict[str, Any]) -> TVShow:
|
||||
"""Convert dict from storage to TVShow entity."""
|
||||
return TVShow(
|
||||
imdb_id=ImdbId(data["imdb_id"]),
|
||||
title=data["title"],
|
||||
seasons_count=data["seasons_count"],
|
||||
status=ShowStatus.from_string(data["status"]),
|
||||
tmdb_id=data.get("tmdb_id"),
|
||||
first_air_date=data.get("first_air_date"),
|
||||
added_at=(
|
||||
datetime.fromisoformat(data["added_at"])
|
||||
if data.get("added_at")
|
||||
else datetime.now()
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user