From acac2c36759b46ba6b17ecb3e4d2e93d29e6dd17 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 21 Feb 2018 13:53:52 +0000 Subject: [PATCH] Try and fuzzy match the title and release if we cannot get the tvdb id or imdbid (depends on the media agents in Plex) #1951 Also fixed #1995 --- src/Ombi.Api.Sonarr/SonarrApi.cs | 6 ++-- .../Jobs/Plex/PlexAvailabilityChecker.cs | 10 ++++++ .../Jobs/Plex/PlexContentSync.cs | 27 +++++++++++++--- .../Jobs/Plex/PlexEpisodeSync.cs | 31 ++++++++++++++++++- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/Ombi.Api.Sonarr/SonarrApi.cs b/src/Ombi.Api.Sonarr/SonarrApi.cs index 8753203a2..7fd74d2a3 100644 --- a/src/Ombi.Api.Sonarr/SonarrApi.cs +++ b/src/Ombi.Api.Sonarr/SonarrApi.cs @@ -66,8 +66,10 @@ namespace Ombi.Api.Sonarr var request = new Request($"/api/series/{id}", baseUrl, HttpMethod.Get); request.AddHeader("X-Api-Key", apiKey); var result = await Api.Request(request); - result.seasons.ToList().RemoveAt(0); - + if (result?.seasons?.Length > 0) + { + result?.seasons?.ToList().RemoveAt(0); + } return result; } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs index c6b876c21..343c4256e 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs @@ -67,6 +67,16 @@ namespace Ombi.Schedule.Jobs.Plex { seriesEpisodes = plexEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString()); } + + if (!seriesEpisodes.Any()) + { + // Let's try and match the series by name + seriesEpisodes = plexEpisodes.Where(x => + x.Series.Title.Equals(child.Title, StringComparison.CurrentCultureIgnoreCase) && + x.Series.ReleaseYear == child.ParentRequest.ReleaseDate.Year.ToString()); + + } + foreach (var season in child.SeasonRequests) { foreach (var episode in season.Episodes) diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs index 3eda1cdad..3c3404bd7 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs @@ -127,13 +127,32 @@ namespace Ombi.Schedule.Jobs.Plex && x.ReleaseYear == show.year.ToString() && x.Type == PlexMediaTypeEntity.Show); - if (existingContent == null) + if (existingContent != null) { // Just check the key - var hasSameKey = await Repo.GetByKey(show.ratingKey); - if (hasSameKey != null) + var existingKey = await Repo.GetByKey(show.ratingKey); + if (existingKey != null) + { + // The rating key is all good! + existingContent = existingKey; + } + else { - existingContent = hasSameKey; + // This means the rating key has changed somehow. + // We need to reset the correct keys + var oldKey = existingContent.Key; + existingContent.Key = show.ratingKey; + + // Because we have changed the rating key, we need to change all children too + var episodeToChange = Repo.GetAllEpisodes().Where(x => x.GrandparentKey == oldKey); + if (episodeToChange.Any()) + { + foreach (var e in episodeToChange) + { + e.GrandparentKey = existingContent.Key; + } + } + await Repo.SaveChangesAsync(); } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs index 308c74489..f9fc3fbf8 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs @@ -48,8 +48,9 @@ namespace Ombi.Schedule.Jobs.Plex foreach (var server in s.Servers) { await Cache(server); - BackgroundJob.Enqueue(() => _availabilityChecker.Start()); } + + BackgroundJob.Enqueue(() => _availabilityChecker.Start()); } catch (Exception e) { @@ -127,7 +128,10 @@ namespace Ombi.Schedule.Jobs.Plex private async Task ProcessEpsiodes(PlexContainer episodes) { var ep = new HashSet(); + try + { + foreach (var episode in episodes?.MediaContainer?.Metadata ?? new Metadata[]{}) { // I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes? @@ -142,6 +146,25 @@ namespace Ombi.Schedule.Jobs.Plex // continue; //} + // Let's check if we have the parent + var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey); + if (seriesExists == null) + { + // Ok let's try and match it to a title. TODO (This is experimental) + var seriesMatch = await _repo.GetAll().FirstOrDefaultAsync(x => + x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase)); + if (seriesMatch == null) + { + _log.LogWarning( + "The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}", + episode.title, episode.grandparentRatingKey, episode.grandparentTitle); + continue; + } + + // Set the rating key to the correct one + episode.grandparentRatingKey = seriesMatch.Key; + } + ep.Add(new PlexEpisode { EpisodeNumber = episode.index, @@ -154,6 +177,12 @@ namespace Ombi.Schedule.Jobs.Plex } await _repo.AddRange(ep); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } } private bool Validate(PlexServers settings)