diff --git a/PlexRequests.UI/Helpers/TvSender.cs b/PlexRequests.UI/Helpers/TvSender.cs index 38d32b218..2f4f8d60a 100644 --- a/PlexRequests.UI/Helpers/TvSender.cs +++ b/PlexRequests.UI/Helpers/TvSender.cs @@ -70,14 +70,12 @@ namespace PlexRequests.UI.Helpers { int.TryParse(sonarrSettings.QualityProfile, out qualityProfile); } - - - var seriesTask = GetSonarrSeries(sonarrSettings, model.ProviderId); + + var series = await GetSonarrSeries(sonarrSettings, model.ProviderId); if (episodeRequest) { // Does series exist? - var series = await seriesTask; if (series != null) { // Series Exists @@ -85,33 +83,49 @@ namespace PlexRequests.UI.Helpers await RequestEpisodesWithExistingSeries(model, series, sonarrSettings); return new SonarrAddSeries {title = series.title}; } - else - { - // Series doesn't exist, need to add it as unmonitored. - var addResult = await Task.Run(() => SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, - sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, new int[0], sonarrSettings.ApiKey, - sonarrSettings.FullUri, false)); - // Get the series that was just added - series = await GetSonarrSeries(sonarrSettings, model.ProviderId); - series.monitored = false; // Un-monitor the series + // Series doesn't exist, need to add it as unmonitored. + var addResult = await Task.Run(() => SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, + sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, new int[0], sonarrSettings.ApiKey, + sonarrSettings.FullUri, false)); - // Un-monitor all seasons - foreach (var season in series.seasons) - { - season.monitored = false; - } - // Update the series, Since we cannot add as un-monitored due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404 - SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); + // Get the series that was just added + series = await GetSonarrSeries(sonarrSettings, model.ProviderId); + series.monitored = false; // Un-monitor the series + // Un-monitor all seasons + foreach (var season in series.seasons) + { + season.monitored = false; + } + + // Update the series, Since we cannot add as un-monitored due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404 + SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); - // We now have the series in Sonarr, update it to request the episodes. - await RequestEpisodesWithExistingSeries(model, series, sonarrSettings); - return addResult; + // We now have the series in Sonarr, update it to request the episodes. + await RequestEpisodesWithExistingSeries(model, series, sonarrSettings); + + return addResult; + } + + if (series != null) + { + // Monitor the seasons that we have chosen + foreach (var season in series.seasons) + { + if (model.SeasonList.Contains(season.seasonNumber)) + { + season.monitored = true; + } } + + // Update the series in sonarr with the new monitored status + SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); + await RequestAllEpisodesWithExistingSeries(model, series, sonarrSettings); + return new SonarrAddSeries { title = series.title }; // We have updated it } @@ -175,6 +189,37 @@ namespace PlexRequests.UI.Helpers SonarrApi.SearchForEpisodes(internalEpisodeIds.ToArray(), sonarrSettings.ApiKey, sonarrSettings.FullUri); } + + internal async Task RequestAllEpisodesWithExistingSeries(RequestedModel model, Series selectedSeries, SonarrSettings sonarrSettings) + { + // Show Exists + // Look up all episodes + var ep = SonarrApi.GetEpisodes(selectedSeries.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); + var episodes = ep?.ToList() ?? new List(); + + var internalEpisodeIds = new List(); + var tasks = new List(); + foreach (var r in episodes) + { + if(r.monitored || r.hasFile) // If it's already montiored or has the file, there is no point in updating it + { + continue; + } + + // Lookup the individual episode details + var episodeInfo = SonarrApi.GetEpisode(r.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); + episodeInfo.monitored = true; // Set the episode to monitored + + tasks.Add(Task.Run(() => SonarrApi.UpdateEpisode(episodeInfo, sonarrSettings.ApiKey, + sonarrSettings.FullUri))); + internalEpisodeIds.Add(r.id); + } + + await Task.WhenAll(tasks.ToArray()); + + SonarrApi.SearchForEpisodes(internalEpisodeIds.ToArray(), sonarrSettings.ApiKey, sonarrSettings.FullUri); + } + private async Task GetSonarrSeries(SonarrSettings sonarrSettings, int showId) { var task = await Task.Run(() => SonarrApi.GetSeries(sonarrSettings.ApiKey, sonarrSettings.FullUri)).ConfigureAwait(false);