parent
6739bf72c4
commit
55134d76c1
@ -1,39 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Movies;
|
||||
using NzbDrone.Core.Parser;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.NetImport.Radarr
|
||||
{
|
||||
public class RadarrImport : HttpNetImportBase<RadarrSettings>
|
||||
public class RadarrImport : NetImportBase<RadarrSettings>
|
||||
{
|
||||
private readonly IRadarrV3Proxy _radarrV3Proxy;
|
||||
public override string Name => "Radarr";
|
||||
public override bool Enabled => true;
|
||||
public override bool EnableAuto => false;
|
||||
|
||||
public override NetImportType ListType => NetImportType.Program;
|
||||
|
||||
public RadarrImport(IHttpClient httpClient,
|
||||
public RadarrImport(IRadarrV3Proxy radarrV3Proxy,
|
||||
IConfigService configService,
|
||||
IParsingService parsingService,
|
||||
Logger logger)
|
||||
: base(httpClient, configService, parsingService, logger)
|
||||
: base(configService, parsingService, logger)
|
||||
{
|
||||
_radarrV3Proxy = radarrV3Proxy;
|
||||
}
|
||||
|
||||
public override INetImportRequestGenerator GetRequestGenerator()
|
||||
public override NetImportFetchResult Fetch()
|
||||
{
|
||||
return new RadarrRequestGenerator()
|
||||
var movies = new List<Movie>();
|
||||
var anyFailure = false;
|
||||
|
||||
try
|
||||
{
|
||||
Settings = Settings,
|
||||
Logger = _logger,
|
||||
HttpClient = _httpClient
|
||||
};
|
||||
var remoteMovies = _radarrV3Proxy.GetMovies(Settings);
|
||||
|
||||
foreach (var remoteMovie in remoteMovies)
|
||||
{
|
||||
if (!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(remoteMovie.QualityProfileId))
|
||||
{
|
||||
movies.Add(new Movie
|
||||
{
|
||||
TmdbId = remoteMovie.TmdbId,
|
||||
Title = remoteMovie.Title,
|
||||
SortTitle = remoteMovie.SortTitle,
|
||||
TitleSlug = remoteMovie.TitleSlug,
|
||||
Overview = remoteMovie.Overview,
|
||||
Images = remoteMovie.Images.Select(x => MapImage(x, Settings.BaseUrl)).ToList(),
|
||||
Monitored = remoteMovie.Monitored,
|
||||
PhysicalRelease = remoteMovie.PhysicalRelease,
|
||||
InCinemas = remoteMovie.InCinemas,
|
||||
Year = remoteMovie.Year
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
anyFailure = true;
|
||||
}
|
||||
|
||||
return new NetImportFetchResult { Movies = movies, AnyFailure = anyFailure };
|
||||
}
|
||||
|
||||
public override IParseNetImportResponse GetParser()
|
||||
public override object RequestAction(string action, IDictionary<string, string> query)
|
||||
{
|
||||
return new RadarrParser(Settings);
|
||||
if (action == "getDevices")
|
||||
{
|
||||
// Return early if there is not an API key
|
||||
if (Settings.ApiKey.IsNullOrWhiteSpace())
|
||||
{
|
||||
return new
|
||||
{
|
||||
devices = new List<object>()
|
||||
};
|
||||
}
|
||||
|
||||
Settings.Validate().Filter("ApiKey").ThrowOnError();
|
||||
|
||||
var devices = _radarrV3Proxy.GetProfiles(Settings);
|
||||
|
||||
return new
|
||||
{
|
||||
options = devices.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase)
|
||||
.Select(d => new
|
||||
{
|
||||
id = d.Id,
|
||||
name = d.Name
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
return new { };
|
||||
}
|
||||
|
||||
protected override void Test(List<ValidationFailure> failures)
|
||||
{
|
||||
failures.AddIfNotNull(_radarrV3Proxy.Test(Settings));
|
||||
}
|
||||
|
||||
private static MediaCover.MediaCover MapImage(MediaCover.MediaCover arg, string baseUrl)
|
||||
{
|
||||
var newImage = new MediaCover.MediaCover
|
||||
{
|
||||
Url = string.Format("{0}{1}", baseUrl, arg.Url),
|
||||
CoverType = arg.CoverType
|
||||
};
|
||||
|
||||
return newImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Movies;
|
||||
|
||||
namespace NzbDrone.Core.NetImport.Radarr
|
||||
{
|
||||
public class RadarrParser : IParseNetImportResponse
|
||||
{
|
||||
private readonly RadarrSettings _settings;
|
||||
public RadarrParser(RadarrSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public IList<Movie> ParseResponse(NetImportResponse netMovieImporterResponse)
|
||||
{
|
||||
var remoteMovies = Json.Deserialize<List<RadarrMovie>>(netMovieImporterResponse.Content);
|
||||
|
||||
var movies = new List<Movie>();
|
||||
|
||||
foreach (var remoteMovie in remoteMovies)
|
||||
{
|
||||
movies.Add(new Movie
|
||||
{
|
||||
TmdbId = remoteMovie.TmdbId,
|
||||
Title = remoteMovie.Title,
|
||||
SortTitle = remoteMovie.SortTitle,
|
||||
TitleSlug = remoteMovie.TitleSlug,
|
||||
Overview = remoteMovie.Overview,
|
||||
Images = remoteMovie.Images.Select(x => MapImage(x, _settings.BaseUrl)).ToList(),
|
||||
Monitored = remoteMovie.Monitored,
|
||||
PhysicalRelease = remoteMovie.PhysicalRelease,
|
||||
InCinemas = remoteMovie.InCinemas,
|
||||
Year = remoteMovie.Year
|
||||
});
|
||||
}
|
||||
|
||||
return movies;
|
||||
}
|
||||
|
||||
private static MediaCover.MediaCover MapImage(MediaCover.MediaCover arg, string baseUrl)
|
||||
{
|
||||
var newImage = new MediaCover.MediaCover
|
||||
{
|
||||
Url = string.Format("{0}{1}", baseUrl, arg.Url),
|
||||
CoverType = arg.CoverType
|
||||
};
|
||||
|
||||
return newImage;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.NetImport.Radarr
|
||||
{
|
||||
public interface IRadarrV3Proxy
|
||||
{
|
||||
List<RadarrMovie> GetMovies(RadarrSettings settings);
|
||||
List<RadarrProfile> GetProfiles(RadarrSettings settings);
|
||||
ValidationFailure Test(RadarrSettings settings);
|
||||
}
|
||||
|
||||
public class RadarrV3Proxy : IRadarrV3Proxy
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RadarrV3Proxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public List<RadarrMovie> GetMovies(RadarrSettings settings)
|
||||
{
|
||||
return Execute<RadarrMovie>("/api/v3/movie", settings);
|
||||
}
|
||||
|
||||
public List<RadarrProfile> GetProfiles(RadarrSettings settings)
|
||||
{
|
||||
return Execute<RadarrProfile>("/api/v3/qualityprofile", settings);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(RadarrSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
GetMovies(settings);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
_logger.Error(ex, "API Key is invalid");
|
||||
return new ValidationFailure("ApiKey", "API Key is invalid");
|
||||
}
|
||||
|
||||
_logger.Error(ex, "Unable to send test message");
|
||||
return new ValidationFailure("ApiKey", "Unable to send test message");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to send test message");
|
||||
return new ValidationFailure("", "Unable to send test message");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<TResource> Execute<TResource>(string resource, RadarrSettings settings)
|
||||
{
|
||||
if (settings.BaseUrl.IsNullOrWhiteSpace() || settings.ApiKey.IsNullOrWhiteSpace())
|
||||
{
|
||||
return new List<TResource>();
|
||||
}
|
||||
|
||||
var baseUrl = settings.BaseUrl.TrimEnd('/');
|
||||
|
||||
var request = new HttpRequestBuilder(baseUrl).Resource(resource).Accept(HttpAccept.Json)
|
||||
.SetHeader("X-Api-Key", settings.ApiKey).Build();
|
||||
|
||||
var response = _httpClient.Get(request);
|
||||
|
||||
var results = JsonConvert.DeserializeObject<List<TResource>>(response.Content);
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue