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

76 lines
2.2 KiB
C#
Raw Normal View History

2013-04-07 09:30:37 +02:00
using System.Collections.Generic;
2013-02-21 08:07:34 +01:00
using System.Linq;
using NLog;
2013-02-21 17:38:31 +01:00
using NzbDrone.Core.Lifecycle;
2013-03-02 19:25:39 +01:00
2013-02-21 08:07:34 +01:00
namespace NzbDrone.Core.Indexers
{
public interface IIndexerService
{
2013-04-07 09:30:37 +02:00
List<IndexerDefinition> All();
List<IIndexerBase> GetAvailableIndexers();
void Save(IndexerDefinition indexer);
IndexerDefinition Get(string name);
2013-02-21 08:07:34 +01:00
}
2013-02-21 17:38:31 +01:00
public class IndexerService : IIndexerService, IInitializable
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-04-07 09:30:37 +02:00
private readonly IList<IIndexerBase> _indexers;
2013-02-21 08:07:34 +01:00
2013-04-07 09:30:37 +02:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexerBase> 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 void Init()
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
var currentIndexers = All();
foreach (var feedProvider in _indexers)
{
2013-04-07 09:30:37 +02:00
IIndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.Name == indexerLocal.Name))
2013-02-21 17:38:31 +01:00
{
2013-04-07 09:30:37 +02:00
var settings = new IndexerDefinition
2013-02-21 17:38:31 +01:00
{
Enable = indexerLocal.EnabledByDefault,
2013-04-07 09:30:37 +02:00
Name = indexerLocal.Name.ToLower()
2013-02-21 17:38:31 +01:00
};
_indexerRepository.Insert(settings);
}
}
2013-02-21 08:07:34 +01:00
}
2013-04-07 09:30:37 +02:00
public List<IndexerDefinition> All()
2013-02-21 08:07:34 +01:00
{
return _indexerRepository.All().ToList();
}
2013-04-07 09:30:37 +02:00
public List<IIndexerBase> GetAvailableIndexers()
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
var enabled = All().Where(c => c.Enable).Select(c => c.Name);
var configureIndexers = _indexers.Where(c => c.Settings.IsValid);
return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList();
2013-02-21 08:07:34 +01:00
}
2013-04-07 09:30:37 +02:00
public void Save(IndexerDefinition indexer)
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
_indexerRepository.Update(indexer);
2013-02-21 08:07:34 +01:00
}
2013-04-07 09:30:37 +02:00
public IndexerDefinition Get(string name)
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
return _indexerRepository.Get(name);
2013-02-21 08:07:34 +01:00
}
}
}