fix: update to not add empty dirs
This commit is contained in:
+91
-1
@@ -205,7 +205,7 @@ class NFOGuardDatabase:
|
||||
# Debug: Check what was actually saved
|
||||
cursor.execute("SELECT dateadded, source FROM movies WHERE imdb_id = %s", (imdb_id,))
|
||||
result = cursor.fetchone()
|
||||
print(f"🔍 DATABASE VERIFY: After upsert, found dateadded={result[0] if result else 'NOT_FOUND'}, source={result[1] if result else 'NOT_FOUND'}")
|
||||
print(f"🔍 DATABASE VERIFY: After upsert, found dateadded={result['dateadded'] if result else 'NOT_FOUND'}, source={result['source'] if result else 'NOT_FOUND'}")
|
||||
|
||||
def get_series_episodes(self, imdb_id: str, has_video_file_only: bool = False) -> List[Dict]:
|
||||
"""Get all episodes for a series"""
|
||||
@@ -411,6 +411,96 @@ class NFOGuardDatabase:
|
||||
|
||||
return deleted_episodes
|
||||
|
||||
def delete_movie(self, imdb_id: str) -> bool:
|
||||
"""
|
||||
Delete a specific movie from the database
|
||||
|
||||
Args:
|
||||
imdb_id: Movie IMDb ID
|
||||
|
||||
Returns:
|
||||
True if movie was deleted, False if not found
|
||||
"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
DELETE FROM movies
|
||||
WHERE imdb_id = %s
|
||||
""", (imdb_id,))
|
||||
|
||||
deleted_count = cursor.rowcount
|
||||
conn.commit()
|
||||
|
||||
return deleted_count > 0
|
||||
|
||||
def delete_orphaned_movies(self) -> List[Dict]:
|
||||
"""
|
||||
Find and delete movies that don't have corresponding video files on disk
|
||||
This requires checking filesystem for each movie, so use carefully
|
||||
|
||||
Returns:
|
||||
List of deleted movies with their details
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
deleted_movies = []
|
||||
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Get all movies with their paths
|
||||
cursor.execute("""
|
||||
SELECT imdb_id, path, dateadded, source
|
||||
FROM movies
|
||||
""")
|
||||
|
||||
movies_list = cursor.fetchall()
|
||||
|
||||
for movie in movies_list:
|
||||
imdb_id = movie['imdb_id']
|
||||
movie_path = Path(movie['path'])
|
||||
|
||||
if not movie_path.exists():
|
||||
# Movie directory doesn't exist - delete it
|
||||
cursor.execute("""
|
||||
DELETE FROM movies
|
||||
WHERE imdb_id = %s
|
||||
""", (imdb_id,))
|
||||
|
||||
deleted_movies.append({
|
||||
'imdb_id': imdb_id,
|
||||
'reason': 'directory_not_found',
|
||||
'path': str(movie_path),
|
||||
'dateadded': movie['dateadded'],
|
||||
'source': movie['source']
|
||||
})
|
||||
continue
|
||||
|
||||
# Check for video files
|
||||
video_exts = (".mkv", ".mp4", ".avi", ".mov", ".m4v")
|
||||
has_video = any(f.is_file() and f.suffix.lower() in video_exts
|
||||
for f in movie_path.iterdir() if f.is_file())
|
||||
|
||||
if not has_video:
|
||||
# No video files found - delete this movie
|
||||
cursor.execute("""
|
||||
DELETE FROM movies
|
||||
WHERE imdb_id = %s
|
||||
""", (imdb_id,))
|
||||
|
||||
deleted_movies.append({
|
||||
'imdb_id': imdb_id,
|
||||
'reason': 'no_video_files',
|
||||
'path': str(movie_path),
|
||||
'dateadded': movie['dateadded'],
|
||||
'source': movie['source']
|
||||
})
|
||||
|
||||
conn.commit()
|
||||
|
||||
return deleted_movies
|
||||
|
||||
def close(self):
|
||||
"""Close all database connections"""
|
||||
if hasattr(self._local, 'connection'):
|
||||
|
||||
Reference in New Issue
Block a user