You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
9.9 KiB
216 lines
9.9 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using FluentValidation.Results;
|
|
using Newtonsoft.Json.Linq;
|
|
using NLog;
|
|
using NzbDrone.Common.Cache;
|
|
using NzbDrone.Common.Extensions;
|
|
using NzbDrone.Core.Configuration;
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
namespace NzbDrone.Core.Applications.Radarr
|
|
{
|
|
public class Radarr : ApplicationBase<RadarrSettings>
|
|
{
|
|
public override string Name => "Radarr";
|
|
|
|
private readonly IRadarrV3Proxy _radarrV3Proxy;
|
|
private readonly ICached<List<RadarrIndexer>> _schemaCache;
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
public Radarr(ICacheManager cacheManager, IRadarrV3Proxy radarrV3Proxy, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger)
|
|
: base(appIndexerMapService, logger)
|
|
{
|
|
_schemaCache = cacheManager.GetCache<List<RadarrIndexer>>(GetType());
|
|
_radarrV3Proxy = radarrV3Proxy;
|
|
_configFileProvider = configFileProvider;
|
|
}
|
|
|
|
public override ValidationResult Test()
|
|
{
|
|
var failures = new List<ValidationFailure>();
|
|
|
|
var testIndexer = new IndexerDefinition
|
|
{
|
|
Id = 0,
|
|
Name = "Test",
|
|
Protocol = DownloadProtocol.Usenet,
|
|
Capabilities = new IndexerCapabilities()
|
|
};
|
|
|
|
foreach (var cat in NewznabStandardCategory.AllCats)
|
|
{
|
|
testIndexer.Capabilities.Categories.AddCategoryMapping(1, cat);
|
|
}
|
|
|
|
try
|
|
{
|
|
failures.AddIfNotNull(_radarrV3Proxy.TestConnection(BuildRadarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
_logger.Error(ex, "Unable to send test message");
|
|
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Radarr"));
|
|
}
|
|
|
|
return new ValidationResult(failures);
|
|
}
|
|
|
|
public override List<AppIndexerMap> GetIndexerMappings()
|
|
{
|
|
var indexers = _radarrV3Proxy.GetIndexers(Settings)
|
|
.Where(i => i.Implementation == "Newznab" || i.Implementation == "Torznab");
|
|
|
|
var mappings = new List<AppIndexerMap>();
|
|
|
|
foreach (var indexer in indexers)
|
|
{
|
|
if ((string)indexer.Fields.FirstOrDefault(x => x.Name == "apiKey")?.Value == _configFileProvider.ApiKey)
|
|
{
|
|
var match = AppIndexerRegex.Match((string)indexer.Fields.FirstOrDefault(x => x.Name == "baseUrl").Value);
|
|
|
|
if (match.Groups["indexer"].Success && int.TryParse(match.Groups["indexer"].Value, out var indexerId))
|
|
{
|
|
// Add parsed mapping if it's mapped to a Indexer in this Prowlarr instance
|
|
mappings.Add(new AppIndexerMap { IndexerId = indexerId, RemoteIndexerId = indexer.Id });
|
|
}
|
|
}
|
|
}
|
|
|
|
return mappings;
|
|
}
|
|
|
|
public override void AddIndexer(IndexerDefinition indexer)
|
|
{
|
|
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Empty())
|
|
{
|
|
_logger.Trace("Skipping add for indexer {0} [{1}] due to no app Sync Categories supported by the indexer", indexer.Name, indexer.Id);
|
|
|
|
return;
|
|
}
|
|
|
|
_logger.Trace("Adding indexer {0} [{1}]", indexer.Name, indexer.Id);
|
|
|
|
var radarrIndexer = BuildRadarrIndexer(indexer, indexer.Protocol);
|
|
|
|
var remoteIndexer = _radarrV3Proxy.AddIndexer(radarrIndexer, Settings);
|
|
_appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = indexer.Id, RemoteIndexerId = remoteIndexer.Id });
|
|
}
|
|
|
|
public override void RemoveIndexer(int indexerId)
|
|
{
|
|
var appMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id);
|
|
|
|
var indexerMapping = appMappings.FirstOrDefault(m => m.IndexerId == indexerId);
|
|
|
|
if (indexerMapping != null)
|
|
{
|
|
//Remove Indexer remotely and then remove the mapping
|
|
_radarrV3Proxy.RemoveIndexer(indexerMapping.RemoteIndexerId, Settings);
|
|
_appIndexerMapService.Delete(indexerMapping.Id);
|
|
}
|
|
}
|
|
|
|
public override void UpdateIndexer(IndexerDefinition indexer)
|
|
{
|
|
_logger.Debug("Updating indexer {0} [{1}]", indexer.Name, indexer.Id);
|
|
|
|
var appMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id);
|
|
var indexerMapping = appMappings.FirstOrDefault(m => m.IndexerId == indexer.Id);
|
|
|
|
var radarrIndexer = BuildRadarrIndexer(indexer, indexer.Protocol, indexerMapping?.RemoteIndexerId ?? 0);
|
|
|
|
var remoteIndexer = _radarrV3Proxy.GetIndexer(indexerMapping.RemoteIndexerId, Settings);
|
|
|
|
if (remoteIndexer != null)
|
|
{
|
|
_logger.Debug("Remote indexer found, syncing with current settings");
|
|
|
|
if (!radarrIndexer.Equals(remoteIndexer))
|
|
{
|
|
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
|
{
|
|
// Retain user fields not-affiliated with Prowlarr
|
|
radarrIndexer.Fields.AddRange(remoteIndexer.Fields.Where(f => radarrIndexer.Fields.All(s => s.Name != f.Name)));
|
|
|
|
// Retain user tags not-affiliated with Prowlarr
|
|
radarrIndexer.Tags.UnionWith(remoteIndexer.Tags);
|
|
|
|
// Retain user settings not-affiliated with Prowlarr
|
|
radarrIndexer.DownloadClientId = remoteIndexer.DownloadClientId;
|
|
|
|
// Update the indexer if it still has categories that match
|
|
_radarrV3Proxy.UpdateIndexer(radarrIndexer, Settings);
|
|
}
|
|
else
|
|
{
|
|
// Else remove it, it no longer should be used
|
|
_radarrV3Proxy.RemoveIndexer(remoteIndexer.Id, Settings);
|
|
_appIndexerMapService.Delete(indexerMapping.Id);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_appIndexerMapService.Delete(indexerMapping.Id);
|
|
|
|
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
|
{
|
|
_logger.Debug("Remote indexer not found, re-adding {0} [{1}] to Radarr", indexer.Name, indexer.Id);
|
|
radarrIndexer.Id = 0;
|
|
var newRemoteIndexer = _radarrV3Proxy.AddIndexer(radarrIndexer, Settings);
|
|
_appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = indexer.Id, RemoteIndexerId = newRemoteIndexer.Id });
|
|
}
|
|
else
|
|
{
|
|
_logger.Debug("Remote indexer not found for {0} [{1}], skipping re-add to Radarr due to indexer capabilities", indexer.Name, indexer.Id);
|
|
}
|
|
}
|
|
}
|
|
|
|
private RadarrIndexer BuildRadarrIndexer(IndexerDefinition indexer, DownloadProtocol protocol, int id = 0)
|
|
{
|
|
var cacheKey = $"{Settings.BaseUrl}";
|
|
var schemas = _schemaCache.Get(cacheKey, () => _radarrV3Proxy.GetIndexerSchema(Settings), TimeSpan.FromDays(7));
|
|
var syncFields = new[] { "baseUrl", "apiPath", "apiKey", "categories", "minimumSeeders", "seedCriteria.seedRatio", "seedCriteria.seedTime" };
|
|
|
|
var newznab = schemas.First(i => i.Implementation == "Newznab");
|
|
var torznab = schemas.First(i => i.Implementation == "Torznab");
|
|
|
|
var schema = protocol == DownloadProtocol.Usenet ? newznab : torznab;
|
|
|
|
var radarrIndexer = new RadarrIndexer
|
|
{
|
|
Id = id,
|
|
Name = $"{indexer.Name} (Prowlarr)",
|
|
EnableRss = indexer.Enable && indexer.AppProfile.Value.EnableRss,
|
|
EnableAutomaticSearch = indexer.Enable && indexer.AppProfile.Value.EnableAutomaticSearch,
|
|
EnableInteractiveSearch = indexer.Enable && indexer.AppProfile.Value.EnableInteractiveSearch,
|
|
Priority = indexer.Priority,
|
|
Implementation = indexer.Protocol == DownloadProtocol.Usenet ? "Newznab" : "Torznab",
|
|
ConfigContract = schema.ConfigContract,
|
|
Fields = new List<RadarrField>(),
|
|
Tags = new HashSet<int>()
|
|
};
|
|
|
|
radarrIndexer.Fields.AddRange(schema.Fields.Where(x => syncFields.Contains(x.Name)));
|
|
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "baseUrl").Value = $"{Settings.ProwlarrUrl.TrimEnd('/')}/{indexer.Id}/";
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "apiPath").Value = "/api";
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "apiKey").Value = _configFileProvider.ApiKey;
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "categories").Value = JArray.FromObject(indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()));
|
|
|
|
if (indexer.Protocol == DownloadProtocol.Torrent)
|
|
{
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "minimumSeeders").Value = ((ITorrentIndexerSettings)indexer.Settings).TorrentBaseSettings.AppMinimumSeeders ?? indexer.AppProfile.Value.MinimumSeeders;
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "seedCriteria.seedRatio").Value = ((ITorrentIndexerSettings)indexer.Settings).TorrentBaseSettings.SeedRatio;
|
|
radarrIndexer.Fields.FirstOrDefault(x => x.Name == "seedCriteria.seedTime").Value = ((ITorrentIndexerSettings)indexer.Settings).TorrentBaseSettings.SeedTime;
|
|
}
|
|
|
|
return radarrIndexer;
|
|
}
|
|
}
|
|
}
|