1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Indexers/IndexerService.cs

174 lines
5.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-02-21 08:07:34 +01:00
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
2013-05-29 02:44:29 +02:00
using NzbDrone.Common.Serializer;
2013-05-28 02:19:07 +02:00
using NzbDrone.Core.Indexers.Newznab;
2013-02-21 17:38:31 +01:00
using NzbDrone.Core.Lifecycle;
using Omu.ValueInjecter;
2013-03-02 19:25:39 +01:00
2013-02-21 08:07:34 +01:00
namespace NzbDrone.Core.Indexers
{
public class Indexer
{
2013-05-03 01:06:08 +02:00
public int Id { get; set; }
public string Name { get; set; }
public bool Enable { get; set; }
public IIndexerSetting Settings { get; set; }
public IIndexer Instance { get; set; }
2013-05-29 02:44:29 +02:00
public string Implementation { get; set; }
}
2013-02-21 08:07:34 +01:00
public interface IIndexerService
{
List<Indexer> All();
List<IIndexer> GetAvailableIndexers();
Indexer Get(int id);
Indexer Get(string name);
2013-05-28 02:19:07 +02:00
List<Indexer> Schema();
2013-05-29 02:44:29 +02:00
Indexer Create(Indexer indexer);
Indexer Update(Indexer indexer);
2013-06-27 09:37:39 +02:00
void Delete(int id);
2013-02-21 08:07:34 +01:00
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
2013-02-21 08:07:34 +01:00
{
private readonly IIndexerRepository _indexerRepository;
2013-02-21 17:38:31 +01:00
private readonly Logger _logger;
2013-02-21 08:07:34 +01:00
2013-05-03 06:37:08 +02:00
private readonly List<IIndexer> _indexers;
2013-02-21 08:07:34 +01:00
2013-05-03 06:37:08 +02:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexer> indexers, Logger logger)
2013-02-21 08:07:34 +01:00
{
_indexerRepository = indexerRepository;
2013-02-21 17:38:31 +01:00
_logger = logger;
2013-02-21 08:07:34 +01:00
_indexers = indexers.ToList();
2013-02-21 17:38:31 +01:00
}
public List<Indexer> All()
2013-02-21 08:07:34 +01:00
{
return _indexerRepository.All().Select(ToIndexer).ToList();
2013-02-21 08:07:34 +01:00
}
public List<IIndexer> GetAvailableIndexers()
2013-02-21 08:07:34 +01:00
{
2013-05-03 01:06:08 +02:00
return All().Where(c => c.Enable && c.Settings.IsValid).Select(c => c.Instance).ToList();
}
2013-04-07 09:30:37 +02:00
public Indexer Get(int id)
{
return ToIndexer(_indexerRepository.Get(id));
}
public Indexer Get(string name)
{
return ToIndexer(_indexerRepository.Get(name));
2013-02-21 08:07:34 +01:00
}
2013-05-28 02:19:07 +02:00
public List<Indexer> Schema()
{
var indexers = new List<Indexer>();
var newznab = new Indexer();
newznab.Instance = new Newznab.Newznab();
newznab.Id = 1;
newznab.Name = "Newznab";
newznab.Settings = new NewznabSettings();
2013-05-29 02:44:29 +02:00
newznab.Implementation = "Newznab";
2013-05-28 02:19:07 +02:00
indexers.Add(newznab);
2013-05-28 17:06:36 +02:00
return indexers;
2013-05-28 02:19:07 +02:00
}
2013-05-29 02:44:29 +02:00
public Indexer Create(Indexer indexer)
{
var definition = new IndexerDefinition
{
Name = indexer.Name,
Enable = indexer.Enable,
Implementation = indexer.Implementation,
Settings = indexer.Settings.ToJson()
2013-05-29 02:44:29 +02:00
};
definition = _indexerRepository.Insert(definition);
indexer.Id = definition.Id;
return indexer;
}
public Indexer Update(Indexer indexer)
{
var definition = _indexerRepository.Get(indexer.Id);
definition.InjectFrom(indexer);
definition.Settings = indexer.Settings.ToJson();
_indexerRepository.Update(definition);
return indexer;
}
2013-06-27 09:37:39 +02:00
public void Delete(int id)
{
_indexerRepository.Delete(id);
}
private Indexer ToIndexer(IndexerDefinition definition)
2013-02-21 08:07:34 +01:00
{
var indexer = new Indexer();
2013-05-03 01:06:08 +02:00
indexer.Id = definition.Id;
indexer.Enable = definition.Enable;
indexer.Instance = GetInstance(definition);
indexer.Name = definition.Name;
2013-05-29 02:44:29 +02:00
indexer.Implementation = definition.Implementation;
if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null)
{
indexer.Settings = ((dynamic)indexer.Instance).ImportSettingsFromJson(definition.Settings);
}
else
{
indexer.Settings = NullSetting.Instance;
}
return indexer;
2013-02-21 08:07:34 +01:00
}
private IIndexer GetInstance(IndexerDefinition indexerDefinition)
2013-02-21 08:07:34 +01:00
{
2013-06-28 02:05:09 +02:00
var type = GetImplementation(indexerDefinition);
2013-05-03 06:37:08 +02:00
var instance = (IIndexer)Activator.CreateInstance(type);
instance.InstanceDefinition = indexerDefinition;
return instance;
2013-02-21 08:07:34 +01:00
}
2013-06-28 02:05:09 +02:00
private Type GetImplementation(IndexerDefinition indexerDefinition)
{
return _indexers.Select(c => c.GetType()).SingleOrDefault(c => c.Name.Equals(indexerDefinition.Implementation, StringComparison.InvariantCultureIgnoreCase));
}
public void Handle(ApplicationStartedEvent message)
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
2013-06-28 02:05:09 +02:00
RemoveMissingImplementations();
if (!All().Any())
{
2013-05-03 06:37:08 +02:00
var definitions = _indexers.SelectMany(indexer => indexer.DefaultDefinitions);
_indexerRepository.InsertMany(definitions.ToList());
}
}
2013-06-28 02:05:09 +02:00
private void RemoveMissingImplementations()
{
var storedIndexers = _indexerRepository.All();
foreach (var indexerDefinition in storedIndexers.Where(i => GetImplementation(i) == null))
{
_logger.Debug("Removing Indexer {0} ", indexerDefinition.Name);
_indexerRepository.Delete(indexerDefinition);
}
}
2013-02-21 08:07:34 +01:00
}
}