From 96990eabb3894e1dcdb43fb56d5d9d0e06c20682 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Wed, 1 May 2013 18:59:09 -0700 Subject: [PATCH] indexers implementation is now separated from settings/definition so we can have multiple newznab definitions. --- NzbDrone.App.Test/ContainerFixture.cs | 2 +- .../IndexerTests/IndexerServiceFixture.cs | 73 +++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + .../Datastore/Migration/Migration20130324.cs | 7 +- NzbDrone.Core/Datastore/TableMapping.cs | 2 - .../IndexerSearch/NzbSearchService.cs | 4 +- .../Indexers/FetchAndParseRssService.cs | 2 +- .../Indexers/{IIndexerBase.cs => IIndexer.cs} | 9 +- NzbDrone.Core/Indexers/IIndexerSettings.cs | 7 + .../Indexers/{Indexer.cs => IndexerBase.cs} | 22 ++- NzbDrone.Core/Indexers/IndexerDefinition.cs | 1 + NzbDrone.Core/Indexers/IndexerFetchService.cs | 22 +-- NzbDrone.Core/Indexers/IndexerService.cs | 82 +++++++---- .../Indexers/IndexerSettingProvider.cs | 4 +- NzbDrone.Core/Indexers/IndexerWithSetting.cs | 34 +---- NzbDrone.Core/Indexers/Newznab/Newznab.cs | 86 +++++++---- .../Indexers/Newznab/NewznabDefinition.cs | 13 -- .../Indexers/Newznab/NewznabParser.cs | 4 +- .../Indexers/Newznab/NewznabRepository.cs | 23 --- .../Indexers/Newznab/NewznabService.cs | 138 ------------------ .../Indexers/Newznab/NewznabSettings.cs | 18 +++ NzbDrone.Core/Indexers/NzbClub/NzbClub.cs | 14 +- NzbDrone.Core/Indexers/NzbIndex/NzbIndex.cs | 8 +- NzbDrone.Core/Indexers/NzbsRUs/NzbsRUs.cs | 11 +- NzbDrone.Core/Indexers/Nzbx/Nzbx.cs | 2 +- .../Indexers/Omgwtfnzbs/Omgwtfnzbs.cs | 8 +- NzbDrone.Core/Indexers/Wombles/Wombles.cs | 8 +- NzbDrone.Core/NzbDrone.Core.csproj | 8 +- 28 files changed, 269 insertions(+), 344 deletions(-) create mode 100644 NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs rename NzbDrone.Core/Indexers/{IIndexerBase.cs => IIndexer.cs} (79%) rename NzbDrone.Core/Indexers/{Indexer.cs => IndexerBase.cs} (66%) delete mode 100644 NzbDrone.Core/Indexers/Newznab/NewznabDefinition.cs delete mode 100644 NzbDrone.Core/Indexers/Newznab/NewznabRepository.cs delete mode 100644 NzbDrone.Core/Indexers/Newznab/NewznabService.cs create mode 100644 NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs diff --git a/NzbDrone.App.Test/ContainerFixture.cs b/NzbDrone.App.Test/ContainerFixture.cs index dc8b4bec6..3893a898f 100644 --- a/NzbDrone.App.Test/ContainerFixture.cs +++ b/NzbDrone.App.Test/ContainerFixture.cs @@ -20,7 +20,7 @@ namespace NzbDrone.App.Test [Test] public void should_be_able_to_resolve_indexers() { - MainAppContainerBuilder.BuildContainer().Resolve>().Should().NotBeEmpty(); + MainAppContainerBuilder.BuildContainer().Resolve>().Should().NotBeEmpty(); } [Test] diff --git a/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs b/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs new file mode 100644 index 000000000..f2a080c8d --- /dev/null +++ b/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.Indexers.Newznab; +using NzbDrone.Core.Indexers.NzbClub; +using NzbDrone.Core.Indexers.NzbIndex; +using NzbDrone.Core.Indexers.NzbsRUs; +using NzbDrone.Core.Indexers.Omgwtfnzbs; +using NzbDrone.Core.Indexers.Wombles; +using NzbDrone.Core.Lifecycle; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.IndexerTests +{ + public class IndexerServiceFixture : DbTest + { + private List _indexers; + + [SetUp] + public void Setup() + { + _indexers = new List(); + + _indexers.Add(new Newznab()); + _indexers.Add(new Nzbsrus()); + _indexers.Add(new NzbClub()); + _indexers.Add(new NzbIndex()); + _indexers.Add(new Omgwtfnzbs()); + _indexers.Add(new Wombles()); + + Mocker.SetConstant>(_indexers); + + } + + [Test] + public void should_create_default_indexer_on_startup() + { + IList storedIndexers = null; + + Mocker.GetMock() + .Setup(c => c.InsertMany(It.IsAny>())) + .Callback>(indexers => storedIndexers = indexers); + + Subject.Handle(new ApplicationStartedEvent()); + + storedIndexers.Should().NotBeEmpty(); + storedIndexers.Select(c => c.Name).Should().OnlyHaveUniqueItems(); + storedIndexers.Select(c => c.Enable).Should().NotBeEmpty(); + storedIndexers.Select(c => c.Implementation).Should().NotContainNulls(); + } + + [Test] + public void getting_list_of_indexers() + { + Mocker.SetConstant(Mocker.Resolve()); + + Subject.Handle(new ApplicationStartedEvent()); + + var indexers = Subject.All().ToList(); + indexers.Should().NotBeEmpty(); + indexers.Should().NotContain(c => c.Settings == null); + indexers.Should().NotContain(c => c.Instance == null); + indexers.Should().NotContain(c => c.Name == null); + indexers.Select(c => c.Name).Should().OnlyHaveUniqueItems(); + indexers.Select(c => c.Instance).Should().OnlyHaveUniqueItems(); + + } + + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 313744e89..46d5ab977 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -132,6 +132,7 @@ + diff --git a/NzbDrone.Core/Datastore/Migration/Migration20130324.cs b/NzbDrone.Core/Datastore/Migration/Migration20130324.cs index 080053575..440a110d3 100644 --- a/NzbDrone.Core/Datastore/Migration/Migration20130324.cs +++ b/NzbDrone.Core/Datastore/Migration/Migration20130324.cs @@ -102,14 +102,9 @@ namespace NzbDrone.Core.Datastore.Migration Create.TableForModel("IndexerDefinitions") .WithColumn("Enable").AsBoolean() .WithColumn("Name").AsString().Unique() + .WithColumn("Implementation").AsString() .WithColumn("Settings").AsString().Nullable(); - Create.TableForModel("NewznabDefinitions") - .WithColumn("Enable").AsBoolean() - .WithColumn("Name").AsString().Unique() - .WithColumn("Url").AsString() - .WithColumn("ApiKey").AsString().Nullable(); - Create.TableForModel("QualityProfiles") .WithColumn("Name").AsString().Unique() .WithColumn("Cutoff").AsInt32() diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index cc8e27e91..018ee4198 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -9,7 +9,6 @@ using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.Datastore.Converters; using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Indexers; -using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Jobs; using NzbDrone.Core.MediaFiles; @@ -34,7 +33,6 @@ namespace NzbDrone.Core.Datastore Mapper.Entity().RegisterModel("RootFolders").Ignore(r => r.FreeSpace); Mapper.Entity().RegisterModel("IndexerDefinitions"); - Mapper.Entity().RegisterModel("NewznabDefinitions"); Mapper.Entity().RegisterModel("JobDefinitions"); Mapper.Entity().RegisterModel("ExternalNotificationDefinitions"); diff --git a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs index f69c0b39b..09ea801c2 100644 --- a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs +++ b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs @@ -109,9 +109,9 @@ namespace NzbDrone.Core.IndexerSearch return spec; } - private List Dispatch(Func> searchAction, SearchDefinitionBase definitionBase) + private List Dispatch(Func> searchAction, SearchDefinitionBase definitionBase) { - var indexers = _indexerService.GetAvailableIndexers(); + var indexers = _indexerService.GetAvailableIndexers().ToList(); var reports = new List(); Parallel.ForEach(indexers, indexer => diff --git a/NzbDrone.Core/Indexers/FetchAndParseRssService.cs b/NzbDrone.Core/Indexers/FetchAndParseRssService.cs index 7e727c56c..686e370e3 100644 --- a/NzbDrone.Core/Indexers/FetchAndParseRssService.cs +++ b/NzbDrone.Core/Indexers/FetchAndParseRssService.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Core.Indexers { var result = new List(); - var indexers = _indexerService.GetAvailableIndexers(); + var indexers = _indexerService.GetAvailableIndexers().ToList(); if (!indexers.Any()) { diff --git a/NzbDrone.Core/Indexers/IIndexerBase.cs b/NzbDrone.Core/Indexers/IIndexer.cs similarity index 79% rename from NzbDrone.Core/Indexers/IIndexerBase.cs rename to NzbDrone.Core/Indexers/IIndexer.cs index 6e3b5ec56..bed871a3e 100644 --- a/NzbDrone.Core/Indexers/IIndexerBase.cs +++ b/NzbDrone.Core/Indexers/IIndexer.cs @@ -3,17 +3,18 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers { - public interface IIndexerBase + public interface IIndexer { string Name { get; } - bool EnabledByDefault { get; } + + IEnumerable DefaultDefinitions { get; } + + IndexerDefinition InstanceDefinition { get; set; } IEnumerable RecentFeed { get; } IParseFeed Parser { get; } - bool IsConfigured { get; } - IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber); IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date); IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber); diff --git a/NzbDrone.Core/Indexers/IIndexerSettings.cs b/NzbDrone.Core/Indexers/IIndexerSettings.cs index c68ecf0ad..a979b3531 100644 --- a/NzbDrone.Core/Indexers/IIndexerSettings.cs +++ b/NzbDrone.Core/Indexers/IIndexerSettings.cs @@ -8,6 +8,13 @@ public class NullSetting : IIndexerSetting { + public static NullSetting Instance = new NullSetting(); + + private NullSetting() + { + + } + public bool IsValid { get diff --git a/NzbDrone.Core/Indexers/Indexer.cs b/NzbDrone.Core/Indexers/IndexerBase.cs similarity index 66% rename from NzbDrone.Core/Indexers/Indexer.cs rename to NzbDrone.Core/Indexers/IndexerBase.cs index 471fcfcdc..055663464 100644 --- a/NzbDrone.Core/Indexers/Indexer.cs +++ b/NzbDrone.Core/Indexers/IndexerBase.cs @@ -3,18 +3,23 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers { - public abstract class Indexer : IIndexerBase + public abstract class IndexerBase : IIndexer { - public abstract string Name { get; } + public IndexerDefinition InstanceDefinition { get; set; } - - public virtual bool EnabledByDefault + public virtual IEnumerable DefaultDefinitions { get { - return true; + yield return new IndexerDefinition + { + Name = Name, + Enable = true, + Implementation = GetType().Name, + Settings = string.Empty + }; } } @@ -26,13 +31,6 @@ namespace NzbDrone.Core.Indexers } } - public virtual bool IsConfigured - { - get { return true; } - } - - - public abstract IEnumerable RecentFeed { get; } public abstract IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber); diff --git a/NzbDrone.Core/Indexers/IndexerDefinition.cs b/NzbDrone.Core/Indexers/IndexerDefinition.cs index 398dd4bf1..a061052e7 100644 --- a/NzbDrone.Core/Indexers/IndexerDefinition.cs +++ b/NzbDrone.Core/Indexers/IndexerDefinition.cs @@ -8,5 +8,6 @@ namespace NzbDrone.Core.Indexers public Boolean Enable { get; set; } public String Name { get; set; } public String Settings { get; set; } + public String Implementation { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/IndexerFetchService.cs b/NzbDrone.Core/Indexers/IndexerFetchService.cs index e2e1291b1..a95d43a57 100644 --- a/NzbDrone.Core/Indexers/IndexerFetchService.cs +++ b/NzbDrone.Core/Indexers/IndexerFetchService.cs @@ -12,12 +12,12 @@ namespace NzbDrone.Core.Indexers { public interface IFetchFeedFromIndexers { - IList FetchRss(IIndexerBase indexer); + IList FetchRss(IIndexer indexer); - IList Fetch(IIndexerBase indexer, SeasonSearchDefinition searchDefinition); - IList Fetch(IIndexerBase indexer, SingleEpisodeSearchDefinition searchDefinition); - IList Fetch(IIndexerBase indexer, PartialSeasonSearchDefinition searchDefinition); - IList Fetch(IIndexerBase indexer, DailyEpisodeSearchDefinition searchDefinition); + IList Fetch(IIndexer indexer, SeasonSearchDefinition searchDefinition); + IList Fetch(IIndexer indexer, SingleEpisodeSearchDefinition searchDefinition); + IList Fetch(IIndexer indexer, PartialSeasonSearchDefinition searchDefinition); + IList Fetch(IIndexer indexer, DailyEpisodeSearchDefinition searchDefinition); } public class FetchFeedService : IFetchFeedFromIndexers @@ -33,7 +33,7 @@ namespace NzbDrone.Core.Indexers } - public virtual IList FetchRss(IIndexerBase indexer) + public virtual IList FetchRss(IIndexer indexer) { _logger.Debug("Fetching feeds from " + indexer.Name); @@ -44,7 +44,7 @@ namespace NzbDrone.Core.Indexers return result; } - public IList Fetch(IIndexerBase indexer, SeasonSearchDefinition searchDefinition) + public IList Fetch(IIndexer indexer, SeasonSearchDefinition searchDefinition) { _logger.Debug("Searching for {0}", searchDefinition); @@ -56,7 +56,7 @@ namespace NzbDrone.Core.Indexers return result; } - public IList Fetch(IIndexerBase indexer, SingleEpisodeSearchDefinition searchDefinition) + public IList Fetch(IIndexer indexer, SingleEpisodeSearchDefinition searchDefinition) { _logger.Debug("Searching for {0}", searchDefinition); @@ -69,7 +69,7 @@ namespace NzbDrone.Core.Indexers } - public IList Fetch(IIndexerBase indexer, PartialSeasonSearchDefinition searchDefinition) + public IList Fetch(IIndexer indexer, PartialSeasonSearchDefinition searchDefinition) { _logger.Debug("Searching for {0}", searchDefinition); @@ -81,7 +81,7 @@ namespace NzbDrone.Core.Indexers return result; } - public IList Fetch(IIndexerBase indexer, DailyEpisodeSearchDefinition searchDefinition) + public IList Fetch(IIndexer indexer, DailyEpisodeSearchDefinition searchDefinition) { _logger.Debug("Searching for {0}", searchDefinition); @@ -92,7 +92,7 @@ namespace NzbDrone.Core.Indexers return result; } - private List Fetch(IIndexerBase indexer, IEnumerable urls) + private List Fetch(IIndexer indexer, IEnumerable urls) { var result = new List(); diff --git a/NzbDrone.Core/Indexers/IndexerService.cs b/NzbDrone.Core/Indexers/IndexerService.cs index 71855ab00..6c425300c 100644 --- a/NzbDrone.Core/Indexers/IndexerService.cs +++ b/NzbDrone.Core/Indexers/IndexerService.cs @@ -8,12 +8,21 @@ using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Indexers { + + public class Indexer + { + public int DefinitionId { get; set; } + public string Name { get; set; } + public bool Enable { get; set; } + public IIndexerSetting Settings { get; set; } + public IIndexer Instance { get; set; } + } + public interface IIndexerService { - List All(); - List GetAvailableIndexers(); - void Save(IndexerDefinition indexer); - IndexerDefinition Get(string name); + List All(); + List GetAvailableIndexers(); + Indexer Get(string name); } public class IndexerService : IIndexerService, IHandle @@ -21,9 +30,9 @@ namespace NzbDrone.Core.Indexers private readonly IIndexerRepository _indexerRepository; private readonly Logger _logger; - private readonly IList _indexers; + private readonly IList _indexers; - public IndexerService(IIndexerRepository indexerRepository, IEnumerable indexers, Logger logger) + public IndexerService(IIndexerRepository indexerRepository, IEnumerable indexers, Logger logger) { _indexerRepository = indexerRepository; _logger = logger; @@ -31,48 +40,65 @@ namespace NzbDrone.Core.Indexers } - public List All() + public List All() { - return _indexerRepository.All().ToList(); + return _indexerRepository.All().Select(ToIndexer).ToList(); } - public List GetAvailableIndexers() + public List GetAvailableIndexers() { - var enabled = All().Where(c => c.Enable).Select(c => c.Name); - var configureIndexers = _indexers.Where(c => c.IsConfigured); + return All().Where(c => c.Enable && c.Settings.IsValid).Select(c=>c.Instance).ToList(); + } - return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList(); + + public Indexer Get(string name) + { + return ToIndexer(_indexerRepository.Get(name)); } - public void Save(IndexerDefinition indexer) + + private Indexer ToIndexer(IndexerDefinition definition) { - _indexerRepository.Update(indexer); + var indexer = new Indexer(); + indexer.DefinitionId = definition.Id; + indexer.Enable = definition.Enable; + indexer.Instance = GetInstance(definition); + indexer.Name = definition.Name; + + if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null) + { + indexer.Settings = ((dynamic)indexer.Instance).ImportSettingsFromJson(definition.Settings); + } + else + { + indexer.Settings = NullSetting.Instance; + } + + + return indexer; } - public IndexerDefinition Get(string name) + private IIndexer GetInstance(IndexerDefinition indexerDefinition) { - return _indexerRepository.Get(name); + var existingInstance = _indexers.Single(c => c.GetType().Name.Equals(indexerDefinition.Implementation, StringComparison.CurrentCultureIgnoreCase)); + var instance = (IIndexer)Activator.CreateInstance(existingInstance.GetType()); + instance.InstanceDefinition = indexerDefinition; + + return instance; } public void Handle(ApplicationStartedEvent message) { _logger.Debug("Initializing indexers. Count {0}", _indexers.Count); - var currentIndexers = All(); - - foreach (var feedProvider in _indexers) + if (!All().Any()) { - IIndexerBase indexerLocal = feedProvider; - if (!currentIndexers.Exists(c => c.Name.Equals(indexerLocal.Name, StringComparison.InvariantCultureIgnoreCase))) - { - var settings = new IndexerDefinition + var definitions = _indexers.SelectMany(delegate(IIndexer indexer) { - Enable = indexerLocal.EnabledByDefault, - Name = indexerLocal.Name.ToLower() - }; + return indexer.DefaultDefinitions; + }); - _indexerRepository.Insert(settings); - } + _indexerRepository.InsertMany(definitions.ToList()); } } } diff --git a/NzbDrone.Core/Indexers/IndexerSettingProvider.cs b/NzbDrone.Core/Indexers/IndexerSettingProvider.cs index d5dddee68..a0a0650c7 100644 --- a/NzbDrone.Core/Indexers/IndexerSettingProvider.cs +++ b/NzbDrone.Core/Indexers/IndexerSettingProvider.cs @@ -4,7 +4,7 @@ namespace NzbDrone.Core.Indexers { public interface IProviderIndexerSetting { - TSetting Get(IIndexerBase indexer) where TSetting : IIndexerSetting, new(); + TSetting Get(IIndexer indexer) where TSetting : IIndexerSetting, new(); } public class IndexerSettingProvider : IProviderIndexerSetting @@ -16,7 +16,7 @@ namespace NzbDrone.Core.Indexers _indexerRepository = indexerRepository; } - public TSetting Get(IIndexerBase indexer) where TSetting : IIndexerSetting, new() + public TSetting Get(IIndexer indexer) where TSetting : IIndexerSetting, new() { var indexerDef = _indexerRepository.Find(indexer.Name); diff --git a/NzbDrone.Core/Indexers/IndexerWithSetting.cs b/NzbDrone.Core/Indexers/IndexerWithSetting.cs index 8c8b06361..7efa6367f 100644 --- a/NzbDrone.Core/Indexers/IndexerWithSetting.cs +++ b/NzbDrone.Core/Indexers/IndexerWithSetting.cs @@ -1,38 +1,16 @@ -using System; -using NzbDrone.Common.Messaging; +using NzbDrone.Common; namespace NzbDrone.Core.Indexers { - public abstract class IndexerWithSetting : - Indexer, - IHandle where TSetting : IIndexerSetting, new() + public abstract class IndexerWithSetting : IndexerBase where TSetting : class, IIndexerSetting, new() { - protected IndexerWithSetting(IProviderIndexerSetting settingProvider) - { - Settings = settingProvider.Get(this); - } - - public override bool IsConfigured - { - get { return Settings.IsValid; } - } - - public override bool EnabledByDefault - { - get - { - return false; - } - } - public TSetting Settings { get; private set; } - public void Handle(IndexerSettingUpdatedEvent message) + public TSetting ImportSettingsFromJson(string json) { - if (message.IndexerName.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) - { - Settings = (TSetting)message.IndexerSetting; - } + Settings = new JsonSerializer().Deserialize(json) ?? new TSetting(); + + return Settings; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Newznab/Newznab.cs b/NzbDrone.Core/Indexers/Newznab/Newznab.cs index 6692c758d..1df895dd8 100644 --- a/NzbDrone.Core/Indexers/Newznab/Newznab.cs +++ b/NzbDrone.Core/Indexers/Newznab/Newznab.cs @@ -1,22 +1,74 @@ using System; using System.Collections.Generic; using System.Linq; +using NzbDrone.Common; namespace NzbDrone.Core.Indexers.Newznab { - public class Newznab : Indexer + public class Newznab : IndexerWithSetting { - private readonly INewznabService _newznabProvider; + private readonly IJsonSerializer _jsonSerializer; - public Newznab(INewznabService newznabProvider) + public Newznab() { - _newznabProvider = newznabProvider; + _jsonSerializer = new JsonSerializer(); } + public override IEnumerable DefaultDefinitions + { + get + { + var list = new List(); + + list.Add(new IndexerDefinition + { + Enable = false, + Name = "Nzbs.org", + Implementation = GetType().Name, + Settings = GetSettings("http://nzbs.org") + }); + + + list.Add(new IndexerDefinition + { + Enable = false, + Name = "Nzb.su", + Implementation = GetType().Name, + Settings = GetSettings("https://nzb.su") + }); + + list.Add(new IndexerDefinition + { + Enable = false, + Name = "Dognzb.cr", + Implementation = GetType().Name, + Settings = GetSettings("https://dognzb.cr") + }); + + return list; + + } + } + + private string GetSettings(string url) + { + return _jsonSerializer.Serialize(new NewznabSettings { Url = url }); + } + public override IEnumerable RecentFeed { - get { return GetUrls(); } + get + { + var url = String.Format("{0}/api?t=tvsearch&cat=5030,5040,5070,5090s", Settings.Url); + + if (String.IsNullOrWhiteSpace(Settings.ApiKey)) + { + url += "&apikey=" + Settings.ApiKey; + } + + yield return url; + } } public override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) @@ -41,35 +93,15 @@ namespace NzbDrone.Core.Indexers.Newznab public override string Name { - get { return "Newznab"; } - } - - - private IEnumerable GetUrls() - { - var urls = new List(); - var newznabIndexers = _newznabProvider.Enabled(); - - foreach (var newznabDefinition in newznabIndexers) + get { - var url = String.Format("{0}/api?t=tvsearch&cat=5030,5040,5070,5090s", newznabDefinition.Url); - - if (String.IsNullOrWhiteSpace(newznabDefinition.ApiKey)) - { - url += "&apikey=" + newznabDefinition.ApiKey; - } - - urls.Add(url); + return InstanceDefinition.Name; } - - return urls; } - private static string NewsnabifyTitle(string title) { return title.Replace("+", "%20"); } - } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabDefinition.cs b/NzbDrone.Core/Indexers/Newznab/NewznabDefinition.cs deleted file mode 100644 index 2fad208af..000000000 --- a/NzbDrone.Core/Indexers/Newznab/NewznabDefinition.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using NzbDrone.Core.Datastore; - -namespace NzbDrone.Core.Indexers.Newznab -{ - public class NewznabDefinition : ModelBase - { - public Boolean Enable { get; set; } - public String Name { get; set; } - public String Url { get; set; } - public String ApiKey { get; set; } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabParser.cs b/NzbDrone.Core/Indexers/Newznab/NewznabParser.cs index fe3d94ff3..db71d631b 100644 --- a/NzbDrone.Core/Indexers/Newznab/NewznabParser.cs +++ b/NzbDrone.Core/Indexers/Newznab/NewznabParser.cs @@ -1,7 +1,5 @@ using System; using System.ServiceModel.Syndication; -using NzbDrone.Core.Model; -using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.Indexers.Newznab @@ -25,7 +23,9 @@ namespace NzbDrone.Core.Indexers.Newznab if (currentResult != null) { if (item.Links.Count > 1) + { currentResult.Size = item.Links[1].Length; + } currentResult.Indexer = GetName(item); } diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabRepository.cs b/NzbDrone.Core/Indexers/Newznab/NewznabRepository.cs deleted file mode 100644 index 16a304104..000000000 --- a/NzbDrone.Core/Indexers/Newznab/NewznabRepository.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using NzbDrone.Core.Datastore; - -namespace NzbDrone.Core.Indexers.Newznab -{ - public interface INewznabRepository : IBasicRepository - { - IEnumerable Enabled(); - } - - public class NewznabRepository : BasicRepository, INewznabRepository - { - public NewznabRepository(IDatabase database) - : base(database) - { - } - - public IEnumerable Enabled() - { - return Query.Where(n => n.Enable); - } - } -} diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabService.cs b/NzbDrone.Core/Indexers/Newznab/NewznabService.cs deleted file mode 100644 index 520992ac6..000000000 --- a/NzbDrone.Core/Indexers/Newznab/NewznabService.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using NLog; -using NzbDrone.Common.Messaging; -using NzbDrone.Core.Lifecycle; - -namespace NzbDrone.Core.Indexers.Newznab -{ - public interface INewznabService - { - List All(); - List Enabled(); - NewznabDefinition Insert(NewznabDefinition definition); - void Update(NewznabDefinition definition); - } - - public class NewznabService : INewznabService, IHandle - { - private readonly INewznabRepository _newznabRepository; - private readonly Logger _logger; - - public NewznabService(INewznabRepository newznabRepository, Logger logger) - { - _newznabRepository = newznabRepository; - _logger = logger; - } - - public List All() - { - return _newznabRepository.All().ToList(); - } - - public List Enabled() - { - return _newznabRepository.Enabled().ToList(); - } - - public NewznabDefinition Insert(NewznabDefinition definition) - { - definition.Url = definition.Url.ToLower(); - _logger.Debug("Adding Newznab definition for {0}", definition.Name); - return _newznabRepository.Insert(definition); - } - - public void Update(NewznabDefinition definition) - { - definition.Url = definition.Url.ToLower(); - _logger.Debug("Updating Newznab definition for {0}", definition.Name); - _newznabRepository.Update(definition); - } - - public void Delete(int id) - { - - _newznabRepository.Delete(id); - } - - public void CheckHostname(string url) - { - try - { - var uri = new Uri(url); - var hostname = uri.DnsSafeHost; - - Dns.GetHostEntry(hostname); - } - catch (Exception ex) - { - _logger.Error("Invalid address {0}, please correct the site URL.", url); - _logger.TraceException(ex.Message, ex); - throw; - } - - } - - public void Handle(ApplicationStartedEvent message) - { - var newznabIndexers = new List - { - new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org" }, - new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su" }, - new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr" } - }; - - _logger.Debug("Initializing Newznab indexers. Count {0}", newznabIndexers); - - try - { - var currentIndexers = All(); - - _logger.Debug("Deleting broken Newznab indexer"); - var brokenIndexers = currentIndexers.Where(i => String.IsNullOrEmpty(i.Name) || String.IsNullOrWhiteSpace(i.Url)).ToList(); - brokenIndexers.ForEach(e => Delete(e.Id)); - - currentIndexers = All(); - - foreach (var feedProvider in newznabIndexers) - { - try - { - NewznabDefinition indexerLocal = feedProvider; - var currentIndexer = currentIndexers - .FirstOrDefault(c => new Uri(c.Url.ToLower()).Host == new Uri(indexerLocal.Url.ToLower()).Host); - - if (currentIndexer == null) - { - var definition = new NewznabDefinition - { - Enable = false, - Name = indexerLocal.Name, - Url = indexerLocal.Url, - ApiKey = indexerLocal.ApiKey, - }; - - Insert(definition); - } - - else - { - currentIndexer.Url = indexerLocal.Url; - Update(currentIndexer); - } - } - catch (Exception ex) - { - _logger.ErrorException("An error occurred while setting up indexer: " + feedProvider.Name, ex); - } - } - } - catch (Exception ex) - { - _logger.ErrorException("An Error occurred while initializing Newznab Indexers", ex); - } - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs b/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs new file mode 100644 index 000000000..a24d5d15b --- /dev/null +++ b/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs @@ -0,0 +1,18 @@ +using System; + +namespace NzbDrone.Core.Indexers.Newznab +{ + public class NewznabSettings : IIndexerSetting + { + public String Url { get; set; } + public String ApiKey { get; set; } + + public bool IsValid + { + get + { + return !string.IsNullOrWhiteSpace(Url); + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/NzbClub/NzbClub.cs b/NzbDrone.Core/Indexers/NzbClub/NzbClub.cs index 289d38d28..a9dc270f6 100644 --- a/NzbDrone.Core/Indexers/NzbClub/NzbClub.cs +++ b/NzbDrone.Core/Indexers/NzbClub/NzbClub.cs @@ -3,8 +3,13 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers.NzbClub { - public class NzbClub : Indexer + public class NzbClub : IndexerBase { + public override string Name + { + get { return "NzbClub"; } + } + public override IEnumerable RecentFeed { get @@ -17,13 +22,6 @@ namespace NzbDrone.Core.Indexers.NzbClub } } - public override string Name - { - get { return "NzbClub"; } - } - - - public override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { var searchUrls = new List(); diff --git a/NzbDrone.Core/Indexers/NzbIndex/NzbIndex.cs b/NzbDrone.Core/Indexers/NzbIndex/NzbIndex.cs index 97b2823d0..04af23b28 100644 --- a/NzbDrone.Core/Indexers/NzbIndex/NzbIndex.cs +++ b/NzbDrone.Core/Indexers/NzbIndex/NzbIndex.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers.NzbIndex { - public class NzbIndex : Indexer + public class NzbIndex : IndexerBase { public override IEnumerable RecentFeed { @@ -73,11 +73,5 @@ namespace NzbDrone.Core.Indexers.NzbIndex return searchUrls; } - - - - - - } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/NzbsRUs/NzbsRUs.cs b/NzbDrone.Core/Indexers/NzbsRUs/NzbsRUs.cs index 90f7592b8..7f19c8542 100644 --- a/NzbDrone.Core/Indexers/NzbsRUs/NzbsRUs.cs +++ b/NzbDrone.Core/Indexers/NzbsRUs/NzbsRUs.cs @@ -5,20 +5,13 @@ namespace NzbDrone.Core.Indexers.NzbsRUs { public class Nzbsrus : IndexerWithSetting { - private readonly NzbsrusSetting _setting; - - public Nzbsrus(IProviderIndexerSetting settingProvider):base(settingProvider) - { - - } - public override IEnumerable RecentFeed { get { yield return string.Format("https://www.nzbsrus.com/rssfeed.php?cat=91,75&i={0}&h={1}", - _setting.Uid, - _setting.Hash); + Settings.Uid, + Settings.Hash); } } diff --git a/NzbDrone.Core/Indexers/Nzbx/Nzbx.cs b/NzbDrone.Core/Indexers/Nzbx/Nzbx.cs index 6a25b1304..1d934d6e3 100644 --- a/NzbDrone.Core/Indexers/Nzbx/Nzbx.cs +++ b/NzbDrone.Core/Indexers/Nzbx/Nzbx.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers.Nzbx { - public class Nzbx : Indexer + public class Nzbx : IndexerBase { public override IParseFeed Parser { diff --git a/NzbDrone.Core/Indexers/Omgwtfnzbs/Omgwtfnzbs.cs b/NzbDrone.Core/Indexers/Omgwtfnzbs/Omgwtfnzbs.cs index 5c4ea4727..1da8456e6 100644 --- a/NzbDrone.Core/Indexers/Omgwtfnzbs/Omgwtfnzbs.cs +++ b/NzbDrone.Core/Indexers/Omgwtfnzbs/Omgwtfnzbs.cs @@ -5,11 +5,6 @@ namespace NzbDrone.Core.Indexers.Omgwtfnzbs { public class Omgwtfnzbs : IndexerWithSetting { - public Omgwtfnzbs(IProviderIndexerSetting settingProvider) - : base(settingProvider) - { - } - public override string Name { get { return "omgwtfnzbs"; } @@ -20,8 +15,7 @@ namespace NzbDrone.Core.Indexers.Omgwtfnzbs get { - yield return - String.Format("http://rss.omgwtfnzbs.org/rss-search.php?catid=19,20&user={0}&api={1}&eng=1", + yield return String.Format("http://rss.omgwtfnzbs.org/rss-search.php?catid=19,20&user={0}&api={1}&eng=1", Settings.Username, Settings.ApiKey); } } diff --git a/NzbDrone.Core/Indexers/Wombles/Wombles.cs b/NzbDrone.Core/Indexers/Wombles/Wombles.cs index 2dbdfe4a0..bf699be14 100644 --- a/NzbDrone.Core/Indexers/Wombles/Wombles.cs +++ b/NzbDrone.Core/Indexers/Wombles/Wombles.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace NzbDrone.Core.Indexers.Wombles { - public class Wombles : Indexer + public class Wombles : IndexerBase { public override IEnumerable RecentFeed { @@ -34,11 +34,5 @@ namespace NzbDrone.Core.Indexers.Wombles { return new List(); } - - - public bool EnabledByDefault - { - get { return true; } - } } } \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index f13a67421..8d1f7652b 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -237,7 +237,7 @@ - + @@ -258,15 +258,13 @@ - + - + - -