mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
snyc...
This commit is contained in:
@@ -56,7 +56,9 @@ class JellyfinSyncService extends BaseSyncService
|
||||
try {
|
||||
$this->logProgress('Fetching movies from Jellyfin...');
|
||||
$movies = $this->getJellyfinItems('Movie');
|
||||
$this->logProgress("Found " . count($movies) . " movies in Jellyfin");
|
||||
$movieCount = count($movies);
|
||||
$this->setTotalItems($movieCount);
|
||||
$this->logProgress("Found {$movieCount} movies in Jellyfin");
|
||||
|
||||
if (empty($movies)) {
|
||||
$this->logProgress('No movies found in Jellyfin library');
|
||||
@@ -118,7 +120,9 @@ class JellyfinSyncService extends BaseSyncService
|
||||
$this->logProgress('=== Starting TV Shows Sync ===');
|
||||
$this->logProgress('Fetching TV shows from Jellyfin...');
|
||||
$tvShows = $this->getJellyfinItems('Series');
|
||||
$this->logProgress("Found " . count($tvShows) . " TV shows in Jellyfin");
|
||||
$tvShowCount = count($tvShows);
|
||||
$this->setTotalItems($tvShowCount);
|
||||
$this->logProgress("Found {$tvShowCount} TV shows in Jellyfin");
|
||||
|
||||
if (empty($tvShows)) {
|
||||
$this->logProgress('No TV shows found in Jellyfin library');
|
||||
@@ -131,7 +135,8 @@ class JellyfinSyncService extends BaseSyncService
|
||||
|
||||
foreach ($tvShows as $showData) {
|
||||
$processedShows++;
|
||||
$this->logProgress("Processing TV show {$processedShows}/" . count($tvShows) . ": {$showData['Name']} (ID: {$showData['Id']})");
|
||||
$this->processedCount++; // Increment processed count for each TV show
|
||||
$this->logProgress("Processing TV show {$processedShows}/{$tvShowCount}: {$showData['Name']} (ID: {$showData['Id']})");
|
||||
|
||||
try {
|
||||
$this->syncTvShow($showData);
|
||||
@@ -858,8 +863,196 @@ class JellyfinSyncService extends BaseSyncService
|
||||
return $this->updatedCount;
|
||||
}
|
||||
|
||||
protected function getDeletedCount(): int
|
||||
protected function executeCleanup(): void
|
||||
{
|
||||
return 0; // Jellyfin doesn't provide deletion info in this context
|
||||
$this->logProgress("Starting cleanup - detecting deleted media in Jellyfin...");
|
||||
|
||||
// Clean up movies
|
||||
$this->cleanupMovies();
|
||||
|
||||
// Clean up TV shows and episodes
|
||||
$this->cleanupTvShows();
|
||||
|
||||
$this->logProgress("Cleanup completed. Deleted {$this->deletedCount} items.");
|
||||
}
|
||||
|
||||
private function cleanupMovies(): void
|
||||
{
|
||||
$this->logProgress("Checking for deleted movies...");
|
||||
|
||||
try {
|
||||
// Get all movies from Jellyfin
|
||||
$jellyfinMovies = $this->getJellyfinItems('Movie');
|
||||
$jellyfinMovieIds = array_column($jellyfinMovies, 'Id');
|
||||
$this->logProgress("Found " . count($jellyfinMovieIds) . " movies in Jellyfin");
|
||||
|
||||
// Get all movies from local database for this source
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT id, metadata FROM movies WHERE source_id = :source_id
|
||||
");
|
||||
$stmt->execute(['source_id' => $this->source['id']]);
|
||||
$localMovies = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$this->logProgress("Found " . count($localMovies) . " movies in local database");
|
||||
|
||||
$deletedCount = 0;
|
||||
foreach ($localMovies as $localMovie) {
|
||||
$metadata = json_decode($localMovie['metadata'], true);
|
||||
$jellyfinId = $metadata['jellyfin_id'] ?? null;
|
||||
|
||||
if ($jellyfinId && !in_array($jellyfinId, $jellyfinMovieIds)) {
|
||||
// Movie exists in local DB but not in Jellyfin - delete it
|
||||
$this->deleteMovie($localMovie['id']);
|
||||
$deletedCount++;
|
||||
$this->deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->logProgress("Deleted {$deletedCount} movies that no longer exist in Jellyfin");
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error during movie cleanup: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanupTvShows(): void
|
||||
{
|
||||
$this->logProgress("Checking for deleted TV shows and episodes...");
|
||||
|
||||
try {
|
||||
// Get all TV shows from Jellyfin
|
||||
$jellyfinShows = $this->getJellyfinItems('Series');
|
||||
$jellyfinShowIds = array_column($jellyfinShows, 'Id');
|
||||
$this->logProgress("Found " . count($jellyfinShowIds) . " TV shows in Jellyfin");
|
||||
|
||||
// Get all TV shows from local database for this source
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT id, metadata FROM tv_shows WHERE source_id = :source_id
|
||||
");
|
||||
$stmt->execute(['source_id' => $this->source['id']]);
|
||||
$localShows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$this->logProgress("Found " . count($localShows) . " TV shows in local database");
|
||||
|
||||
// Check for deleted shows
|
||||
$deletedShows = 0;
|
||||
foreach ($localShows as $localShow) {
|
||||
$metadata = json_decode($localShow['metadata'], true);
|
||||
$jellyfinId = $metadata['jellyfin_id'] ?? null;
|
||||
|
||||
if ($jellyfinId && !in_array($jellyfinId, $jellyfinShowIds)) {
|
||||
// Show exists in local DB but not in Jellyfin - delete it
|
||||
$this->deleteTvShow($localShow['id']);
|
||||
$deletedShows++;
|
||||
$this->deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->logProgress("Deleted {$deletedShows} TV shows that no longer exist in Jellyfin");
|
||||
|
||||
// Also clean up episodes that might be orphaned (show deleted but episodes remain)
|
||||
$this->cleanupOrphanedEpisodes();
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error during TV show cleanup: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanupOrphanedEpisodes(): void
|
||||
{
|
||||
$this->logProgress("Checking for orphaned episodes...");
|
||||
|
||||
try {
|
||||
// Get all episode IDs that belong to shows from this source
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT te.id, te.metadata, ts.metadata as show_metadata
|
||||
FROM tv_episodes te
|
||||
JOIN tv_shows ts ON te.tv_show_id = ts.id
|
||||
WHERE ts.source_id = :source_id
|
||||
");
|
||||
$stmt->execute(['source_id' => $this->source['id']]);
|
||||
$episodes = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$deletedEpisodes = 0;
|
||||
foreach ($episodes as $episode) {
|
||||
$episodeMetadata = json_decode($episode['metadata'], true);
|
||||
$showMetadata = json_decode($episode['show_metadata'], true);
|
||||
|
||||
$episodeJellyfinId = $episodeMetadata['jellyfin_id'] ?? null;
|
||||
$showJellyfinId = $showMetadata['jellyfin_id'] ?? null;
|
||||
|
||||
// If either the episode or its parent show doesn't exist in Jellyfin, delete the episode
|
||||
if (!$episodeJellyfinId || !$showJellyfinId) {
|
||||
$this->deleteTvEpisode($episode['id']);
|
||||
$deletedEpisodes++;
|
||||
$this->deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($deletedEpisodes > 0) {
|
||||
$this->logProgress("Deleted {$deletedEpisodes} orphaned episodes");
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error during orphaned episode cleanup: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteMovie(int $movieId): void
|
||||
{
|
||||
try {
|
||||
// Delete actor relationships first
|
||||
$stmt = $this->pdo->prepare("DELETE FROM actor_movie WHERE movie_id = :movie_id");
|
||||
$stmt->execute(['movie_id' => $movieId]);
|
||||
|
||||
// Delete the movie
|
||||
$stmt = $this->pdo->prepare("DELETE FROM movies WHERE id = :id");
|
||||
$stmt->execute(['id' => $movieId]);
|
||||
|
||||
$this->logProgress("Deleted movie with ID: {$movieId}");
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error deleting movie {$movieId}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteTvShow(int $showId): void
|
||||
{
|
||||
try {
|
||||
// Delete actor relationships first
|
||||
$stmt = $this->pdo->prepare("DELETE FROM actor_tv_show WHERE tv_show_id = :tv_show_id");
|
||||
$stmt->execute(['tv_show_id' => $showId]);
|
||||
|
||||
// Delete episodes (which will also delete episode-actor relationships via CASCADE)
|
||||
$stmt = $this->pdo->prepare("DELETE FROM tv_episodes WHERE tv_show_id = :tv_show_id");
|
||||
$stmt->execute(['tv_show_id' => $showId]);
|
||||
|
||||
// Delete the show
|
||||
$stmt = $this->pdo->prepare("DELETE FROM tv_shows WHERE id = :id");
|
||||
$stmt->execute(['id' => $showId]);
|
||||
|
||||
$this->logProgress("Deleted TV show with ID: {$showId}");
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error deleting TV show {$showId}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteTvEpisode(int $episodeId): void
|
||||
{
|
||||
try {
|
||||
// Delete actor relationships first
|
||||
$stmt = $this->pdo->prepare("DELETE FROM actor_tv_episode WHERE tv_episode_id = :tv_episode_id");
|
||||
$stmt->execute(['tv_episode_id' => $episodeId]);
|
||||
|
||||
// Delete the episode
|
||||
$stmt = $this->pdo->prepare("DELETE FROM tv_episodes WHERE id = :id");
|
||||
$stmt->execute(['id' => $episodeId]);
|
||||
|
||||
$this->logProgress("Deleted TV episode with ID: {$episodeId}");
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logProgress("Error deleting TV episode {$episodeId}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user