From 636b6c2b027d3e2b0cbdd01a45bf56f4a91e4924 Mon Sep 17 00:00:00 2001 From: Jamie Date: Wed, 1 Aug 2018 21:22:18 +0100 Subject: [PATCH] #2371 Fixed the issue where certain actions would not setup the series correctly in Sonarr --- src/Ombi.Api.Sonarr/Models/Episode.cs | 62 +++++++++++++++++++ src/Ombi.Api.Sonarr/Models/Quality.cs | 10 +++ src/Ombi.Core/Senders/TvSender.cs | 87 ++++++++++++++++++++------- 3 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/Ombi.Api.Sonarr/Models/Episode.cs b/src/Ombi.Api.Sonarr/Models/Episode.cs index c17f5486c..b01e6fd8c 100644 --- a/src/Ombi.Api.Sonarr/Models/Episode.cs +++ b/src/Ombi.Api.Sonarr/Models/Episode.cs @@ -6,6 +6,30 @@ namespace Ombi.Api.Sonarr.Models { public class Episode { + public Episode() + { + + } + + public Episode(Episode ep) + { + seriesId = ep.seriesId; + episodeFileId = ep.episodeFileId; + seasonNumber = ep.seasonNumber; + episodeNumber = ep.episodeNumber; + title = ep.title; + airDate = ep.airDate; + airDateUtc = ep.airDateUtc; + overview = ep.overview; + hasFile = ep.hasFile; + monitored = ep.monitored; + unverifiedSceneNumbering = ep.unverifiedSceneNumbering; + id = ep.id; + absoluteEpisodeNumber = ep.absoluteEpisodeNumber; + sceneAbsoluteEpisodeNumber = ep.sceneAbsoluteEpisodeNumber; + sceneEpisodeNumber = ep.sceneEpisodeNumber; + sceneSeasonNumber = ep.sceneSeasonNumber; + } public int seriesId { get; set; } public int episodeFileId { get; set; } public int seasonNumber { get; set; } @@ -27,6 +51,24 @@ namespace Ombi.Api.Sonarr.Models public class Episodefile { + public Episodefile() + { + + } + + public Episodefile(Episodefile e) + { + seriesId = e.seriesId; + seasonNumber = e.seasonNumber; + relativePath = e.relativePath; + path = e.path; + size = e.size; + dateAdded = e.dateAdded; + sceneName = e.sceneName; + quality = new EpisodeQuality(e.quality); + qualityCutoffNotMet = e.qualityCutoffNotMet; + id = e.id; + } public int seriesId { get; set; } public int seasonNumber { get; set; } public string relativePath { get; set; } @@ -41,12 +83,32 @@ namespace Ombi.Api.Sonarr.Models public class EpisodeQuality { + public EpisodeQuality() + { + + } + + public EpisodeQuality(EpisodeQuality e) + { + quality = new Quality(e.quality); + revision = new Revision(e.revision); + } public Quality quality { get; set; } public Revision revision { get; set; } } public class Revision { + public Revision() + { + + } + + public Revision(Revision r) + { + version = r.version; + real = r.real; + } public int version { get; set; } public int real { get; set; } } diff --git a/src/Ombi.Api.Sonarr/Models/Quality.cs b/src/Ombi.Api.Sonarr/Models/Quality.cs index 76a1c92d8..9989a9c3e 100644 --- a/src/Ombi.Api.Sonarr/Models/Quality.cs +++ b/src/Ombi.Api.Sonarr/Models/Quality.cs @@ -2,6 +2,16 @@ namespace Ombi.Api.Sonarr.Models { public class Quality { + public Quality() + { + + } + + public Quality(Quality q) + { + id = q.id; + name = q.name; + } public int id { get; set; } public string name { get; set; } } diff --git a/src/Ombi.Core/Senders/TvSender.cs b/src/Ombi.Core/Senders/TvSender.cs index 2cccd6778..3f5192ab8 100644 --- a/src/Ombi.Core/Senders/TvSender.cs +++ b/src/Ombi.Core/Senders/TvSender.cs @@ -165,25 +165,15 @@ namespace Ombi.Core.Senders titleSlug = model.ParentRequest.Title, addOptions = new AddOptions { - ignoreEpisodesWithFiles = true, // There shouldn't be any episodes with files, this is a new season - ignoreEpisodesWithoutFiles = true, // We want all missing + ignoreEpisodesWithFiles = false, // There shouldn't be any episodes with files, this is a new season + ignoreEpisodesWithoutFiles = false, // We want all missing searchForMissingEpisodes = false // we want dont want to search yet. We want to make sure everything is unmonitored/monitored correctly. } }; // Montitor the correct seasons, // If we have that season in the model then it's monitored! - var seasonsToAdd = new List(); - for (var i = 0; i < model.ParentRequest.TotalSeasons + 1; i++) - { - var index = i; - var season = new Season - { - seasonNumber = i, - monitored = model.SeasonRequests.Any(x => x.SeasonNumber == index && x.SeasonNumber != 0) - }; - seasonsToAdd.Add(season); - } + var seasonsToAdd = GetSeasonsToCreate(model); newSeries.seasons = seasonsToAdd; var result = await SonarrApi.AddSeries(newSeries, s.ApiKey, s.FullUri); existingSeries = await SonarrApi.GetSeriesById(result.id, s.ApiKey, s.FullUri); @@ -237,7 +227,7 @@ namespace Ombi.Core.Senders { var sonarrEp = sonarrEpList.FirstOrDefault(x => x.episodeNumber == ep.EpisodeNumber && x.seasonNumber == req.SeasonNumber); - if (sonarrEp != null) + if (sonarrEp != null && !sonarrEp.monitored) { sonarrEp.monitored = true; episodesToUpdate.Add(sonarrEp); @@ -245,27 +235,64 @@ namespace Ombi.Core.Senders } } var seriesChanges = false; + foreach (var season in model.SeasonRequests) { var sonarrSeason = sonarrEpList.Where(x => x.seasonNumber == season.SeasonNumber); var sonarrEpCount = sonarrSeason.Count(); var ourRequestCount = season.Episodes.Count; + var existingSeason = + result.seasons.FirstOrDefault(x => x.seasonNumber == season.SeasonNumber); + if (existingSeason == null) + { + Logger.LogError("There was no season numer {0} in Sonarr for title {1}", season.SeasonNumber, model.ParentRequest.Title); + continue; + } + + if (sonarrEpCount == ourRequestCount) { // We have the same amount of requests as all of the episodes in the season. - var existingSeason = - result.seasons.FirstOrDefault(x => x.seasonNumber == season.SeasonNumber); - if (existingSeason == null) + + if (!existingSeason.monitored) { - Logger.LogError("The sonarr ep count was the same as out request count, but could not match the season number {0}", season.SeasonNumber); - continue; + existingSeason.monitored = true; + seriesChanges = true; } - existingSeason.monitored = true; - seriesChanges = true; } else { + // Make sure this season is set to monitored + if (!existingSeason.monitored) + { + // We need to monitor it, problem being is all episodes will now be monitored + // So we need to monior the series but unmonitor every episode + // Except the episodes that are already monitored before we update the series (we do not want to unmonitor episodes that are monitored beforehand) + existingSeason.monitored = true; + var sea = result.seasons.FirstOrDefault(x => x.seasonNumber == existingSeason.seasonNumber); + sea.monitored = true; + //var previouslyMonitoredEpisodes = sonarrEpList.Where(x => + // x.seasonNumber == existingSeason.seasonNumber && x.monitored).Select(x => x.episodeNumber).ToList(); // We probably don't actually care about this + result = await SonarrApi.UpdateSeries(result, s.ApiKey, s.FullUri); + var epToUnmonitor = new List(); + var newEpList = sonarrEpList.ConvertAll(ep => new Episode(ep)); // Clone it so we don't modify the orignal member + foreach (var ep in newEpList.Where(x => x.seasonNumber == existingSeason.seasonNumber).ToList()) + { + //if (previouslyMonitoredEpisodes.Contains(ep.episodeNumber)) + //{ + // // This was previously monitored. + // continue; + //} + ep.monitored = false; + epToUnmonitor.Add(ep); + } + + foreach (var epToUpdate in epToUnmonitor) + { + await SonarrApi.UpdateEpisode(epToUpdate, s.ApiKey, s.FullUri); + } + } // Now update the episodes that need updating foreach (var epToUpdate in episodesToUpdate.Where(x => x.seasonNumber == season.SeasonNumber)) { @@ -285,6 +312,24 @@ namespace Ombi.Core.Senders } } + private static List GetSeasonsToCreate(ChildRequests model) + { + // Let's get a list of seasons just incase we need to change it + var seasonsToUpdate = new List(); + for (var i = 0; i < model.ParentRequest.TotalSeasons + 1; i++) + { + var index = i; + var sea = new Season + { + seasonNumber = i, + monitored = model.SeasonRequests.Any(x => x.SeasonNumber == index && x.SeasonNumber != 0) + }; + seasonsToUpdate.Add(sea); + } + + return seasonsToUpdate; + } + private async Task SendToSickRage(ChildRequests model, SickRageSettings settings, string qualityId = null) { var tvdbid = model.ParentRequest.TvDbId;