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