mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-04 10:02:40 +01:00
parent
a711cbd475
commit
39250abf7d
@ -7,6 +7,7 @@ public interface IRadarrCloudRequestBuilder
|
||||
IHttpRequestBuilderFactory Services { get; }
|
||||
IHttpRequestBuilderFactory TMDB { get; }
|
||||
IHttpRequestBuilderFactory TMDBSingle { get; }
|
||||
IHttpRequestBuilderFactory RadarrAPI { get; }
|
||||
}
|
||||
|
||||
public class RadarrCloudRequestBuilder : IRadarrCloudRequestBuilder
|
||||
@ -23,11 +24,15 @@ public RadarrCloudRequestBuilder()
|
||||
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.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,36 +12,110 @@ 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";
|
||||
_apiBuilder = requestBuilder.RadarrAPI;
|
||||
}
|
||||
|
||||
RadarrAPI = new HttpRequestBuilder(APIURL + "/{route}/{action}")
|
||||
.CreateFactory();
|
||||
public List<MovieResult> DiscoverMovies(string action, Func<HttpRequest, HttpRequest> enhanceRequest = null)
|
||||
{
|
||||
var request = _apiBuilder.Create().SetSegment("route", "discovery").SetSegment("action", action).Build();
|
||||
|
||||
if (enhanceRequest != null)
|
||||
{
|
||||
request = enhanceRequest(request);
|
||||
}
|
||||
|
||||
return Execute<List<MovieResult>>(request);
|
||||
}
|
||||
|
||||
public List<AlternativeTitle> AlternativeTitlesForMovie(int tmdbId)
|
||||
{
|
||||
var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build();
|
||||
|
||||
var mappings = Execute<Mapping>(request);
|
||||
|
||||
var titles = new List<AlternativeTitle>();
|
||||
|
||||
foreach (var altTitle in mappings.Mappings.Titles)
|
||||
{
|
||||
titles.Add(new AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id));
|
||||
}
|
||||
|
||||
return titles;
|
||||
}
|
||||
|
||||
public Tuple<List<AlternativeTitle>, AlternativeYear> AlternativeTitlesAndYearForMovie(int tmdbId)
|
||||
{
|
||||
var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "find").AddQueryParam("tmdbid", tmdbId).Build();
|
||||
|
||||
var mappings = Execute<Mapping>(request);
|
||||
|
||||
var titles = new List<AlternativeTitle>();
|
||||
|
||||
foreach (var altTitle in mappings.Mappings.Titles)
|
||||
{
|
||||
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();
|
||||
|
||||
AlternativeYear newYear = null;
|
||||
|
||||
if (year != null)
|
||||
{
|
||||
newYear = new AlternativeYear
|
||||
{
|
||||
Year = year.Info.AkaYear,
|
||||
SourceId = year.Id
|
||||
};
|
||||
}
|
||||
|
||||
return new Tuple<List<AlternativeTitle>, AlternativeYear>(titles, newYear);
|
||||
}
|
||||
|
||||
public AlternativeTitle AddNewAlternativeTitle(AlternativeTitle title, int tmdbId)
|
||||
{
|
||||
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();
|
||||
|
||||
var newMapping = Execute<AddTitleMapping>(request);
|
||||
|
||||
var newTitle = new AlternativeTitle(newMapping.Info.AkaTitle, SourceType.Mappings, newMapping.Id, title.Language);
|
||||
newTitle.VoteCount = newMapping.VoteCount;
|
||||
newTitle.Votes = newMapping.Votes;
|
||||
|
||||
return newTitle;
|
||||
}
|
||||
|
||||
public AlternativeYear AddNewAlternativeYear(int year, int tmdbId)
|
||||
{
|
||||
var request = _apiBuilder.Create().SetSegment("route", "mappings").SetSegment("action", "add")
|
||||
.AddQueryParam("tmdbid", tmdbId).AddQueryParam("type", "year")
|
||||
.AddQueryParam("aka_year", year).Build();
|
||||
|
||||
var newYear = Execute<AddYearMapping>(request);
|
||||
|
||||
return new AlternativeYear
|
||||
{
|
||||
Year = newYear.Info.AkaYear,
|
||||
SourceId = newYear.Id
|
||||
};
|
||||
}
|
||||
|
||||
private HttpResponse Execute(HttpRequest request)
|
||||
@ -89,95 +163,5 @@ private T Execute<T>(HttpRequest request)
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(response.Content);
|
||||
}
|
||||
|
||||
public List<MovieResult> DiscoverMovies(string action, Func<HttpRequest, HttpRequest> enhanceRequest = null)
|
||||
{
|
||||
var request = RadarrAPI.Create().SetSegment("route", "discovery").SetSegment("action", action).Build();
|
||||
|
||||
if (enhanceRequest != null)
|
||||
{
|
||||
request = enhanceRequest(request);
|
||||
}
|
||||
|
||||
return Execute<List<MovieResult>>(request);
|
||||
}
|
||||
|
||||
public List<AlternativeTitle> AlternativeTitlesForMovie(int tmdbId)
|
||||
{
|
||||
var request = RadarrAPI.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>();
|
||||
|
||||
foreach (var altTitle in mappings.Mappings.Titles)
|
||||
{
|
||||
titles.Add(new NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id));
|
||||
}
|
||||
|
||||
return titles;
|
||||
}
|
||||
|
||||
public Tuple<List<AlternativeTitle>, AlternativeYear> AlternativeTitlesAndYearForMovie(int tmdbId)
|
||||
{
|
||||
var request = RadarrAPI.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>();
|
||||
|
||||
foreach (var altTitle in mappings.Mappings.Titles)
|
||||
{
|
||||
titles.Add(new NzbDrone.Core.Movies.AlternativeTitles.AlternativeTitle(altTitle.Info.AkaTitle, SourceType.Mappings, altTitle.Id));
|
||||
}
|
||||
|
||||
var year = mappings.Mappings.Years.Where(y => y.Votes >= 3).OrderBy(y => y.Votes).FirstOrDefault();
|
||||
|
||||
AlternativeYear newYear = null;
|
||||
|
||||
if (year != null)
|
||||
{
|
||||
newYear = new AlternativeYear
|
||||
{
|
||||
Year = year.Info.AkaYear,
|
||||
SourceId = year.Id
|
||||
};
|
||||
}
|
||||
|
||||
return new Tuple<List<AlternativeTitle>, AlternativeYear>(titles, newYear);
|
||||
}
|
||||
|
||||
public AlternativeTitle AddNewAlternativeTitle(AlternativeTitle title, int tmdbId)
|
||||
{
|
||||
var request = RadarrAPI.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();
|
||||
|
||||
var newMapping = Execute<AddTitleMapping>(request);
|
||||
|
||||
var newTitle = new AlternativeTitle(newMapping.Info.AkaTitle, SourceType.Mappings, newMapping.Id, title.Language);
|
||||
newTitle.VoteCount = newMapping.VoteCount;
|
||||
newTitle.Votes = newMapping.Votes;
|
||||
|
||||
return newTitle;
|
||||
}
|
||||
|
||||
public AlternativeYear AddNewAlternativeYear(int year, int tmdbId)
|
||||
{
|
||||
var request = RadarrAPI.Create().SetSegment("route", "mappings").SetSegment("action", "add")
|
||||
.AddQueryParam("tmdbid", tmdbId).AddQueryParam("type", "year")
|
||||
.AddQueryParam("aka_year", year).Build();
|
||||
|
||||
var newYear = Execute<AddYearMapping>(request);
|
||||
|
||||
return new AlternativeYear
|
||||
{
|
||||
Year = newYear.Info.AkaYear,
|
||||
SourceId = newYear.Id
|
||||
};
|
||||
}
|
||||
|
||||
public IHttpRequestBuilderFactory RadarrAPI { get; private set; }
|
||||
}
|
||||
}
|
||||
|
@ -134,8 +134,6 @@ private void RefreshMovieInfo(Movie movie)
|
||||
|
||||
movieInfo.AlternativeTitles.AddRange(mappingsTitles);
|
||||
|
||||
movie.AlternativeTitles = _titleService.UpdateTitles(movieInfo.AlternativeTitles, movie);
|
||||
|
||||
if (mappings.Item2 != null)
|
||||
{
|
||||
movie.SecondaryYear = mappings.Item2.Year;
|
||||
@ -156,21 +154,11 @@ private void RefreshMovieInfo(Movie movie)
|
||||
_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…
Reference in New Issue
Block a user