New archi: domain driven development
Working but need to check out code
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Application layer - Use cases and application services."""
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Filesystem use cases."""
|
||||
from .set_folder_path import SetFolderPathUseCase
|
||||
from .list_folder import ListFolderUseCase
|
||||
from .dto import SetFolderPathResponse, ListFolderResponse
|
||||
|
||||
__all__ = [
|
||||
"SetFolderPathUseCase",
|
||||
"ListFolderUseCase",
|
||||
"SetFolderPathResponse",
|
||||
"ListFolderResponse",
|
||||
]
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Filesystem application DTOs."""
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
@dataclass
|
||||
class SetFolderPathResponse:
|
||||
"""Response from setting a folder path."""
|
||||
status: str
|
||||
folder_name: Optional[str] = None
|
||||
path: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dict for agent compatibility."""
|
||||
result = {"status": self.status}
|
||||
|
||||
if self.error:
|
||||
result["error"] = self.error
|
||||
result["message"] = self.message
|
||||
else:
|
||||
if self.folder_name:
|
||||
result["folder_name"] = self.folder_name
|
||||
if self.path:
|
||||
result["path"] = self.path
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@dataclass
|
||||
class ListFolderResponse:
|
||||
"""Response from listing a folder."""
|
||||
status: str
|
||||
folder_type: Optional[str] = None
|
||||
path: Optional[str] = None
|
||||
entries: Optional[List[str]] = None
|
||||
count: Optional[int] = None
|
||||
error: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dict for agent compatibility."""
|
||||
result = {"status": self.status}
|
||||
|
||||
if self.error:
|
||||
result["error"] = self.error
|
||||
result["message"] = self.message
|
||||
else:
|
||||
if self.folder_type:
|
||||
result["folder_type"] = self.folder_type
|
||||
if self.path:
|
||||
result["path"] = self.path
|
||||
if self.entries is not None:
|
||||
result["entries"] = self.entries
|
||||
if self.count is not None:
|
||||
result["count"] = self.count
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,52 @@
|
||||
"""List folder use case."""
|
||||
import logging
|
||||
|
||||
from infrastructure.filesystem import FileManager
|
||||
from .dto import ListFolderResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ListFolderUseCase:
|
||||
"""
|
||||
Use case for listing folder contents.
|
||||
|
||||
This orchestrates the FileManager to list folders.
|
||||
"""
|
||||
|
||||
def __init__(self, file_manager: FileManager):
|
||||
"""
|
||||
Initialize use case.
|
||||
|
||||
Args:
|
||||
file_manager: FileManager instance
|
||||
"""
|
||||
self.file_manager = file_manager
|
||||
|
||||
def execute(self, folder_type: str, path: str = ".") -> ListFolderResponse:
|
||||
"""
|
||||
List contents of a folder.
|
||||
|
||||
Args:
|
||||
folder_type: Type of folder to list (download, tvshow, movie, torrent)
|
||||
path: Relative path within the folder (default: ".")
|
||||
|
||||
Returns:
|
||||
ListFolderResponse with folder contents or error information
|
||||
"""
|
||||
result = self.file_manager.list_folder(folder_type, path)
|
||||
|
||||
if result.get("status") == "ok":
|
||||
return ListFolderResponse(
|
||||
status="ok",
|
||||
folder_type=result.get("folder_type"),
|
||||
path=result.get("path"),
|
||||
entries=result.get("entries"),
|
||||
count=result.get("count")
|
||||
)
|
||||
else:
|
||||
return ListFolderResponse(
|
||||
status="error",
|
||||
error=result.get("error"),
|
||||
message=result.get("message")
|
||||
)
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Set folder path use case."""
|
||||
import logging
|
||||
|
||||
from infrastructure.filesystem import FileManager
|
||||
from .dto import SetFolderPathResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SetFolderPathUseCase:
|
||||
"""
|
||||
Use case for setting a folder path in configuration.
|
||||
|
||||
This orchestrates the FileManager to set folder paths.
|
||||
"""
|
||||
|
||||
def __init__(self, file_manager: FileManager):
|
||||
"""
|
||||
Initialize use case.
|
||||
|
||||
Args:
|
||||
file_manager: FileManager instance
|
||||
"""
|
||||
self.file_manager = file_manager
|
||||
|
||||
def execute(self, folder_name: str, path_value: str) -> SetFolderPathResponse:
|
||||
"""
|
||||
Set a folder path in configuration.
|
||||
|
||||
Args:
|
||||
folder_name: Name of folder to set (download, tvshow, movie, torrent)
|
||||
path_value: Absolute path to the folder
|
||||
|
||||
Returns:
|
||||
SetFolderPathResponse with success or error information
|
||||
"""
|
||||
result = self.file_manager.set_folder_path(folder_name, path_value)
|
||||
|
||||
if result.get("status") == "ok":
|
||||
return SetFolderPathResponse(
|
||||
status="ok",
|
||||
folder_name=result.get("folder_name"),
|
||||
path=result.get("path")
|
||||
)
|
||||
else:
|
||||
return SetFolderPathResponse(
|
||||
status="error",
|
||||
error=result.get("error"),
|
||||
message=result.get("message")
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
"""Movie use cases."""
|
||||
from .search_movie import SearchMovieUseCase
|
||||
from .dto import SearchMovieResponse
|
||||
|
||||
__all__ = [
|
||||
"SearchMovieUseCase",
|
||||
"SearchMovieResponse",
|
||||
]
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Movie application DTOs."""
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class SearchMovieResponse:
|
||||
"""Response from searching for a movie."""
|
||||
status: str
|
||||
imdb_id: Optional[str] = None
|
||||
title: Optional[str] = None
|
||||
media_type: Optional[str] = None
|
||||
tmdb_id: Optional[int] = None
|
||||
overview: Optional[str] = None
|
||||
release_date: Optional[str] = None
|
||||
vote_average: Optional[float] = None
|
||||
error: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dict for agent compatibility."""
|
||||
result = {"status": self.status}
|
||||
|
||||
if self.error:
|
||||
result["error"] = self.error
|
||||
result["message"] = self.message
|
||||
else:
|
||||
if self.imdb_id:
|
||||
result["imdb_id"] = self.imdb_id
|
||||
if self.title:
|
||||
result["title"] = self.title
|
||||
if self.media_type:
|
||||
result["media_type"] = self.media_type
|
||||
if self.tmdb_id:
|
||||
result["tmdb_id"] = self.tmdb_id
|
||||
if self.overview:
|
||||
result["overview"] = self.overview
|
||||
if self.release_date:
|
||||
result["release_date"] = self.release_date
|
||||
if self.vote_average:
|
||||
result["vote_average"] = self.vote_average
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,95 @@
|
||||
"""Search movie use case."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from infrastructure.api.tmdb import TMDBClient, TMDBNotFoundError, TMDBAPIError, TMDBConfigurationError
|
||||
from .dto import SearchMovieResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SearchMovieUseCase:
|
||||
"""
|
||||
Use case for searching a movie and retrieving its IMDb ID.
|
||||
|
||||
This orchestrates the TMDB API client to find movie information.
|
||||
"""
|
||||
|
||||
def __init__(self, tmdb_client: TMDBClient):
|
||||
"""
|
||||
Initialize use case.
|
||||
|
||||
Args:
|
||||
tmdb_client: TMDB API client
|
||||
"""
|
||||
self.tmdb_client = tmdb_client
|
||||
|
||||
def execute(self, media_title: str) -> SearchMovieResponse:
|
||||
"""
|
||||
Search for a movie by title.
|
||||
|
||||
Args:
|
||||
media_title: Title of the movie to search for
|
||||
|
||||
Returns:
|
||||
SearchMovieResponse with movie information or error
|
||||
"""
|
||||
try:
|
||||
# Use the TMDB client to search for media
|
||||
result = self.tmdb_client.search_media(media_title)
|
||||
|
||||
# Check if IMDb ID was found
|
||||
if result.imdb_id:
|
||||
logger.info(f"IMDb ID found for '{media_title}': {result.imdb_id}")
|
||||
return SearchMovieResponse(
|
||||
status="ok",
|
||||
imdb_id=result.imdb_id,
|
||||
title=result.title,
|
||||
media_type=result.media_type,
|
||||
tmdb_id=result.tmdb_id,
|
||||
overview=result.overview,
|
||||
release_date=result.release_date,
|
||||
vote_average=result.vote_average
|
||||
)
|
||||
else:
|
||||
logger.warning(f"No IMDb ID available for '{media_title}'")
|
||||
return SearchMovieResponse(
|
||||
status="ok",
|
||||
title=result.title,
|
||||
media_type=result.media_type,
|
||||
tmdb_id=result.tmdb_id,
|
||||
error="no_imdb_id",
|
||||
message=f"No IMDb ID available for '{result.title}'"
|
||||
)
|
||||
|
||||
except TMDBNotFoundError as e:
|
||||
logger.info(f"Media not found: {e}")
|
||||
return SearchMovieResponse(
|
||||
status="error",
|
||||
error="not_found",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except TMDBConfigurationError as e:
|
||||
logger.error(f"TMDB configuration error: {e}")
|
||||
return SearchMovieResponse(
|
||||
status="error",
|
||||
error="configuration_error",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except TMDBAPIError as e:
|
||||
logger.error(f"TMDB API error: {e}")
|
||||
return SearchMovieResponse(
|
||||
status="error",
|
||||
error="api_error",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {e}")
|
||||
return SearchMovieResponse(
|
||||
status="error",
|
||||
error="validation_failed",
|
||||
message=str(e)
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Torrent use cases."""
|
||||
from .search_torrents import SearchTorrentsUseCase
|
||||
from .add_torrent import AddTorrentUseCase
|
||||
from .dto import SearchTorrentsResponse, AddTorrentResponse
|
||||
|
||||
__all__ = [
|
||||
"SearchTorrentsUseCase",
|
||||
"AddTorrentUseCase",
|
||||
"SearchTorrentsResponse",
|
||||
"AddTorrentResponse",
|
||||
]
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Add torrent use case."""
|
||||
import logging
|
||||
|
||||
from infrastructure.api.qbittorrent import QBittorrentClient, QBittorrentAuthError, QBittorrentAPIError
|
||||
from .dto import AddTorrentResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AddTorrentUseCase:
|
||||
"""
|
||||
Use case for adding a torrent to qBittorrent.
|
||||
|
||||
This orchestrates the qBittorrent API client to add torrents.
|
||||
"""
|
||||
|
||||
def __init__(self, qbittorrent_client: QBittorrentClient):
|
||||
"""
|
||||
Initialize use case.
|
||||
|
||||
Args:
|
||||
qbittorrent_client: qBittorrent API client
|
||||
"""
|
||||
self.qbittorrent_client = qbittorrent_client
|
||||
|
||||
def execute(self, magnet_link: str) -> AddTorrentResponse:
|
||||
"""
|
||||
Add a torrent to qBittorrent using a magnet link.
|
||||
|
||||
Args:
|
||||
magnet_link: Magnet link of the torrent to add
|
||||
|
||||
Returns:
|
||||
AddTorrentResponse with success or error information
|
||||
"""
|
||||
try:
|
||||
# Validate magnet link
|
||||
if not magnet_link or not isinstance(magnet_link, str):
|
||||
raise ValueError("Magnet link must be a non-empty string")
|
||||
|
||||
if not magnet_link.startswith("magnet:"):
|
||||
raise ValueError("Invalid magnet link format")
|
||||
|
||||
logger.info("Adding torrent to qBittorrent")
|
||||
|
||||
# Add torrent to qBittorrent
|
||||
success = self.qbittorrent_client.add_torrent(magnet_link)
|
||||
|
||||
if success:
|
||||
logger.info("Torrent added successfully to qBittorrent")
|
||||
return AddTorrentResponse(
|
||||
status="ok",
|
||||
message="Torrent added successfully to qBittorrent"
|
||||
)
|
||||
else:
|
||||
logger.warning("Failed to add torrent to qBittorrent")
|
||||
return AddTorrentResponse(
|
||||
status="error",
|
||||
error="add_failed",
|
||||
message="Failed to add torrent to qBittorrent"
|
||||
)
|
||||
|
||||
except QBittorrentAuthError as e:
|
||||
logger.error(f"qBittorrent authentication error: {e}")
|
||||
return AddTorrentResponse(
|
||||
status="error",
|
||||
error="authentication_failed",
|
||||
message="Failed to authenticate with qBittorrent"
|
||||
)
|
||||
|
||||
except QBittorrentAPIError as e:
|
||||
logger.error(f"qBittorrent API error: {e}")
|
||||
return AddTorrentResponse(
|
||||
status="error",
|
||||
error="api_error",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {e}")
|
||||
return AddTorrentResponse(
|
||||
status="error",
|
||||
error="validation_failed",
|
||||
message=str(e)
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Torrent application DTOs."""
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List, Dict, Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class SearchTorrentsResponse:
|
||||
"""Response from searching for torrents."""
|
||||
status: str
|
||||
torrents: Optional[List[Dict[str, Any]]] = None
|
||||
count: Optional[int] = None
|
||||
error: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dict for agent compatibility."""
|
||||
result = {"status": self.status}
|
||||
|
||||
if self.error:
|
||||
result["error"] = self.error
|
||||
result["message"] = self.message
|
||||
else:
|
||||
if self.torrents is not None:
|
||||
result["torrents"] = self.torrents
|
||||
if self.count is not None:
|
||||
result["count"] = self.count
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@dataclass
|
||||
class AddTorrentResponse:
|
||||
"""Response from adding a torrent."""
|
||||
status: str
|
||||
message: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dict for agent compatibility."""
|
||||
result = {"status": self.status}
|
||||
|
||||
if self.error:
|
||||
result["error"] = self.error
|
||||
if self.message:
|
||||
result["message"] = self.message
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,94 @@
|
||||
"""Search torrents use case."""
|
||||
import logging
|
||||
|
||||
from infrastructure.api.knaben import KnabenClient, KnabenNotFoundError, KnabenAPIError
|
||||
from .dto import SearchTorrentsResponse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SearchTorrentsUseCase:
|
||||
"""
|
||||
Use case for searching torrents.
|
||||
|
||||
This orchestrates the Knaben API client to find torrents.
|
||||
"""
|
||||
|
||||
def __init__(self, knaben_client: KnabenClient):
|
||||
"""
|
||||
Initialize use case.
|
||||
|
||||
Args:
|
||||
knaben_client: Knaben API client
|
||||
"""
|
||||
self.knaben_client = knaben_client
|
||||
|
||||
def execute(self, media_title: str, limit: int = 10) -> SearchTorrentsResponse:
|
||||
"""
|
||||
Search for torrents by media title.
|
||||
|
||||
Args:
|
||||
media_title: Title of the media to search for
|
||||
limit: Maximum number of results
|
||||
|
||||
Returns:
|
||||
SearchTorrentsResponse with torrent information or error
|
||||
"""
|
||||
try:
|
||||
# Search for torrents
|
||||
results = self.knaben_client.search(media_title, limit=limit)
|
||||
|
||||
if not results:
|
||||
logger.info(f"No torrents found for '{media_title}'")
|
||||
return SearchTorrentsResponse(
|
||||
status="error",
|
||||
error="not_found",
|
||||
message=f"No torrents found for '{media_title}'"
|
||||
)
|
||||
|
||||
# Convert to dict format
|
||||
torrents = []
|
||||
for torrent in results:
|
||||
torrents.append({
|
||||
"name": torrent.title,
|
||||
"size": torrent.size,
|
||||
"seeders": torrent.seeders,
|
||||
"leechers": torrent.leechers,
|
||||
"magnet": torrent.magnet,
|
||||
"info_hash": torrent.info_hash,
|
||||
"tracker": torrent.tracker,
|
||||
"upload_date": torrent.upload_date,
|
||||
"category": torrent.category
|
||||
})
|
||||
|
||||
logger.info(f"Found {len(torrents)} torrents for '{media_title}'")
|
||||
|
||||
return SearchTorrentsResponse(
|
||||
status="ok",
|
||||
torrents=torrents,
|
||||
count=len(torrents)
|
||||
)
|
||||
|
||||
except KnabenNotFoundError as e:
|
||||
logger.info(f"Torrents not found: {e}")
|
||||
return SearchTorrentsResponse(
|
||||
status="error",
|
||||
error="not_found",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except KnabenAPIError as e:
|
||||
logger.error(f"Knaben API error: {e}")
|
||||
return SearchTorrentsResponse(
|
||||
status="error",
|
||||
error="api_error",
|
||||
message=str(e)
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {e}")
|
||||
return SearchTorrentsResponse(
|
||||
status="error",
|
||||
error="validation_failed",
|
||||
message=str(e)
|
||||
)
|
||||
Reference in New Issue
Block a user