mirror of
https://github.com/Prowlarr/Prowlarr
synced 2026-01-02 22:06:28 +01:00
New: Detect and map App Indexers setup outside of Prowlarr
This commit is contained in:
parent
66237af562
commit
6e46cd09c1
11 changed files with 120 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Indexers;
|
||||
|
|
@ -13,6 +14,9 @@ public abstract class ApplicationBase<TSettings> : IApplication
|
|||
protected readonly IAppIndexerMapService _appIndexerMapService;
|
||||
protected readonly Logger _logger;
|
||||
|
||||
protected static readonly Regex AppIndexerRegex = new Regex(@"api\/v\d*\/indexer\/(?<indexer>\d*)",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
public abstract string Name { get; }
|
||||
|
||||
public Type ConfigContract => typeof(TSettings);
|
||||
|
|
@ -54,6 +58,7 @@ public virtual IEnumerable<ProviderDefinition> DefaultDefinitions
|
|||
public abstract void AddIndexer(IndexerDefinition indexer);
|
||||
public abstract void UpdateIndexer(IndexerDefinition indexer);
|
||||
public abstract void RemoveIndexer(int indexerId);
|
||||
public abstract Dictionary<int, int> GetIndexerMappings();
|
||||
|
||||
public virtual object RequestAction(string action, IDictionary<string, string> query)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -111,6 +111,23 @@ private void SyncIndexers(List<IApplication> applications, List<IndexerDefinitio
|
|||
{
|
||||
var indexerMappings = _appIndexerMapService.GetMappingsForApp(app.Definition.Id);
|
||||
|
||||
//Remote-Local mappings currently stored by Prowlarr
|
||||
var prowlarrMappings = indexerMappings.ToDictionary(i => i.RemoteIndexerId, i => i.IndexerId);
|
||||
|
||||
//Get Dictionary of Remote Indexers point to Prowlarr and what they are mapped to
|
||||
var remoteMappings = app.GetIndexerMappings();
|
||||
|
||||
//Add mappings if not already in db, these were setup manually in the app or orphaned by a table wipe
|
||||
foreach (var mapping in remoteMappings)
|
||||
{
|
||||
if (!prowlarrMappings.ContainsKey(mapping.Key))
|
||||
{
|
||||
var addMapping = new AppIndexerMap { AppId = app.Definition.Id, RemoteIndexerId = mapping.Key, IndexerId = mapping.Value };
|
||||
_appIndexerMapService.Insert(addMapping);
|
||||
indexerMappings.Add(addMapping);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var indexer in indexers)
|
||||
{
|
||||
var definition = indexer;
|
||||
|
|
|
|||
|
|
@ -9,5 +9,6 @@ public interface IApplication : IProvider
|
|||
void AddIndexer(IndexerDefinition indexer);
|
||||
void UpdateIndexer(IndexerDefinition indexer);
|
||||
void RemoveIndexer(int indexerId);
|
||||
Dictionary<int, int> GetIndexerMappings();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,28 @@ public override ValidationResult Test()
|
|||
return new ValidationResult(failures);
|
||||
}
|
||||
|
||||
public override Dictionary<int, int> GetIndexerMappings()
|
||||
{
|
||||
var indexers = _lidarrV1Proxy.GetIndexers(Settings);
|
||||
var mappings = new Dictionary<int, int>();
|
||||
|
||||
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(indexer.Id, indexerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public override void AddIndexer(IndexerDefinition indexer)
|
||||
{
|
||||
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
||||
|
|
@ -69,7 +91,7 @@ public override void UpdateIndexer(IndexerDefinition indexer)
|
|||
var appMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id);
|
||||
var indexerMapping = appMappings.FirstOrDefault(m => m.IndexerId == indexer.Id);
|
||||
|
||||
var readarrIndexer = BuildLidarrIndexer(indexer, indexer.Protocol, indexerMapping?.RemoteIndexerId ?? 0);
|
||||
var lidarrIndexer = BuildLidarrIndexer(indexer, indexer.Protocol, indexerMapping?.RemoteIndexerId ?? 0);
|
||||
|
||||
var remoteIndexer = _lidarrV1Proxy.GetIndexer(indexerMapping.RemoteIndexerId, Settings);
|
||||
|
||||
|
|
@ -77,17 +99,17 @@ public override void UpdateIndexer(IndexerDefinition indexer)
|
|||
{
|
||||
_logger.Debug("Remote indexer found, syncing with current settings");
|
||||
|
||||
if (!readarrIndexer.Equals(remoteIndexer))
|
||||
if (!lidarrIndexer.Equals(remoteIndexer))
|
||||
{
|
||||
_lidarrV1Proxy.UpdateIndexer(readarrIndexer, Settings);
|
||||
_lidarrV1Proxy.UpdateIndexer(lidarrIndexer, Settings);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Debug("Remote indexer not found, re-adding indexer to Lidarr");
|
||||
readarrIndexer.Id = 0;
|
||||
lidarrIndexer.Id = 0;
|
||||
|
||||
var newRemoteIndexer = _lidarrV1Proxy.AddIndexer(readarrIndexer, Settings);
|
||||
var newRemoteIndexer = _lidarrV1Proxy.AddIndexer(lidarrIndexer, Settings);
|
||||
_appIndexerMapService.Delete(indexerMapping.Id);
|
||||
_appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = indexer.Id, RemoteIndexerId = newRemoteIndexer.Id });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ public LidarrIndexer GetIndexer(int indexerId, LidarrSettings settings)
|
|||
public void RemoveIndexer(int indexerId, LidarrSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings, $"/api/v1/indexer/{indexerId}", HttpMethod.DELETE);
|
||||
var response = _httpClient.Execute(request);
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
|
||||
public List<LidarrIndexer> GetIndexerSchema(LidarrSettings settings)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,28 @@ public override ValidationResult Test()
|
|||
return new ValidationResult(failures);
|
||||
}
|
||||
|
||||
public override Dictionary<int, int> GetIndexerMappings()
|
||||
{
|
||||
var indexers = _radarrV3Proxy.GetIndexers(Settings);
|
||||
var mappings = new Dictionary<int, int>();
|
||||
|
||||
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(indexer.Id, indexerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public override void AddIndexer(IndexerDefinition indexer)
|
||||
{
|
||||
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ public RadarrIndexer GetIndexer(int indexerId, RadarrSettings settings)
|
|||
public void RemoveIndexer(int indexerId, RadarrSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings, $"/api/v3/indexer/{indexerId}", HttpMethod.DELETE);
|
||||
var response = _httpClient.Execute(request);
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
|
||||
public List<RadarrIndexer> GetIndexerSchema(RadarrSettings settings)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,28 @@ public override ValidationResult Test()
|
|||
return new ValidationResult(failures);
|
||||
}
|
||||
|
||||
public override Dictionary<int, int> GetIndexerMappings()
|
||||
{
|
||||
var indexers = _readarrV1Proxy.GetIndexers(Settings);
|
||||
var mappings = new Dictionary<int, int>();
|
||||
|
||||
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(indexer.Id, indexerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public override void AddIndexer(IndexerDefinition indexer)
|
||||
{
|
||||
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ public ReadarrIndexer GetIndexer(int indexerId, ReadarrSettings settings)
|
|||
public void RemoveIndexer(int indexerId, ReadarrSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings, $"/api/v1/indexer/{indexerId}", HttpMethod.DELETE);
|
||||
var response = _httpClient.Execute(request);
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
|
||||
public List<ReadarrIndexer> GetIndexerSchema(ReadarrSettings settings)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,28 @@ public override ValidationResult Test()
|
|||
return new ValidationResult(failures);
|
||||
}
|
||||
|
||||
public override Dictionary<int, int> GetIndexerMappings()
|
||||
{
|
||||
var indexers = _sonarrV3Proxy.GetIndexers(Settings);
|
||||
var mappings = new Dictionary<int, int>();
|
||||
|
||||
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(indexer.Id, indexerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public override void AddIndexer(IndexerDefinition indexer)
|
||||
{
|
||||
if (indexer.Capabilities.Categories.SupportedCategories(Settings.SyncCategories.ToArray()).Any())
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ public SonarrIndexer GetIndexer(int indexerId, SonarrSettings settings)
|
|||
public void RemoveIndexer(int indexerId, SonarrSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings, $"/api/v3/indexer/{indexerId}", HttpMethod.DELETE);
|
||||
var response = _httpClient.Execute(request);
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
|
||||
public List<SonarrIndexer> GetIndexerSchema(SonarrSettings settings)
|
||||
|
|
|
|||
Loading…
Reference in a new issue