From 4dc4f8350654233f56c0051e9b691669fdf62715 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 25 Oct 2016 22:35:45 +0100 Subject: [PATCH] fixed #608 and some other small stuff --- PlexRequests.Api.Interfaces/ISonarrApi.cs | 4 ++ PlexRequests.Api/SonarrApi.cs | 69 +++++++++++++++++++ .../Templates/BasicRequestTemplate.html | 2 +- PlexRequests.Core/PlexRequests.Core.csproj | 2 +- .../Jobs/Templates/RecentlyAddedTemplate.html | 2 +- PlexRequests.UI/Helpers/TvSender.cs | 53 +++++++------- .../Views/Admin/NewsletterSettings.cshtml | 7 +- PlexRequests.UI/Views/Requests/Index.cshtml | 5 +- 8 files changed, 109 insertions(+), 35 deletions(-) diff --git a/PlexRequests.Api.Interfaces/ISonarrApi.cs b/PlexRequests.Api.Interfaces/ISonarrApi.cs index da16e8344..63c698b74 100644 --- a/PlexRequests.Api.Interfaces/ISonarrApi.cs +++ b/PlexRequests.Api.Interfaces/ISonarrApi.cs @@ -39,6 +39,10 @@ namespace PlexRequests.Api.Interfaces int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false); + SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, + int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, + bool searchForMissingEpisodes = false); + SystemStatus SystemStatus(string apiKey, Uri baseUrl); List GetSeries(string apiKey, Uri baseUrl); diff --git a/PlexRequests.Api/SonarrApi.cs b/PlexRequests.Api/SonarrApi.cs index 41c7473ec..961f04ea2 100644 --- a/PlexRequests.Api/SonarrApi.cs +++ b/PlexRequests.Api/SonarrApi.cs @@ -135,6 +135,75 @@ namespace PlexRequests.Api return result; } + public SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false) + { + var request = new RestRequest + { + Resource = "/api/Series?", + Method = Method.POST + }; + + var options = new SonarrAddSeries + { + seasonFolder = seasonFolders, + title = title, + qualityProfileId = qualityId, + tvdbId = tvdbId, + titleSlug = title, + seasons = new List(), + rootFolderPath = rootPath, + monitored = monitor + }; + + if (!searchForMissingEpisodes) + { + options.addOptions = new AddOptions + { + searchForMissingEpisodes = false, + ignoreEpisodesWithFiles = true, + ignoreEpisodesWithoutFiles = true + }; + } + + for (var i = 1; i <= seasons.Length; i++) + { + var season = new Season + { + seasonNumber = i, + // ReSharper disable once SimplifyConditionalTernaryExpression + monitored = true + }; + options.seasons.Add(season); + } + + Log.Debug("Sonarr API Options:"); + Log.Debug(options.DumpJson()); + + request.AddHeader("X-Api-Key", apiKey); + request.AddJsonBody(options); + + SonarrAddSeries result; + try + { + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] { + TimeSpan.FromSeconds (1), + TimeSpan.FromSeconds(2), + }); + + result = policy.Execute(() => Api.ExecuteJson(request, baseUrl)); + } + catch (JsonSerializationException jse) + { + Log.Error(jse); + var error = Api.ExecuteJson>(request, baseUrl); + var messages = error?.Select(x => x.errorMessage).ToList(); + messages?.ForEach(x => Log.Error(x)); + result = new SonarrAddSeries { ErrorMessages = messages }; + } + + return result; + } + public SystemStatus SystemStatus(string apiKey, Uri baseUrl) { var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET }; diff --git a/PlexRequests.Core/Notification/Templates/BasicRequestTemplate.html b/PlexRequests.Core/Notification/Templates/BasicRequestTemplate.html index 3d005619e..bb03e8ecd 100644 --- a/PlexRequests.Core/Notification/Templates/BasicRequestTemplate.html +++ b/PlexRequests.Core/Notification/Templates/BasicRequestTemplate.html @@ -144,7 +144,7 @@ diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 143508e1e..8d81638fa 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -144,7 +144,7 @@ - PreserveNewest + Always diff --git a/PlexRequests.Services/Jobs/Templates/RecentlyAddedTemplate.html b/PlexRequests.Services/Jobs/Templates/RecentlyAddedTemplate.html index 0ab085dce..5a7258ea0 100644 --- a/PlexRequests.Services/Jobs/Templates/RecentlyAddedTemplate.html +++ b/PlexRequests.Services/Jobs/Templates/RecentlyAddedTemplate.html @@ -144,7 +144,7 @@
- +
diff --git a/PlexRequests.UI/Helpers/TvSender.cs b/PlexRequests.UI/Helpers/TvSender.cs index cc562f2af..b67311d64 100644 --- a/PlexRequests.UI/Helpers/TvSender.cs +++ b/PlexRequests.UI/Helpers/TvSender.cs @@ -119,43 +119,19 @@ namespace PlexRequests.UI.Helpers return addResult; } - - - if (requestAll ?? false) - { - //// Monitor all seasons - //foreach (var season in series.seasons) - //{ - // season.monitored = true; - //} - - - //SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); - //SonarrApi.SearchForSeries(series.id, sonarrSettings.ApiKey, sonarrSettings.FullUri); // Search For all episodes!" - - - // This is a work around for this issue: https://github.com/Sonarr/Sonarr/issues/1507 - // The above is the previous code. - SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, - sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, model.SeasonList, sonarrSettings.ApiKey, - sonarrSettings.FullUri, true, true); - return new SonarrAddSeries { title = series.title }; // We have updated it - } - // Series exists, don't need to add it if (series == null) { // Set the series as monitored with a season count as 0 so it doesn't search for anything - SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, - sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, model.SeasonList, sonarrSettings.ApiKey, + SonarrApi.AddSeriesNew(model.ProviderId, model.Title, qualityProfile, + sonarrSettings.SeasonFolders, sonarrSettings.RootPath, new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13}, sonarrSettings.ApiKey, sonarrSettings.FullUri); await Task.Delay(TimeSpan.FromSeconds(1)); series = await GetSonarrSeries(sonarrSettings, model.ProviderId); - - // Due to the bug above, we need to make sure all seasons are not monitored + foreach (var s in series.seasons) { s.monitored = false; @@ -164,6 +140,29 @@ namespace PlexRequests.UI.Helpers SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); } + if (requestAll ?? false) + { + // Monitor all seasons + foreach (var season in series.seasons) + { + season.monitored = true; + } + + + SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri); + SonarrApi.SearchForSeries(series.id, sonarrSettings.ApiKey, sonarrSettings.FullUri); // Search For all episodes!" + + + //// This is a work around for this issue: https://github.com/Sonarr/Sonarr/issues/1507 + //// The above is the previous code. + //SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, + // sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, model.SeasonList, sonarrSettings.ApiKey, + // sonarrSettings.FullUri, true, true); + return new SonarrAddSeries { title = series.title }; // We have updated it + } + + + if (first ?? false) { var firstSeries = (series?.seasons?.OrderBy(x => x.seasonNumber)).FirstOrDefault(x => x.seasonNumber > 0) ?? new Season(); diff --git a/PlexRequests.UI/Views/Admin/NewsletterSettings.cshtml b/PlexRequests.UI/Views/Admin/NewsletterSettings.cshtml index 422f7745e..8573817a7 100644 --- a/PlexRequests.UI/Views/Admin/NewsletterSettings.cshtml +++ b/PlexRequests.UI/Views/Admin/NewsletterSettings.cshtml @@ -50,7 +50,7 @@
- +
@@ -103,7 +103,7 @@ e.preventDefault(); var base = '@Html.GetBaseUrl()'; var url = createBaseUrl(base, '/admin/recentlyAddedTest'); - + $('#spinner').attr("class", "fa fa-spinner fa-spin"); $.ajax({ type: "post", url: url, @@ -111,14 +111,17 @@ success: function (response) { if (response) { generateNotify(response.message, "success"); + $('#spinner').attr("class", "fa fa-check"); } else { generateNotify(response.message, "danger"); + $('#spinner').attr("class", "fa fa-times"); } }, error: function (e) { console.log(e); generateNotify("Something went wrong!", "danger"); + $('#spinner').attr("class", "fa fa-times"); } }); }); diff --git a/PlexRequests.UI/Views/Requests/Index.cshtml b/PlexRequests.UI/Views/Requests/Index.cshtml index 392d2f11f..8909be59b 100644 --- a/PlexRequests.UI/Views/Requests/Index.cshtml +++ b/PlexRequests.UI/Views/Requests/Index.cshtml @@ -223,11 +223,10 @@ {{/if}}
@UI.Requests_RequestedDate: {{requestedDate}}
- @UI.Issues_Issue: {{#if_eq issueId 0}} - + @*Nothing*@ {{else}} - + @UI.Issues_Issue: {{/if_eq}}
- +