Fixed: Add AltTitles from TMDB if Mapping Fails

Fixes #4300
pull/4303/head
Qstick 5 years ago
parent a711cbd475
commit 39250abf7d

@ -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";
}

@ -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<MovieResult> DiscoverMovies(string action, Func<HttpRequest, HttpRequest> enhanceRequest);
List<AlternativeTitle> AlternativeTitlesForMovie(int tmdbId);
Tuple<List<AlternativeTitle>, 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<T>(HttpRequest request)
{
request.AllowAutoRedirect = true;
request.Headers.Accept = HttpAccept.Json.Value;
request.SuppressHttpError = true;
var response = Execute(request);
try
{
var error = JsonConvert.DeserializeObject<RadarrError>(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<T>(response.Content);
_apiBuilder = requestBuilder.RadarrAPI;
}
public List<MovieResult> DiscoverMovies(string action, Func<HttpRequest, HttpRequest> 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<AlternativeTitle> 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<Mapping>(request);
var titles = new List<NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle>();
var titles = new List<AlternativeTitle>();
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<List<AlternativeTitle>, 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<Mapping>(request);
var titles = new List<NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle>();
var titles = new List<AlternativeTitle>();
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<T>(HttpRequest request)
{
request.AllowAutoRedirect = true;
request.Headers.Accept = HttpAccept.Json.Value;
request.SuppressHttpError = true;
var response = Execute(request);
try
{
var error = JsonConvert.DeserializeObject<RadarrError>(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<T>(response.Content);
}
}
}

@ -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> { 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));
}

Loading…
Cancel
Save