From 5938077d82a5357f79c07b218b3986557a5816e8 Mon Sep 17 00:00:00 2001 From: sephrat <34862846+sephrat@users.noreply.github.com> Date: Tue, 26 Apr 2022 17:33:33 +0200 Subject: [PATCH 1/4] feat(sync): Detect reidentified movies in Emby and Jellyfin --- .../Jobs/Emby/EmbyContentSync.cs | 43 ++++++++++++------ .../Jobs/Jellyfin/JellyfinContentSync.cs | 44 +++++++++++++------ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index b4d61f93d..8d93c804c 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -265,23 +265,22 @@ namespace Ombi.Schedule.Jobs.Emby return; } _logger.LogDebug($"Adding new movie {movieInfo.Name}"); - - content.Add(new EmbyContent - { - ImdbId = movieInfo.ProviderIds.Imdb, - TheMovieDbId = movieInfo.ProviderIds?.Tmdb, - Title = movieInfo.Name, - Type = MediaType.Movie, - EmbyId = movieInfo.Id, - Url = EmbyHelper.GetEmbyMediaUrl(movieInfo.Id, server?.ServerId, server.ServerHostname), - AddedAt = DateTime.UtcNow, - Quality = has4K ? null : quality, - Has4K = has4K - }); + var newMovie = new EmbyContent(); + newMovie.AddedAt = DateTime.UtcNow; + MapEmbyContent(newMovie, movieInfo, server, has4K, quality); + content.Add(newMovie); } else { - if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) + var movieHasChanged = false; + if(existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) + { + _logger.LogDebug($"Updating existing movie '{movieInfo.Name}'"); + MapEmbyContent(existingMovie, movieInfo, server, has4K, quality); + movieHasChanged = true; + + } + else if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) { _logger.LogDebug($"We have found another quality for Movie '{movieInfo.Name}', Quality: '{quality}'"); existingMovie.Quality = has4K ? null : quality; @@ -290,6 +289,11 @@ namespace Ombi.Schedule.Jobs.Emby // Probably could refactor here // If a 4k movie comes in (we don't store the quality on 4k) // it will always get updated even know it's not changed + movieHasChanged = true; + } + + if(movieHasChanged) + { toUpdate.Add(existingMovie); } else @@ -300,6 +304,17 @@ namespace Ombi.Schedule.Jobs.Emby } } + private void MapEmbyContent(EmbyContent content, EmbyMovie movieInfo, EmbyServers server, bool has4K, string quality){ + content.ImdbId = movieInfo.ProviderIds.Imdb; + content.TheMovieDbId = movieInfo.ProviderIds?.Tmdb; + content.Title = movieInfo.Name; + content.Type = MediaType.Movie; + content.EmbyId = movieInfo.Id; + content.Url = EmbyHelper.GetEmbyMediaUrl(movieInfo.Id, server?.ServerId, server.ServerHostname); + content.Quality = has4K ? null : quality; + content.Has4K = has4K; + } + private bool ValidateSettings(EmbyServers server) { if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) diff --git a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs index 060c974e7..eb937ecf0 100644 --- a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs @@ -230,22 +230,22 @@ namespace Ombi.Schedule.Jobs.Jellyfin return; } _logger.LogDebug($"Adding new movie {movieInfo.Name}"); - content.Add(new JellyfinContent - { - ImdbId = movieInfo.ProviderIds.Imdb, - TheMovieDbId = movieInfo.ProviderIds?.Tmdb, - Title = movieInfo.Name, - Type = MediaType.Movie, - JellyfinId = movieInfo.Id, - Url = JellyfinHelper.GetJellyfinMediaUrl(movieInfo.Id, server?.ServerId, server.ServerHostname), - AddedAt = DateTime.UtcNow, - Quality = has4K ? null : quality, - Has4K = has4K - }); + var newMovie = new JellyfinContent(); + newMovie.AddedAt = DateTime.UtcNow; + MapJellyfinMovie(newMovie, movieInfo, server, has4K, quality); + content.Add(newMovie);; } else { - if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) + var movieHasChanged = false; + if(existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) + { + _logger.LogDebug($"Updating existing movie '{movieInfo.Name}'"); + MapJellyfinMovie(existingMovie, movieInfo, server, has4K, quality); + movieHasChanged = true; + + } + else if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) { _logger.LogDebug($"We have found another quality for Movie '{movieInfo.Name}', Quality: '{quality}'"); existingMovie.Quality = has4K ? null : quality; @@ -255,6 +255,12 @@ namespace Ombi.Schedule.Jobs.Jellyfin // If a 4k movie comes in (we don't store the quality on 4k) // it will always get updated even know it's not changed toUpdate.Add(existingMovie); + movieHasChanged = true; + } + + if(movieHasChanged) + { + toUpdate.Add(existingMovie); } else { @@ -264,6 +270,18 @@ namespace Ombi.Schedule.Jobs.Jellyfin } } + private void MapJellyfinMovie(JellyfinContent content, JellyfinMovie movieInfo, JellyfinServers server, bool has4K, string quality) + { + content.ImdbId = movieInfo.ProviderIds.Imdb; + content.TheMovieDbId = movieInfo.ProviderIds?.Tmdb; + content.Title = movieInfo.Name; + content.Type = MediaType.Movie; + content.JellyfinId = movieInfo.Id; + content.Url = JellyfinHelper.GetJellyfinMediaUrl(movieInfo.Id, server?.ServerId, server.ServerHostname); + content.Quality = has4K ? null : quality; + content.Has4K = has4K; + } + private bool ValidateSettings(JellyfinServers server) { if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) From 9096e91d55d268819bce22831f8a8b27f2a1776b Mon Sep 17 00:00:00 2001 From: sephrat <34862846+sephrat@users.noreply.github.com> Date: Tue, 26 Apr 2022 18:17:07 +0200 Subject: [PATCH 2/4] feat(sync): Detect reidentified series in Emby and Jellyfin --- src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs | 11 +++++++++++ .../Jobs/Jellyfin/JellyfinContentSync.cs | 11 +++++++++++ src/Ombi.Store/Repository/EmbyContentRepository.cs | 7 +++++++ .../Repository/IMediaServerContentRepository.cs | 1 + .../Repository/JellyfinContentRepository.cs | 7 +++++++ src/Ombi.Store/Repository/MediaServerRepository.cs | 1 + src/Ombi.Store/Repository/PlexContentRepository.cs | 7 +++++++ 7 files changed, 45 insertions(+) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index 8d93c804c..c0acba55c 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -157,6 +157,17 @@ namespace Ombi.Schedule.Jobs.Emby } var existingTv = await _repo.GetByEmbyId(tvShow.Id); + + if (existingTv != null && + ( existingTv.ImdbId != tvShow.ProviderIds?.Imdb + || existingTv.TheMovieDbId != tvShow.ProviderIds?.Tmdb + || existingTv.TvDbId != tvShow.ProviderIds?.Tvdb)) + { + _logger.LogCritical($"Series '{tvShow.Name}' has different IDs, probably a reidentification."); + await _repo.DeleteTv(existingTv); + existingTv = null; + } + if (existingTv == null) { _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); diff --git a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs index eb937ecf0..cdbb88661 100644 --- a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs @@ -132,6 +132,17 @@ namespace Ombi.Schedule.Jobs.Jellyfin } var existingTv = await _repo.GetByJellyfinId(tvShow.Id); + + if (existingTv != null && + ( existingTv.ImdbId != tvShow.ProviderIds?.Imdb + || existingTv.TheMovieDbId != tvShow.ProviderIds?.Tmdb + || existingTv.TvDbId != tvShow.ProviderIds?.Tvdb)) + { + _logger.LogDebug($"Series '{tvShow.Name}' has different IDs, probably a reidentification."); + await _repo.DeleteTv(existingTv); + existingTv = null; + } + if (existingTv == null) { _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); diff --git a/src/Ombi.Store/Repository/EmbyContentRepository.cs b/src/Ombi.Store/Repository/EmbyContentRepository.cs index df88d8292..8f9904ebb 100644 --- a/src/Ombi.Store/Repository/EmbyContentRepository.cs +++ b/src/Ombi.Store/Repository/EmbyContentRepository.cs @@ -102,6 +102,13 @@ namespace Ombi.Store.Repository return InternalSaveChanges(); } + public override async Task DeleteTv(EmbyContent tv) + { + var episodesToDelete = GetAllEpisodes().Cast().Where(x => x.ParentId == tv.EmbyId).ToList(); + Db.EmbyEpisode.RemoveRange(episodesToDelete); + await Delete(tv); + } + public override RecentlyAddedType RecentlyAddedType => RecentlyAddedType.Emby; } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/IMediaServerContentRepository.cs b/src/Ombi.Store/Repository/IMediaServerContentRepository.cs index db4835a16..3f165b7e6 100644 --- a/src/Ombi.Store/Repository/IMediaServerContentRepository.cs +++ b/src/Ombi.Store/Repository/IMediaServerContentRepository.cs @@ -14,6 +14,7 @@ namespace Ombi.Store.Repository IQueryable GetAllEpisodes(); Task Add(IMediaServerEpisode content); Task AddRange(IEnumerable content); + Task DeleteTv(Content tv); void UpdateWithoutSave(IMediaServerContent existingContent); } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/JellyfinContentRepository.cs b/src/Ombi.Store/Repository/JellyfinContentRepository.cs index c3451da62..8dc729a6b 100644 --- a/src/Ombi.Store/Repository/JellyfinContentRepository.cs +++ b/src/Ombi.Store/Repository/JellyfinContentRepository.cs @@ -104,6 +104,13 @@ namespace Ombi.Store.Repository return InternalSaveChanges(); } + public override async Task DeleteTv(JellyfinContent tv) + { + var episodesToDelete = GetAllEpisodes().Cast().Where(x => x.ParentId == tv.JellyfinId).ToList(); + Db.JellyfinEpisode.RemoveRange(episodesToDelete); + await Delete(tv); + } + public override RecentlyAddedType RecentlyAddedType => RecentlyAddedType.Jellyfin; } } diff --git a/src/Ombi.Store/Repository/MediaServerRepository.cs b/src/Ombi.Store/Repository/MediaServerRepository.cs index c1c7d4ec8..251fe3cea 100644 --- a/src/Ombi.Store/Repository/MediaServerRepository.cs +++ b/src/Ombi.Store/Repository/MediaServerRepository.cs @@ -22,5 +22,6 @@ namespace Ombi.Store.Repository public abstract Task AddRange(IEnumerable content); public abstract void UpdateWithoutSave(IMediaServerContent existingContent); public abstract Task UpdateRange(IEnumerable existingContent); + public abstract Task DeleteTv(T tv); } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/PlexContentRepository.cs b/src/Ombi.Store/Repository/PlexContentRepository.cs index 96dfbe8e1..9f34af6a1 100644 --- a/src/Ombi.Store/Repository/PlexContentRepository.cs +++ b/src/Ombi.Store/Repository/PlexContentRepository.cs @@ -169,5 +169,12 @@ namespace Ombi.Store.Repository Db.PlexServerContent.UpdateRange((IEnumerable)existingContent); return InternalSaveChanges(); } + + public override Task DeleteTv(PlexServerContent tv) + { + // not used for now + // TODO: delete episodes, then delete series + throw new NotImplementedException(); + } } } \ No newline at end of file From ae25e41b1b01dcc949e1cdc4f7e9a69040e587d4 Mon Sep 17 00:00:00 2001 From: sephrat <34862846+sephrat@users.noreply.github.com> Date: Tue, 26 Apr 2022 18:51:06 +0200 Subject: [PATCH 3/4] Fix sync log criticity --- src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index c0acba55c..46cd7cf76 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -163,7 +163,7 @@ namespace Ombi.Schedule.Jobs.Emby || existingTv.TheMovieDbId != tvShow.ProviderIds?.Tmdb || existingTv.TvDbId != tvShow.ProviderIds?.Tvdb)) { - _logger.LogCritical($"Series '{tvShow.Name}' has different IDs, probably a reidentification."); + _logger.LogDebug($"Series '{tvShow.Name}' has different IDs, probably a reidentification."); await _repo.DeleteTv(existingTv); existingTv = null; } From a96d2a7818b50d8dd6ec999f672400070123ec7c Mon Sep 17 00:00:00 2001 From: sephrat <34862846+sephrat@users.noreply.github.com> Date: Wed, 27 Apr 2022 08:15:25 +0200 Subject: [PATCH 4/4] Fix formatting --- src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs | 7 +++---- src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index 46cd7cf76..27cdf0f3a 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -170,7 +170,7 @@ namespace Ombi.Schedule.Jobs.Emby if (existingTv == null) { - _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); + _logger.LogDebug("Adding TV Show {0}", tvShow.Name); mediaToAdd.Add(new EmbyContent { TvDbId = tvShow.ProviderIds?.Tvdb, @@ -284,12 +284,11 @@ namespace Ombi.Schedule.Jobs.Emby else { var movieHasChanged = false; - if(existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) + if (existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) { _logger.LogDebug($"Updating existing movie '{movieInfo.Name}'"); MapEmbyContent(existingMovie, movieInfo, server, has4K, quality); movieHasChanged = true; - } else if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) { @@ -303,7 +302,7 @@ namespace Ombi.Schedule.Jobs.Emby movieHasChanged = true; } - if(movieHasChanged) + if (movieHasChanged) { toUpdate.Add(existingMovie); } diff --git a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs index cdbb88661..5519b3413 100644 --- a/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Jellyfin/JellyfinContentSync.cs @@ -145,7 +145,7 @@ namespace Ombi.Schedule.Jobs.Jellyfin if (existingTv == null) { - _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); + _logger.LogDebug("Adding TV Show {0}", tvShow.Name); mediaToAdd.Add(new JellyfinContent { TvDbId = tvShow.ProviderIds?.Tvdb, @@ -249,12 +249,11 @@ namespace Ombi.Schedule.Jobs.Jellyfin else { var movieHasChanged = false; - if(existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) + if (existingMovie.ImdbId != movieInfo.ProviderIds.Imdb || existingMovie.TheMovieDbId != movieInfo.ProviderIds.Tmdb) { _logger.LogDebug($"Updating existing movie '{movieInfo.Name}'"); MapJellyfinMovie(existingMovie, movieInfo, server, has4K, quality); movieHasChanged = true; - } else if (!quality.Equals(existingMovie?.Quality, StringComparison.InvariantCultureIgnoreCase)) { @@ -269,7 +268,7 @@ namespace Ombi.Schedule.Jobs.Jellyfin movieHasChanged = true; } - if(movieHasChanged) + if (movieHasChanged) { toUpdate.Add(existingMovie); }