c4-rdarr db updates
This commit is contained in:
+44
-10
@@ -26,6 +26,12 @@ except ImportError:
|
||||
|
||||
path_mapper = DummyPathMapper()
|
||||
|
||||
# Import database client for enhanced performance
|
||||
try:
|
||||
from clients.radarr_db_client import RadarrDbClient
|
||||
except ImportError:
|
||||
RadarrDbClient = None
|
||||
|
||||
|
||||
class RadarrClient:
|
||||
"""Enhanced Radarr API client with improved import date detection"""
|
||||
@@ -54,6 +60,17 @@ class RadarrClient:
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
self.retries = max(0, retries)
|
||||
|
||||
# Initialize database client if available
|
||||
self.db_client = None
|
||||
if RadarrDbClient:
|
||||
try:
|
||||
self.db_client = RadarrDbClient.from_env()
|
||||
if self.db_client:
|
||||
_log("INFO", "Enhanced performance mode: Direct database access enabled")
|
||||
except Exception as e:
|
||||
_log("WARNING", f"Failed to initialize database client, using API fallback: {e}")
|
||||
self.db_client = None
|
||||
|
||||
def _get(self, path: str, params: Dict[str, Any] = None) -> Optional[Any]:
|
||||
"""Make GET request to Radarr API with retries"""
|
||||
@@ -94,10 +111,21 @@ class RadarrClient:
|
||||
imdb_id = imdb_id if imdb_id.startswith("tt") else f"tt{imdb_id}"
|
||||
_log("DEBUG", f"Looking up movie by IMDb ID: {imdb_id}")
|
||||
|
||||
# Try database first if available
|
||||
if self.db_client:
|
||||
try:
|
||||
movie = self.db_client.get_movie_by_imdb(imdb_id)
|
||||
if movie:
|
||||
_log("INFO", f"Found via database: {movie.get('title')} (ID: {movie.get('id')})")
|
||||
return movie
|
||||
except Exception as e:
|
||||
_log("WARNING", f"Database lookup failed, using API fallback: {e}")
|
||||
|
||||
# API Fallback methods
|
||||
# Method 1: Direct lookup by ID
|
||||
movie = self._get(f"/api/v3/movie/lookup/imdb/{imdb_id}")
|
||||
if isinstance(movie, dict) and movie.get("imdbId", "").lower() == imdb_id.lower():
|
||||
_log("INFO", f"Found via direct lookup: {movie.get('title')} (ID: {movie.get('id')})")
|
||||
_log("INFO", f"Found via API direct lookup: {movie.get('title')} (ID: {movie.get('id')})")
|
||||
return movie
|
||||
|
||||
# Method 2: Get all movies and filter by IMDb (fallback)
|
||||
@@ -110,7 +138,7 @@ class RadarrClient:
|
||||
_log("INFO", f"Found exact match: {movie.get('title')} (ID: {movie.get('id')})")
|
||||
return movie
|
||||
|
||||
# Method 2: Direct API search with validation
|
||||
# Method 3: Direct API search with validation
|
||||
result = self._get("/api/v3/movie", {"imdbId": imdb_id})
|
||||
if isinstance(result, list) and result:
|
||||
for movie in result:
|
||||
@@ -119,7 +147,7 @@ class RadarrClient:
|
||||
_log("INFO", f"Found via search: {movie.get('title')} (ID: {movie.get('id')})")
|
||||
return movie
|
||||
|
||||
# Method 3: Lookup endpoint
|
||||
# Method 4: Lookup endpoint
|
||||
lookup_result = self._get("/api/v3/movie/lookup/imdb", {"imdbId": imdb_id})
|
||||
if isinstance(lookup_result, dict) and lookup_result.get("imdbId", "").lower() == imdb_id.lower():
|
||||
_log("INFO", f"Found via lookup: {lookup_result.get('title')} (ID: {lookup_result.get('id')})")
|
||||
@@ -442,25 +470,31 @@ class RadarrClient:
|
||||
|
||||
def get_movie_import_date(self, movie_id: int, fallback_to_file_date: bool = True) -> Tuple[Optional[str], str]:
|
||||
"""
|
||||
Get the best import date for a movie.
|
||||
Get the best import date for a movie with database optimization.
|
||||
|
||||
Returns:
|
||||
(date_iso, source_description)
|
||||
"""
|
||||
# Try history first - we want the earliest event regardless of type
|
||||
import_date = self.earliest_import_event_optimized(movie_id)
|
||||
|
||||
# Try history second
|
||||
# Try database first if available (much faster)
|
||||
if self.db_client:
|
||||
try:
|
||||
date_iso, source = self.db_client.get_movie_import_date_optimized(movie_id, fallback_to_file_date)
|
||||
if date_iso:
|
||||
return date_iso, source
|
||||
except Exception as e:
|
||||
_log("WARNING", f"Database import date query failed, using API fallback: {e}")
|
||||
|
||||
# API Fallback: Try history parsing
|
||||
import_date = self.earliest_import_event_optimized(movie_id)
|
||||
if import_date:
|
||||
return import_date, "radarr:history.import"
|
||||
return import_date, "radarr:api.history.import"
|
||||
|
||||
# Fallback to file dateAdded if requested
|
||||
if fallback_to_file_date:
|
||||
file_date = self.earliest_file_dateadded(movie_id)
|
||||
if file_date:
|
||||
_log("WARNING", f"Using file dateAdded as fallback for movie_id {movie_id}")
|
||||
return file_date, "radarr:file.dateAdded"
|
||||
return file_date, "radarr:api.file.dateAdded"
|
||||
|
||||
return None, "radarr:no_date_found"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user