2013-04-10 23:50:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-02-21 07:07:34 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-05-29 00:44:29 +00:00
|
|
|
|
using NzbDrone.Common.Serializer;
|
2013-05-28 00:19:07 +00:00
|
|
|
|
using NzbDrone.Core.Indexers.Newznab;
|
2013-02-21 16:38:31 +00:00
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
2013-05-31 01:03:37 +00:00
|
|
|
|
using Omu.ValueInjecter;
|
2013-03-02 18:25:39 +00:00
|
|
|
|
|
2013-02-21 07:07:34 +00:00
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
|
public class Indexer
|
|
|
|
|
{
|
2013-05-02 23:06:08 +00:00
|
|
|
|
public int Id { get; set; }
|
2013-05-02 01:59:09 +00:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public bool Enable { get; set; }
|
|
|
|
|
public IIndexerSetting Settings { get; set; }
|
|
|
|
|
public IIndexer Instance { get; set; }
|
2013-05-29 00:44:29 +00:00
|
|
|
|
public string Implementation { get; set; }
|
2013-05-02 01:59:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 07:07:34 +00:00
|
|
|
|
public interface IIndexerService
|
|
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
|
List<Indexer> All();
|
|
|
|
|
List<IIndexer> GetAvailableIndexers();
|
2013-05-31 01:03:37 +00:00
|
|
|
|
Indexer Get(int id);
|
2013-05-02 01:59:09 +00:00
|
|
|
|
Indexer Get(string name);
|
2013-05-28 00:19:07 +00:00
|
|
|
|
List<Indexer> Schema();
|
2013-05-29 00:44:29 +00:00
|
|
|
|
Indexer Create(Indexer indexer);
|
2013-05-31 01:03:37 +00:00
|
|
|
|
Indexer Update(Indexer indexer);
|
2013-06-27 07:37:39 +00:00
|
|
|
|
void Delete(int id);
|
2013-02-21 07:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
|
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IIndexerRepository _indexerRepository;
|
2013-02-21 16:38:31 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-02-21 07:07:34 +00:00
|
|
|
|
|
2013-05-03 04:37:08 +00:00
|
|
|
|
private readonly List<IIndexer> _indexers;
|
2013-02-21 07:07:34 +00:00
|
|
|
|
|
2013-05-03 04:37:08 +00:00
|
|
|
|
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexer> indexers, Logger logger)
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
|
|
|
|
_indexerRepository = indexerRepository;
|
2013-02-21 16:38:31 +00:00
|
|
|
|
_logger = logger;
|
2013-02-21 07:07:34 +00:00
|
|
|
|
_indexers = indexers.ToList();
|
2013-02-21 16:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
|
public List<Indexer> All()
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
|
return _indexerRepository.All().Select(ToIndexer).ToList();
|
2013-02-21 07:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
|
public List<IIndexer> GetAvailableIndexers()
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
2013-08-22 08:08:43 +00:00
|
|
|
|
return All().Where(c => c.Enable && c.Settings.Validate().IsValid).Select(c => c.Instance).ToList();
|
2013-05-02 01:59:09 +00:00
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
2013-05-31 01:03:37 +00:00
|
|
|
|
public Indexer Get(int id)
|
|
|
|
|
{
|
|
|
|
|
return ToIndexer(_indexerRepository.Get(id));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
|
public Indexer Get(string name)
|
|
|
|
|
{
|
|
|
|
|
return ToIndexer(_indexerRepository.Get(name));
|
2013-02-21 07:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-28 00:19:07 +00: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 00:44:29 +00:00
|
|
|
|
newznab.Implementation = "Newznab";
|
2013-05-28 00:19:07 +00:00
|
|
|
|
|
|
|
|
|
indexers.Add(newznab);
|
|
|
|
|
|
2013-05-28 15:06:36 +00:00
|
|
|
|
return indexers;
|
2013-05-28 00:19:07 +00:00
|
|
|
|
}
|
2013-05-02 01:59:09 +00:00
|
|
|
|
|
2013-05-29 00:44:29 +00:00
|
|
|
|
public Indexer Create(Indexer indexer)
|
|
|
|
|
{
|
|
|
|
|
var definition = new IndexerDefinition
|
|
|
|
|
{
|
|
|
|
|
Name = indexer.Name,
|
|
|
|
|
Enable = indexer.Enable,
|
|
|
|
|
Implementation = indexer.Implementation,
|
2013-05-29 21:29:51 +00:00
|
|
|
|
Settings = indexer.Settings.ToJson()
|
2013-05-29 00:44:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
definition = _indexerRepository.Insert(definition);
|
|
|
|
|
indexer.Id = definition.Id;
|
|
|
|
|
|
|
|
|
|
return indexer;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 01:03:37 +00:00
|
|
|
|
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 07:37:39 +00:00
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
_indexerRepository.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
|
private Indexer ToIndexer(IndexerDefinition definition)
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
|
var indexer = new Indexer();
|
2013-05-02 23:06:08 +00:00
|
|
|
|
indexer.Id = definition.Id;
|
2013-05-02 01:59:09 +00:00
|
|
|
|
indexer.Enable = definition.Enable;
|
|
|
|
|
indexer.Instance = GetInstance(definition);
|
|
|
|
|
indexer.Name = definition.Name;
|
2013-05-29 00:44:29 +00:00
|
|
|
|
indexer.Implementation = definition.Implementation;
|
2013-05-02 01:59:09 +00:00
|
|
|
|
|
|
|
|
|
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 07:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
|
private IIndexer GetInstance(IndexerDefinition indexerDefinition)
|
2013-02-21 07:07:34 +00:00
|
|
|
|
{
|
2013-06-28 00:05:09 +00:00
|
|
|
|
var type = GetImplementation(indexerDefinition);
|
2013-05-03 04:37:08 +00:00
|
|
|
|
var instance = (IIndexer)Activator.CreateInstance(type);
|
2013-05-02 01:59:09 +00:00
|
|
|
|
instance.InstanceDefinition = indexerDefinition;
|
|
|
|
|
return instance;
|
2013-02-21 07:07:34 +00:00
|
|
|
|
}
|
2013-04-10 00:47:04 +00:00
|
|
|
|
|
2013-06-28 00:05:09 +00:00
|
|
|
|
private Type GetImplementation(IndexerDefinition indexerDefinition)
|
|
|
|
|
{
|
|
|
|
|
return _indexers.Select(c => c.GetType()).SingleOrDefault(c => c.Name.Equals(indexerDefinition.Implementation, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
|
|
|
|
|
|
2013-06-28 00:05:09 +00:00
|
|
|
|
RemoveMissingImplementations();
|
|
|
|
|
|
2013-09-13 23:17:58 +00:00
|
|
|
|
var definitions = _indexers.SelectMany(indexer => indexer.DefaultDefinitions);
|
|
|
|
|
|
|
|
|
|
var currentIndexer = All();
|
|
|
|
|
|
|
|
|
|
var newIndexers = definitions.Where(def => currentIndexer.All(c => c.Implementation != def.Implementation)).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newIndexers.Any())
|
2013-04-10 00:47:04 +00:00
|
|
|
|
{
|
2013-09-13 23:17:58 +00:00
|
|
|
|
_indexerRepository.InsertMany(newIndexers);
|
2013-04-10 00:47:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 00:05:09 +00: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 07:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|