From 24b1d820845cf19a58dc9b75195eded7aac2ddec Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sun, 9 Oct 2016 15:07:36 +0100 Subject: [PATCH] Fixed #515 --- PlexRequests.Api.Interfaces/ISonarrApi.cs | 1 + PlexRequests.Api/SonarrApi.cs | 19 ++++++++++++ PlexRequests.UI/Helpers/TvSender.cs | 34 +++++++++++++++++++++ PlexRequests.UI/Views/Admin/Settings.cshtml | 4 ++- 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/PlexRequests.Api.Interfaces/ISonarrApi.cs b/PlexRequests.Api.Interfaces/ISonarrApi.cs index c0a40449c..da16e8344 100644 --- a/PlexRequests.Api.Interfaces/ISonarrApi.cs +++ b/PlexRequests.Api.Interfaces/ISonarrApi.cs @@ -46,6 +46,7 @@ namespace PlexRequests.Api.Interfaces IEnumerable GetEpisodes(string seriesId, string apiKey, Uri baseUrl); SonarrEpisode GetEpisode(string episodeId, string apiKey, Uri baseUrl); SonarrEpisode UpdateEpisode(SonarrEpisode episodeInfo, string apiKey, Uri baseUrl); + SonarrEpisodes UpdateEpisode(SonarrEpisodes episodeInfo, string apiKey, Uri baseUrl); SonarrAddEpisodeResult SearchForEpisodes(int[] episodeIds, string apiKey, Uri baseUrl); Series UpdateSeries(Series series, string apiKey, Uri baseUrl); SonarrSeasonSearchResult SearchForSeason(int seriesId, int seasonNumber, string apiKey, Uri baseUrl); diff --git a/PlexRequests.Api/SonarrApi.cs b/PlexRequests.Api/SonarrApi.cs index 6cc82fefa..e90c061cd 100644 --- a/PlexRequests.Api/SonarrApi.cs +++ b/PlexRequests.Api/SonarrApi.cs @@ -273,6 +273,25 @@ namespace PlexRequests.Api } } + public SonarrEpisodes UpdateEpisode(SonarrEpisodes episodeInfo, string apiKey, Uri baseUrl) + { + var request = new RestRequest { Resource = "/api/Episode", Method = Method.PUT }; + request.AddHeader("X-Api-Key", apiKey); + request.AddJsonBody(episodeInfo); + try + { + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => + Log.Error(exception, "Exception when calling UpdateEpisode for Sonarr, Retrying {0}", timespan)); + + return policy.Execute(() => Api.ExecuteJson(request, baseUrl)); + } + catch (Exception e) + { + Log.Error(e, "There has been an API exception when put the Sonarr UpdateEpisode"); + return null; + } + } + /// /// Search for one or more episodes /// diff --git a/PlexRequests.UI/Helpers/TvSender.cs b/PlexRequests.UI/Helpers/TvSender.cs index 1a02a1c2e..ad2cc3e37 100644 --- a/PlexRequests.UI/Helpers/TvSender.cs +++ b/PlexRequests.UI/Helpers/TvSender.cs @@ -147,6 +147,23 @@ namespace PlexRequests.UI.Helpers { var firstSeries = (series?.seasons?.OrderBy(x => x.seasonNumber)).FirstOrDefault(x => x.seasonNumber > 0) ?? new Season(); firstSeries.monitored = true; + var episodes = SonarrApi.GetEpisodes(series.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); // Need to get the episodes so we mark them as monitored + + var episodesToUpdate = new List(); + foreach (var e in episodes) + { + if (e.hasFile || e.seasonNumber != firstSeries.seasonNumber) + { + continue; + } + e.monitored = true; // Mark only the episodes we want as monitored + episodesToUpdate.Add(e); + } + foreach (var sonarrEpisode in episodesToUpdate) + { + SonarrApi.UpdateEpisode(sonarrEpisode, sonarrSettings.ApiKey, sonarrSettings.FullUri); + } + SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); SonarrApi.SearchForSeason(series.id, firstSeries.seasonNumber, sonarrSettings.ApiKey, sonarrSettings.FullUri); @@ -157,6 +174,23 @@ namespace PlexRequests.UI.Helpers { var lastSeries = series?.seasons?.OrderByDescending(x => x.seasonNumber)?.FirstOrDefault() ?? new Season(); lastSeries.monitored = true; + + var episodes = SonarrApi.GetEpisodes(series.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); // Need to get the episodes so we mark them as monitored + + var episodesToUpdate = new List(); + foreach (var e in episodes) + { + if (e.hasFile || e.seasonNumber != lastSeries.seasonNumber) + { + continue; + } + e.monitored = true; // Mark only the episodes we want as monitored + episodesToUpdate.Add(e); + } + foreach (var sonarrEpisode in episodesToUpdate) + { + SonarrApi.UpdateEpisode(sonarrEpisode, sonarrSettings.ApiKey, sonarrSettings.FullUri); + } SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); SonarrApi.SearchForSeason(series.id, lastSeries.seasonNumber, sonarrSettings.ApiKey, sonarrSettings.FullUri); diff --git a/PlexRequests.UI/Views/Admin/Settings.cshtml b/PlexRequests.UI/Views/Admin/Settings.cshtml index 13c6431bd..386b209c8 100644 --- a/PlexRequests.UI/Views/Admin/Settings.cshtml +++ b/PlexRequests.UI/Views/Admin/Settings.cshtml @@ -91,7 +91,9 @@ - + +
+