using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; using NLog; using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.ImportLists.ImportListMovies; using NzbDrone.Core.Parser; using NzbDrone.Core.Validation; namespace NzbDrone.Core.ImportLists.Radarr { public class RadarrImport : ImportListBase { private readonly IRadarrV3Proxy _radarrV3Proxy; public override string Name => "Radarr"; public override bool Enabled => true; public override bool EnableAuto => false; public override ImportListType ListType => ImportListType.Program; public RadarrImport(IRadarrV3Proxy radarrV3Proxy, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger) : base(importListStatusService, configService, parsingService, logger) { _radarrV3Proxy = radarrV3Proxy; } public override ImportListFetchResult Fetch() { var movies = new List(); var anyFailure = false; try { var remoteMovies = _radarrV3Proxy.GetMovies(Settings); foreach (var remoteMovie in remoteMovies) { if (Settings.ProfileIds.Any() && !Settings.ProfileIds.Contains(remoteMovie.QualityProfileId)) { continue; } if (Settings.TagIds.Any() && !Settings.TagIds.Any(x => remoteMovie.Tags.Any(y => y == x))) { continue; } if (Settings.RootFolderPaths.Any() && !Settings.RootFolderPaths.Any(rootFolderPath => remoteMovie.Path.ContainsIgnoreCase(rootFolderPath))) { continue; } movies.Add(new ImportListMovie { TmdbId = remoteMovie.TmdbId, Title = remoteMovie.Title, Year = remoteMovie.Year }); } _importListStatusService.RecordSuccess(Definition.Id); } catch { anyFailure = true; _importListStatusService.RecordFailure(Definition.Id); } return new ImportListFetchResult { Movies = CleanupListItems(movies), AnyFailure = anyFailure }; } public override object RequestAction(string action, IDictionary query) { // Return early if there is not an API key if (Settings.ApiKey.IsNullOrWhiteSpace()) { return new { options = new List() }; } Settings.Validate().Filter("ApiKey").ThrowOnError(); if (action == "getProfiles") { var profiles = _radarrV3Proxy.GetProfiles(Settings); if (profiles == null) { return new { options = new List() }; } return new { options = profiles.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) .Select(d => new { Value = d.Id, Name = d.Name }) }; } if (action == "getTags") { var tags = _radarrV3Proxy.GetTags(Settings); if (tags == null) { return new { options = new List() }; } return new { options = tags.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase) .Select(d => new { Value = d.Id, Name = d.Label }) }; } if (action == "getRootFolders") { var remoteRootFolders = _radarrV3Proxy.GetRootFolders(Settings); if (remoteRootFolders == null) { return new { options = new List() }; } return new { options = remoteRootFolders.OrderBy(d => d.Path, StringComparer.InvariantCultureIgnoreCase) .Select(d => new { Value = d.Path, Name = d.Path }) }; } return new { }; } protected override void Test(List 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; } } }