From 39250abf7dabc1ef7194bd9c6c53d8d800e9d630 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 9 Apr 2020 21:22:33 -0400 Subject: [PATCH] Fixed: Add AltTitles from TMDB if Mapping Fails Fixes #4300 --- .../Cloud/RadarrCloudRequestBuilder.cs | 5 + .../RadarrAPI/RadarrAPIClient.cs | 132 ++++++++---------- .../Movies/RefreshMovieService.cs | 16 +-- 3 files changed, 65 insertions(+), 88 deletions(-) diff --git a/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs b/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs index bce97d36a..a3c10f470 100644 --- a/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs +++ b/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs @@ -7,6 +7,7 @@ namespace NzbDrone.Common.Cloud IHttpRequestBuilderFactory Services { get; } IHttpRequestBuilderFactory TMDB { get; } IHttpRequestBuilderFactory TMDBSingle { get; } + IHttpRequestBuilderFactory RadarrAPI { get; } } public class RadarrCloudRequestBuilder : IRadarrCloudRequestBuilder @@ -23,11 +24,15 @@ namespace NzbDrone.Common.Cloud TMDBSingle = new HttpRequestBuilder("https://api.themoviedb.org/3/{route}") .SetHeader("Authorization", $"Bearer {AuthToken}") .CreateFactory(); + + RadarrAPI = new HttpRequestBuilder("https://api.radarr.video/v2/{route}/{action}") + .CreateFactory(); } public IHttpRequestBuilderFactory Services { get; private set; } public IHttpRequestBuilderFactory TMDB { get; private set; } public IHttpRequestBuilderFactory TMDBSingle { get; private set; } + public IHttpRequestBuilderFactory RadarrAPI { get; private set; } public string AuthToken => "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxYTczNzMzMDE5NjFkMDNmOTdmODUzYTg3NmRkMTIxMiIsInN1YiI6IjU4NjRmNTkyYzNhMzY4MGFiNjAxNzUzNCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.gh1BwogCCKOda6xj9FRMgAAj_RYKMMPC3oNlcBtlmwk"; } diff --git a/src/NzbDrone.Core/MetadataSource/RadarrAPI/RadarrAPIClient.cs b/src/NzbDrone.Core/MetadataSource/RadarrAPI/RadarrAPIClient.cs index a0860d1a4..791c552a1 100644 --- a/src/NzbDrone.Core/MetadataSource/RadarrAPI/RadarrAPIClient.cs +++ b/src/NzbDrone.Core/MetadataSource/RadarrAPI/RadarrAPIClient.cs @@ -2,8 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; +using NzbDrone.Common.Cloud; using NzbDrone.Common.Http; -using NzbDrone.Core.Configuration; using NzbDrone.Core.MetadataSource.SkyHook.Resource; using NzbDrone.Core.Movies.AlternativeTitles; using NzbDrone.Core.Parser; @@ -12,87 +12,27 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI { public interface IRadarrAPIClient { - IHttpRequestBuilderFactory RadarrAPI { get; } List DiscoverMovies(string action, Func enhanceRequest); List AlternativeTitlesForMovie(int tmdbId); Tuple, AlternativeYear> AlternativeTitlesAndYearForMovie(int tmdbId); AlternativeTitle AddNewAlternativeTitle(AlternativeTitle title, int tmdbId); AlternativeYear AddNewAlternativeYear(int year, int tmdbId); - string APIURL { get; } } public class RadarrAPIClient : IRadarrAPIClient { + private readonly IHttpRequestBuilderFactory _apiBuilder; private readonly IHttpClient _httpClient; - public string APIURL { get; private set; } - - public RadarrAPIClient(IConfigFileProvider configFile, IHttpClient httpClient) + public RadarrAPIClient(IHttpClient httpClient, IRadarrCloudRequestBuilder requestBuilder) { _httpClient = httpClient; - - if (configFile.Branch == "nightly") - { - APIURL = "https://staging.api.radarr.video"; - } - else - { - APIURL = "https://api.radarr.video/v2"; - } - - RadarrAPI = new HttpRequestBuilder(APIURL + "/{route}/{action}") - .CreateFactory(); - } - - private HttpResponse Execute(HttpRequest request) - { - if (request.Method == HttpMethod.GET) - { - return _httpClient.Get(request); - } - else if (request.Method == HttpMethod.POST) - { - return _httpClient.Post(request); - } - else - { - throw new NotImplementedException($"Method {request.Method} not implemented"); - } - } - - private T Execute(HttpRequest request) - { - request.AllowAutoRedirect = true; - request.Headers.Accept = HttpAccept.Json.Value; - request.SuppressHttpError = true; - - var response = Execute(request); - - try - { - var error = JsonConvert.DeserializeObject(response.Content); - - if (error != null && error.Errors != null && error.Errors.Count != 0) - { - throw new RadarrAPIException(error); - } - } - catch (JsonSerializationException) - { - //No error! - } - - if (response.StatusCode != System.Net.HttpStatusCode.OK) - { - throw new HttpException(request, response); - } - - return JsonConvert.DeserializeObject(response.Content); + _apiBuilder = requestBuilder.RadarrAPI; } public List DiscoverMovies(string action, Func enhanceRequest = null) { - var request = RadarrAPI.Create().SetSegment("route", "discovery").SetSegment("action", action).Build(); + var request = _apiBuilder.Create().SetSegment("route", "discovery").SetSegment("action", action).Build(); if (enhanceRequest != null) { @@ -104,15 +44,15 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI public List AlternativeTitlesForMovie(int tmdbId) { - var request = RadarrAPI.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build(); + var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build(); var mappings = Execute(request); - var titles = new List(); + var titles = new List(); foreach (var altTitle in mappings.Mappings.Titles) { - titles.Add(new NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id)); + titles.Add(new AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id)); } return titles; @@ -120,15 +60,15 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI public Tuple, AlternativeYear> AlternativeTitlesAndYearForMovie(int tmdbId) { - var request = RadarrAPI.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build(); + var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build(); var mappings = Execute(request); - var titles = new List(); + var titles = new List(); foreach (var altTitle in mappings.Mappings.Titles) { - titles.Add(new NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id)); + titles.Add(new AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id)); } var year = mappings.Mappings.Years.Where(y => y.Votes >= 3).OrderBy(y => y.Votes).FirstOrDefault(); @@ -149,7 +89,7 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI public AlternativeTitle AddNewAlternativeTitle(AlternativeTitle title, int tmdbId) { - var request = RadarrAPI.Create().SetSegment("route", "mappings").SetSegment("action", "add") + var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "add") .AddQueryParam("tmdbid", tmdbId).AddQueryParam("type", "title") .AddQueryParam("language", IsoLanguages.Get(title.Language).TwoLetterCode) .AddQueryParam("aka_title", title.Title).Build(); @@ -165,7 +105,7 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI public AlternativeYear AddNewAlternativeYear(int year, int tmdbId) { - var request = RadarrAPI.Create().SetSegment("route", "mappings").SetSegment("action", "add") + var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "add") .AddQueryParam("tmdbid", tmdbId).AddQueryParam("type", "year") .AddQueryParam("aka_year", year).Build(); @@ -178,6 +118,50 @@ namespace NzbDrone.Core.MetadataSource.RadarrAPI }; } - public IHttpRequestBuilderFactory RadarrAPI { get; private set; } + private HttpResponse Execute(HttpRequest request) + { + if (request.Method == HttpMethod.GET) + { + return _httpClient.Get(request); + } + else if (request.Method == HttpMethod.POST) + { + return _httpClient.Post(request); + } + else + { + throw new NotImplementedException($"Method {request.Method} not implemented"); + } + } + + private T Execute(HttpRequest request) + { + request.AllowAutoRedirect = true; + request.Headers.Accept = HttpAccept.Json.Value; + request.SuppressHttpError = true; + + var response = Execute(request); + + try + { + var error = JsonConvert.DeserializeObject(response.Content); + + if (error != null && error.Errors != null && error.Errors.Count != 0) + { + throw new RadarrAPIException(error); + } + } + catch (JsonSerializationException) + { + //No error! + } + + if (response.StatusCode != System.Net.HttpStatusCode.OK) + { + throw new HttpException(request, response); + } + + return JsonConvert.DeserializeObject(response.Content); + } } } diff --git a/src/NzbDrone.Core/Movies/RefreshMovieService.cs b/src/NzbDrone.Core/Movies/RefreshMovieService.cs index a6f13719f..e36e19de3 100644 --- a/src/NzbDrone.Core/Movies/RefreshMovieService.cs +++ b/src/NzbDrone.Core/Movies/RefreshMovieService.cs @@ -134,8 +134,6 @@ namespace NzbDrone.Core.Movies movieInfo.AlternativeTitles.AddRange(mappingsTitles); - movie.AlternativeTitles = _titleService.UpdateTitles(movieInfo.AlternativeTitles, movie); - if (mappings.Item2 != null) { movie.SecondaryYear = mappings.Item2.Year; @@ -156,21 +154,11 @@ namespace NzbDrone.Core.Movies _logger.Info(ex, "Unable to communicate with Mappings Server."); } + movie.AlternativeTitles = _titleService.UpdateTitles(movieInfo.AlternativeTitles, movie); + _movieService.UpdateMovie(new List { movie }, true); _creditService.UpdateCredits(credits, movie); - try - { - var newTitles = movieInfo.AlternativeTitles.Except(movie.AlternativeTitles); - - //_titleService.AddAltTitles(newTitles.ToList(), movie); - } - catch (Exception e) - { - _logger.Debug(e, "Failed adding alternative titles."); - throw; - } - _logger.Debug("Finished movie refresh for {0}", movie.Title); _eventAggregator.PublishEvent(new MovieUpdatedEvent(movie)); }