@@ -167,7 +167,7 @@ class RadarrDbClient:
|
||||
|
||||
def get_all_movies(self) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get all movies from Radarr database
|
||||
Get all movies from Radarr database (only movies with files)
|
||||
|
||||
Returns:
|
||||
List of dictionaries with movie info
|
||||
@@ -182,9 +182,11 @@ class RadarrDbClient:
|
||||
mm."Year" as year,
|
||||
mm."DigitalRelease" as digital_release,
|
||||
mm."PhysicalRelease" as physical_release,
|
||||
mm."InCinemas" as in_cinemas
|
||||
mm."InCinemas" as in_cinemas,
|
||||
(SELECT COUNT(*) FROM "MovieFiles" mf WHERE mf."MovieId" = m."Id") as file_count
|
||||
FROM "Movies" m
|
||||
JOIN "MovieMetadata" mm ON m."MovieMetadataId" = mm."Id"
|
||||
WHERE (SELECT COUNT(*) FROM "MovieFiles" mf WHERE mf."MovieId" = m."Id") > 0
|
||||
ORDER BY mm."Title"
|
||||
"""
|
||||
|
||||
|
||||
+25
-5
@@ -108,6 +108,8 @@ class NFOGuardDatabase:
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS movies (
|
||||
imdb_id VARCHAR(20) PRIMARY KEY,
|
||||
title TEXT,
|
||||
year INTEGER,
|
||||
path TEXT NOT NULL,
|
||||
released DATE,
|
||||
dateadded TIMESTAMP,
|
||||
@@ -117,6 +119,21 @@ class NFOGuardDatabase:
|
||||
)
|
||||
""")
|
||||
|
||||
# Add title and year columns if they don't exist (migration for existing databases)
|
||||
cursor.execute("""
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='movies' AND column_name='title') THEN
|
||||
ALTER TABLE movies ADD COLUMN title TEXT;
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='movies' AND column_name='year') THEN
|
||||
ALTER TABLE movies ADD COLUMN year INTEGER;
|
||||
END IF;
|
||||
END $$;
|
||||
""")
|
||||
|
||||
# Processing history table
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS processing_history (
|
||||
@@ -255,25 +272,28 @@ class NFOGuardDatabase:
|
||||
""", (imdb_id, path, timestamp))
|
||||
|
||||
def upsert_movie_dates(self, imdb_id: str, released: Optional[str],
|
||||
dateadded: Optional[str], source: str, has_video_file: bool = False):
|
||||
dateadded: Optional[str], source: str, has_video_file: bool = False,
|
||||
title: Optional[str] = None, year: Optional[int] = None):
|
||||
"""Insert or update movie date record"""
|
||||
import os
|
||||
if os.environ.get("DEBUG", "false").lower() == "true":
|
||||
print(f"🔍 DATABASE UPSERT: imdb_id={imdb_id}, dateadded={dateadded}, source={source}")
|
||||
print(f"🔍 DATABASE UPSERT: imdb_id={imdb_id}, title={title}, dateadded={dateadded}, source={source}")
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
timestamp = datetime.utcnow()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO movies (imdb_id, path, released, dateadded, source, has_video_file, last_updated)
|
||||
VALUES (%s, COALESCE((SELECT path FROM movies WHERE imdb_id = %s), 'unknown'), %s, %s, %s, %s, %s)
|
||||
INSERT INTO movies (imdb_id, title, year, path, released, dateadded, source, has_video_file, last_updated)
|
||||
VALUES (%s, %s, %s, COALESCE((SELECT path FROM movies WHERE imdb_id = %s), 'unknown'), %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (imdb_id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
year = EXCLUDED.year,
|
||||
released = EXCLUDED.released,
|
||||
dateadded = EXCLUDED.dateadded,
|
||||
source = EXCLUDED.source,
|
||||
has_video_file = EXCLUDED.has_video_file,
|
||||
last_updated = EXCLUDED.last_updated
|
||||
""", (imdb_id, imdb_id, released, dateadded, source, has_video_file, timestamp))
|
||||
""", (imdb_id, title, year, imdb_id, released, dateadded, source, has_video_file, timestamp))
|
||||
|
||||
# Debug: Check what was actually saved
|
||||
cursor.execute("SELECT dateadded, source FROM movies WHERE imdb_id = %s", (imdb_id,))
|
||||
|
||||
@@ -131,10 +131,15 @@ class DatabasePopulator:
|
||||
stats['skipped'] += 1
|
||||
continue
|
||||
|
||||
# Insert into database
|
||||
self.db.upsert_movie_dates(imdb_id, released, dateadded, source, has_video_file=True)
|
||||
# Insert into database with title and year
|
||||
title = movie.get('title')
|
||||
year = movie.get('year')
|
||||
self.db.upsert_movie_dates(
|
||||
imdb_id, released, dateadded, source,
|
||||
has_video_file=True, title=title, year=year
|
||||
)
|
||||
stats['added'] += 1
|
||||
_log("DEBUG", f"Added movie {imdb_id}: {movie.get('title')} (source: {source})")
|
||||
_log("DEBUG", f"Added movie {imdb_id}: {title} ({year}) (source: {source})")
|
||||
|
||||
except Exception as e:
|
||||
_log("ERROR", f"Error processing movie {movie.get('title', 'unknown')}: {e}")
|
||||
|
||||
Reference in New Issue
Block a user