using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Composition; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Indexers { public interface IIndexerFactory : IProviderFactory { List RssEnabled(bool filterBlockedIndexers = true); List SearchEnabled(bool filterBlockedIndexers = true); } public class IndexerFactory : ProviderFactory, IIndexerFactory { private readonly IIndexerStatusService _indexerStatusService; private readonly Logger _logger; public IndexerFactory(IIndexerStatusService indexerStatusService, IIndexerRepository providerRepository, IEnumerable providers, IContainer container, IEventAggregator eventAggregator, Logger logger) : base(providerRepository, providers, container, eventAggregator, logger) { _indexerStatusService = indexerStatusService; _logger = logger; } protected override List Active() { return base.Active().Where(c => c.Enable).ToList(); } public override void SetProviderCharacteristics(IIndexer provider, IndexerDefinition definition) { base.SetProviderCharacteristics(provider, definition); definition.Protocol = provider.Protocol; definition.SupportsRss = provider.SupportsRss; definition.SupportsSearch = provider.SupportsSearch; } public List RssEnabled(bool filterBlockedIndexers = true) { var enabledIndexers = GetAvailableProviders().Where(n => ((IndexerDefinition)n.Definition).EnableRss); if (filterBlockedIndexers) { return FilterBlockedIndexers(enabledIndexers).ToList(); } return enabledIndexers.ToList(); } public List SearchEnabled(bool filterBlockedIndexers = true) { var enabledIndexers = GetAvailableProviders().Where(n => ((IndexerDefinition)n.Definition).EnableSearch); if (filterBlockedIndexers) { return FilterBlockedIndexers(enabledIndexers).ToList(); } return enabledIndexers.ToList(); } private IEnumerable FilterBlockedIndexers(IEnumerable indexers) { var blockedIndexers = _indexerStatusService.GetBlockedIndexers().ToDictionary(v => v.IndexerId, v => v); foreach (var indexer in indexers) { IndexerStatus blockedIndexerStatus; if (blockedIndexers.TryGetValue(indexer.Definition.Id, out blockedIndexerStatus)) { _logger.Debug("Temporarily ignoring indexer {0} till {1} due to recent failures.", indexer.Definition.Name, blockedIndexerStatus.DisabledTill.Value.ToLocalTime()); continue; } yield return indexer; } } } }