From a5d83ed8d830fac9a71b3d086607fd57444df44b Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 16 Feb 2018 21:56:06 +0000 Subject: [PATCH] Fixed #1789 --- src/Ombi.Api.Radarr/IRadarrApi.cs | 3 ++ .../RadarrApi.CommandResult.cs | 17 +++++++++ src/Ombi.Api.Radarr/RadarrApi.cs | 35 +++++++++++++++-- src/Ombi.Core/Senders/MovieSender.cs | 38 +++++++++++++++---- 4 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/Ombi.Api.Radarr/RadarrApi.CommandResult.cs diff --git a/src/Ombi.Api.Radarr/IRadarrApi.cs b/src/Ombi.Api.Radarr/IRadarrApi.cs index 8df55b748..a23f6d3f5 100644 --- a/src/Ombi.Api.Radarr/IRadarrApi.cs +++ b/src/Ombi.Api.Radarr/IRadarrApi.cs @@ -10,6 +10,9 @@ namespace Ombi.Api.Radarr Task> GetProfiles(string apiKey, string baseUrl); Task> GetRootFolders(string apiKey, string baseUrl); Task SystemStatus(string apiKey, string baseUrl); + Task GetMovie(int id, string apiKey, string baseUrl); + Task UpdateMovie(MovieResponse movie, string apiKey, string baseUrl); + Task MovieSearch(int[] movieIds, string apiKey, string baseUrl); Task AddMovie(int tmdbId, string title, int year, int qualityId, string rootPath,string apiKey, string baseUrl, bool searchNow, string minimumAvailability); } } \ No newline at end of file diff --git a/src/Ombi.Api.Radarr/RadarrApi.CommandResult.cs b/src/Ombi.Api.Radarr/RadarrApi.CommandResult.cs new file mode 100644 index 000000000..b14863840 --- /dev/null +++ b/src/Ombi.Api.Radarr/RadarrApi.CommandResult.cs @@ -0,0 +1,17 @@ +using System; + +namespace Ombi.Api.Radarr +{ + public partial class RadarrApi + { + public class CommandResult + { + public string name { get; set; } + public DateTime startedOn { get; set; } + public DateTime stateChangeTime { get; set; } + public bool sendUpdatesToClient { get; set; } + public string state { get; set; } + public int id { get; set; } + } + } +} diff --git a/src/Ombi.Api.Radarr/RadarrApi.cs b/src/Ombi.Api.Radarr/RadarrApi.cs index 9ec66eb70..00786fda6 100644 --- a/src/Ombi.Api.Radarr/RadarrApi.cs +++ b/src/Ombi.Api.Radarr/RadarrApi.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -10,7 +9,7 @@ using Ombi.Helpers; namespace Ombi.Api.Radarr { - public class RadarrApi : IRadarrApi + public partial class RadarrApi : IRadarrApi { public RadarrApi(ILogger logger, IApi api) { @@ -53,6 +52,23 @@ namespace Ombi.Api.Radarr return await Api.Request>(request); } + public async Task GetMovie(int id, string apiKey, string baseUrl) + { + var request = new Request($"/api/movie/{id}", baseUrl, HttpMethod.Get); + AddHeaders(request, apiKey); + + return await Api.Request(request); + } + + public async Task UpdateMovie(MovieResponse movie, string apiKey, string baseUrl) + { + var request = new Request($"/api/movie/", baseUrl, HttpMethod.Put); + AddHeaders(request, apiKey); + request.AddJsonBody(movie); + + return await Api.Request(request); + } + public async Task AddMovie(int tmdbId, string title, int year, int qualityId, string rootPath, string apiKey, string baseUrl, bool searchNow, string minimumAvailability) { var request = new Request("/api/movie", baseUrl, HttpMethod.Post); @@ -103,6 +119,19 @@ namespace Ombi.Api.Radarr return null; } + public async Task MovieSearch(int[] movieIds, string apiKey, string baseUrl) + { + var result = await Command(apiKey, baseUrl, new { name = "MoviesSearch", movieIds }); + return result != null; + } + + private async Task Command(string apiKey, string baseUrl, object body) + { + var request = new Request($"/api/Command/", baseUrl, HttpMethod.Post); + request.AddHeader("X-Api-Key", apiKey); + request.AddJsonBody(body); + return await Api.Request(request); + } /// /// Adds the required headers and also the authorization header diff --git a/src/Ombi.Core/Senders/MovieSender.cs b/src/Ombi.Core/Senders/MovieSender.cs index 5d19b5b06..e57a5bf2a 100644 --- a/src/Ombi.Core/Senders/MovieSender.cs +++ b/src/Ombi.Core/Senders/MovieSender.cs @@ -48,7 +48,7 @@ namespace Ombi.Core.Senders var dogSettings = await DogNzbSettings.GetSettingsAsync(); if (dogSettings.Enabled) { - await SendToDogNzb(model,dogSettings); + await SendToDogNzb(model, dogSettings); return new SenderResult { Success = true, @@ -95,18 +95,40 @@ namespace Ombi.Core.Senders } var rootFolderPath = model.RootPathOverride <= 0 ? settings.DefaultRootPath : await RadarrRootPath(model.RootPathOverride, settings); - var result = await RadarrApi.AddMovie(model.TheMovieDbId, model.Title, model.ReleaseDate.Year, qualityToUse, rootFolderPath, settings.ApiKey, settings.FullUri, !settings.AddOnly, settings.MinimumAvailability); - if (!string.IsNullOrEmpty(result.Error?.message)) + // Check if the movie already exists? Since it could be unmonitored + var movies = await RadarrApi.GetMovies(settings.ApiKey, settings.FullUri); + var existingMovie = movies.FirstOrDefault(x => x.tmdbId == model.TheMovieDbId); + if (existingMovie == null) { - Log.LogError(LoggingEvents.RadarrCacher,result.Error.message); - return new SenderResult { Success = false, Message = result.Error.message, Sent = false }; + var result = await RadarrApi.AddMovie(model.TheMovieDbId, model.Title, model.ReleaseDate.Year, + qualityToUse, rootFolderPath, settings.ApiKey, settings.FullUri, !settings.AddOnly, + settings.MinimumAvailability); + + if (!string.IsNullOrEmpty(result.Error?.message)) + { + Log.LogError(LoggingEvents.RadarrCacher, result.Error.message); + return new SenderResult { Success = false, Message = result.Error.message, Sent = false }; + } + if (!string.IsNullOrEmpty(result.title)) + { + return new SenderResult { Success = true, Sent = false }; + } + return new SenderResult { Success = true, Sent = false }; } - if (!string.IsNullOrEmpty(result.title)) + // We have the movie, check if we can request it or change the status + if (!existingMovie.monitored) { - return new SenderResult { Success = true, Sent = false }; + // let's set it to monitored and search for it + existingMovie.monitored = true; + await RadarrApi.UpdateMovie(existingMovie, settings.ApiKey, settings.FullUri); + // Search for it + await RadarrApi.MovieSearch(new[] { existingMovie.id }, settings.ApiKey, settings.FullUri); + + return new SenderResult { Success = true, Sent = true }; } - return new SenderResult { Success = true, Sent = false }; + + return new SenderResult { Success = false, Sent = false, Message = "Movie is already monitored" }; } private async Task RadarrRootPath(int overrideId, RadarrSettings settings)