From 99f452e29925c767d882fc706bf47199ae3fdfdf Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 26 Sep 2015 10:45:13 +0200 Subject: [PATCH] New: Added support for newznab indexers using tvdbid for searching. --- src/NzbDrone.Api/Parse/ParseModule.cs | 2 +- .../DownloadDecisionMakerFixture.cs | 14 +- .../DownloadClientFixtureBase.cs | 2 +- .../BroadcastheNetFixture.cs | 1 + .../NewznabTests/NewznabFixture.cs | 5 + .../NewznabRequestGeneratorFixture.cs | 87 ++++++- .../IndexerTests/RarbgTests/RarbgFixture.cs | 1 + .../TitansOfTvTests/TitansOfTvFixture.cs | 1 + .../TorznabTests/TorznabFixture.cs | 8 +- .../TorznabRequestGeneratorFixture.cs | 154 ------------ .../TorznabTests/TorznabSettingFixture.cs | 58 ----- .../NzbDrone.Core.Test.csproj | 2 - .../ParsingServiceTests/GetEpisodesFixture.cs | 20 +- .../ParsingServiceTests/MapFixture.cs | 45 +++- .../DecisionEngine/DownloadDecisionMaker.cs | 4 +- .../TrackedDownloadService.cs | 2 +- src/NzbDrone.Core/History/HistoryService.cs | 1 + .../BroadcastheNet/BroadcastheNetParser.cs | 4 + src/NzbDrone.Core/Indexers/Newznab/Newznab.cs | 47 +++- .../Indexers/Newznab/NewznabCapabilities.cs | 28 +++ .../NewznabCapabilitiesProvider.cs} | 30 +-- .../Newznab/NewznabRequestGenerator.cs | 152 +++++++++--- .../Indexers/Newznab/NewznabRssParser.cs | 14 ++ .../Indexers/Newznab/NewznabSettings.cs | 16 +- .../Indexers/Rarbg/RarbgParser.cs | 14 +- src/NzbDrone.Core/Indexers/Torznab/Torznab.cs | 15 +- .../Indexers/Torznab/TorznabCapabilities.cs | 28 --- .../Indexers/Torznab/TorznabException.cs | 3 +- .../Torznab/TorznabRequestGenerator.cs | 228 ------------------ .../Indexers/Torznab/TorznabRssParser.cs | 38 ++- .../Indexers/Torznab/TorznabSettings.cs | 48 ++-- src/NzbDrone.Core/NzbDrone.Core.csproj | 5 +- src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs | 2 + src/NzbDrone.Core/Parser/ParsingService.cs | 52 ++-- 34 files changed, 489 insertions(+), 642 deletions(-) delete mode 100644 src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabRequestGeneratorFixture.cs delete mode 100644 src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabSettingFixture.cs create mode 100644 src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilities.cs rename src/NzbDrone.Core/Indexers/{Torznab/TorznabCapabilitiesProvider.cs => Newznab/NewznabCapabilitiesProvider.cs} (81%) delete mode 100644 src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilities.cs delete mode 100644 src/NzbDrone.Core/Indexers/Torznab/TorznabRequestGenerator.cs diff --git a/src/NzbDrone.Api/Parse/ParseModule.cs b/src/NzbDrone.Api/Parse/ParseModule.cs index 462839cfd..6be81934c 100644 --- a/src/NzbDrone.Api/Parse/ParseModule.cs +++ b/src/NzbDrone.Api/Parse/ParseModule.cs @@ -24,7 +24,7 @@ namespace NzbDrone.Api.Parse return null; } - var remoteEpisode = _parsingService.Map(parsedEpisodeInfo); + var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, 0, 0); if (remoteEpisode == null) { diff --git a/src/NzbDrone.Core.Test/DecisionEngineTests/DownloadDecisionMakerFixture.cs b/src/NzbDrone.Core.Test/DecisionEngineTests/DownloadDecisionMakerFixture.cs index f3a7fb7e4..cb4d2a97d 100644 --- a/src/NzbDrone.Core.Test/DecisionEngineTests/DownloadDecisionMakerFixture.cs +++ b/src/NzbDrone.Core.Test/DecisionEngineTests/DownloadDecisionMakerFixture.cs @@ -54,7 +54,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests }; Mocker.GetMock() - .Setup(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny())) + .Setup(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(_remoteEpisode); } @@ -125,7 +125,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests var results = Subject.GetRssDecision(_reports).ToList(); - Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); _pass1.Verify(c => c.IsSatisfiedBy(It.IsAny(), null), Times.Never()); _pass2.Verify(c => c.IsSatisfiedBy(It.IsAny(), null), Times.Never()); @@ -142,7 +142,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests var results = Subject.GetRssDecision(_reports).ToList(); - Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); _pass1.Verify(c => c.IsSatisfiedBy(It.IsAny(), null), Times.Never()); _pass2.Verify(c => c.IsSatisfiedBy(It.IsAny(), null), Times.Never()); @@ -170,7 +170,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests { GivenSpecifications(_pass1); - Mocker.GetMock().Setup(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny())) + Mocker.GetMock().Setup(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Throws(); _reports = new List @@ -182,7 +182,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests Subject.GetRssDecision(_reports); - Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(_reports.Count)); + Mocker.GetMock().Verify(c => c.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(_reports.Count)); ExceptionVerification.ExpectedErrors(3); } @@ -221,8 +221,8 @@ namespace NzbDrone.Core.Test.DecisionEngineTests }).ToList(); Mocker.GetMock() - .Setup(v => v.Map(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns((p,id,c) => + .Setup(v => v.Map(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns((p,tvdbid,tvrageid,c) => new RemoteEpisode { DownloadAllowed = true, diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/DownloadClientFixtureBase.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/DownloadClientFixtureBase.cs index de696feda..c0eacf7bc 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/DownloadClientFixtureBase.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/DownloadClientFixtureBase.cs @@ -30,7 +30,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests .Returns(30); Mocker.GetMock() - .Setup(s => s.Map(It.IsAny(), It.IsAny(), (SearchCriteriaBase)null)) + .Setup(s => s.Map(It.IsAny(), It.IsAny(), It.IsAny(), (SearchCriteriaBase)null)) .Returns(() => CreateRemoteEpisode()); Mocker.GetMock() diff --git a/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetFixture.cs index fb35e1987..0945eef82 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetFixture.cs @@ -51,6 +51,7 @@ namespace NzbDrone.Core.Test.IndexerTests.BroadcastheNetTests torrentInfo.PublishDate.Should().Be(DateTime.Parse("2014/09/16 21:15:33")); torrentInfo.Size.Should().Be(505099926); torrentInfo.InfoHash.Should().Be("123"); + torrentInfo.TvdbId.Should().Be(71998); torrentInfo.TvRageId.Should().Be(4055); torrentInfo.MagnetUrl.Should().BeNullOrEmpty(); torrentInfo.Peers.Should().Be(40+9); diff --git a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabFixture.cs index dd675bd12..899015030 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabFixture.cs @@ -6,6 +6,7 @@ using NUnit.Framework; using NzbDrone.Common.Http; using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers.Newznab; +using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.IndexerTests.NewznabTests @@ -26,6 +27,10 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests Categories = new int[] { 1 } } }; + + Mocker.GetMock() + .Setup(v => v.GetCapabilities(It.IsAny())) + .Returns(new NewznabCapabilities()); } [Test] diff --git a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabRequestGeneratorFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabRequestGeneratorFixture.cs index 1bc95a2a7..66cc059b3 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabRequestGeneratorFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabRequestGeneratorFixture.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using FluentAssertions; +using Moq; using NUnit.Framework; using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.IndexerSearch.Definitions; @@ -11,7 +12,9 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests { public class NewznabRequestGeneratorFixture : CoreTest { - AnimeEpisodeSearchCriteria _animeSearchCriteria; + private SingleEpisodeSearchCriteria _singleEpisodeSearchCriteria; + private AnimeEpisodeSearchCriteria _animeSearchCriteria; + private NewznabCapabilities _capabilities; [SetUp] public void SetUp() @@ -24,11 +27,25 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests ApiKey = "abcd", }; + _singleEpisodeSearchCriteria = new SingleEpisodeSearchCriteria + { + Series = new Tv.Series { TvRageId = 10, TvdbId = 20 }, + SceneTitles = new List { "Monkey Island" }, + SeasonNumber = 1, + EpisodeNumber = 2 + }; + _animeSearchCriteria = new AnimeEpisodeSearchCriteria() { SceneTitles = new List() { "Monkey+Island" }, AbsoluteEpisodeNumber = 100 }; + + _capabilities = new NewznabCapabilities(); + + Mocker.GetMock() + .Setup(v => v.GetCapabilities(It.IsAny())) + .Returns(_capabilities); } [Test] @@ -106,5 +123,73 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests pages.Count.Should().BeLessThan(500); } + + [Test] + public void should_not_search_by_rid_if_not_supported() + { + _capabilities.SupportedTvSearchParameters = new[] { "q", "season", "ep" }; + + var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); + + results.Should().HaveCount(1); + + var page = results.First().First(); + + page.Url.Query.Should().NotContain("rid=10"); + page.Url.Query.Should().Contain("q=Monkey"); + } + + [Test] + public void should_search_by_rid_if_supported() + { + var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); + results.Should().HaveCount(1); + + var page = results.First().First(); + + page.Url.Query.Should().Contain("rid=10"); + } + + [Test] + public void should_not_search_by_tvdbid_if_not_supported() + { + _capabilities.SupportedTvSearchParameters = new[] { "q", "season", "ep" }; + + var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); + + results.Should().HaveCount(1); + + var page = results.First().First(); + + page.Url.Query.Should().NotContain("rid=10"); + page.Url.Query.Should().Contain("q=Monkey"); + } + + [Test] + public void should_search_by_tvdbid_if_supported() + { + _capabilities.SupportedTvSearchParameters = new[] { "q", "tvdbid", "season", "ep" }; + + var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); + results.Should().HaveCount(1); + + var page = results.First().First(); + + page.Url.Query.Should().Contain("tvdbid=20"); + } + + [Test] + public void should_prefer_search_by_tvdbid_if_rid_supported() + { + _capabilities.SupportedTvSearchParameters = new[] { "q", "tvdbid", "rid", "season", "ep" }; + + var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); + results.Should().HaveCount(1); + + var page = results.First().First(); + + page.Url.Query.Should().Contain("tvdbid=20"); + page.Url.Query.Should().NotContain("rid=10"); + } } } diff --git a/src/NzbDrone.Core.Test/IndexerTests/RarbgTests/RarbgFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/RarbgTests/RarbgFixture.cs index 441133d6c..7f442fcb2 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/RarbgTests/RarbgFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/RarbgTests/RarbgFixture.cs @@ -56,6 +56,7 @@ namespace NzbDrone.Core.Test.IndexerTests.RarbgTests torrentInfo.MagnetUrl.Should().BeNull(); torrentInfo.Peers.Should().Be(304 + 200); torrentInfo.Seeders.Should().Be(304); + torrentInfo.TvdbId.Should().Be(268156); torrentInfo.TvRageId.Should().Be(35197); } diff --git a/src/NzbDrone.Core.Test/IndexerTests/TitansOfTvTests/TitansOfTvFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/TitansOfTvTests/TitansOfTvFixture.cs index 15ae31b8c..4847b1efe 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/TitansOfTvTests/TitansOfTvFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/TitansOfTvTests/TitansOfTvFixture.cs @@ -51,6 +51,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TitansOfTvTests torrentInfo.PublishDate.Should().Be(DateTime.Parse("2015-06-25 04:13:44")); torrentInfo.Size.Should().Be(435402993); torrentInfo.InfoHash.Should().BeNullOrEmpty(); + torrentInfo.TvdbId.Should().Be(0); torrentInfo.TvRageId.Should().Be(0); torrentInfo.MagnetUrl.Should().BeNullOrEmpty(); torrentInfo.Peers.Should().Be(2+5); diff --git a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabFixture.cs index 6fd8d5588..be48f1ecf 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabFixture.cs @@ -5,6 +5,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common.Http; using NzbDrone.Core.Indexers; +using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Indexers.Torznab; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; @@ -27,9 +28,9 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests } }; - Mocker.GetMock() - .Setup(v => v.GetCapabilities(It.IsAny())) - .Returns(new TorznabCapabilities()); + Mocker.GetMock() + .Setup(v => v.GetCapabilities(It.IsAny())) + .Returns(new NewznabCapabilities()); } [Test] @@ -56,6 +57,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests releaseInfo.Indexer.Should().Be(Subject.Definition.Name); releaseInfo.PublishDate.Should().Be(DateTime.Parse("2015/03/14 21:10:42")); releaseInfo.Size.Should().Be(2538463390); + releaseInfo.TvdbId.Should().Be(273181); releaseInfo.TvRageId.Should().Be(37780); releaseInfo.InfoHash.Should().Be("63e07ff523710ca268567dad344ce1e0e6b7e8a3"); releaseInfo.Seeders.Should().Be(7); diff --git a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabRequestGeneratorFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabRequestGeneratorFixture.cs deleted file mode 100644 index ff33ba6d8..000000000 --- a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabRequestGeneratorFixture.cs +++ /dev/null @@ -1,154 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using FluentAssertions; -using Moq; -using NUnit.Framework; -using NzbDrone.Core.Indexers.Torznab; -using NzbDrone.Core.IndexerSearch.Definitions; -using NzbDrone.Core.Test.Framework; - -namespace NzbDrone.Core.Test.IndexerTests.TorznabTests -{ - public class TorznabRequestGeneratorFixture : CoreTest - { - private SingleEpisodeSearchCriteria _singleEpisodeSearchCriteria; - private AnimeEpisodeSearchCriteria _animeSearchCriteria; - private TorznabCapabilities _capabilities; - - [SetUp] - public void SetUp() - { - Subject.Settings = new TorznabSettings() - { - Url = "http://127.0.0.1:1234/", - Categories = new [] { 1, 2 }, - AnimeCategories = new [] { 3, 4 }, - ApiKey = "abcd", - EnableRageIDLookup = true - }; - - _singleEpisodeSearchCriteria = new SingleEpisodeSearchCriteria - { - Series = new Tv.Series { TvRageId = 10 }, - SceneTitles = new List { "Monkey Island" }, - SeasonNumber = 1, - EpisodeNumber = 2 - }; - - _animeSearchCriteria = new AnimeEpisodeSearchCriteria() - { - SceneTitles = new List() { "Monkey+Island" }, - AbsoluteEpisodeNumber = 100 - }; - - _capabilities = new TorznabCapabilities(); - - Mocker.GetMock() - .Setup(v => v.GetCapabilities(It.IsAny())) - .Returns(_capabilities); - } - - [Test] - public void should_use_all_categories_for_feed() - { - var results = Subject.GetRecentRequests(); - - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().Contain("&cat=1,2,3,4&"); - } - - [Test] - public void should_not_have_duplicate_categories() - { - Subject.Settings.Categories = new[] { 1, 2, 3 }; - - var results = Subject.GetRecentRequests(); - - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().Contain("&cat=1,2,3,4&"); - } - - [Test] - public void should_use_only_anime_categories_for_anime_search() - { - var results = Subject.GetSearchRequests(_animeSearchCriteria); - - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().Contain("&cat=3,4&"); - } - - [Test] - public void should_use_mode_search_for_anime() - { - var results = Subject.GetSearchRequests(_animeSearchCriteria); - - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().Contain("?t=search&"); - } - - [Test] - public void should_return_subsequent_pages() - { - var results = Subject.GetSearchRequests(_animeSearchCriteria); - - results.Should().HaveCount(1); - - var pages = results.First().Take(3).ToList(); - - pages[0].Url.Query.Should().Contain("&offset=0&"); - pages[1].Url.Query.Should().Contain("&offset=100&"); - pages[2].Url.Query.Should().Contain("&offset=200&"); - } - - [Test] - public void should_not_get_unlimited_pages() - { - var results = Subject.GetSearchRequests(_animeSearchCriteria); - - results.Should().HaveCount(1); - - var pages = results.First().Take(500).ToList(); - - pages.Count.Should().BeLessThan(500); - } - - [Test] - public void should_not_search_by_rid_if_not_supported() - { - _capabilities.SupportedTvSearchParameters = new[] { "q", "season", "ep" }; - - var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); - - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().NotContain("rid=10"); - page.Url.Query.Should().Contain("q=Monkey"); - } - - [Test] - public void should_search_by_rid_if_supported() - { - var results = Subject.GetSearchRequests(_singleEpisodeSearchCriteria); - results.Should().HaveCount(1); - - var page = results.First().First(); - - page.Url.Query.Should().Contain("rid=10"); - } - } -} diff --git a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabSettingFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabSettingFixture.cs deleted file mode 100644 index 2fc06ba66..000000000 --- a/src/NzbDrone.Core.Test/IndexerTests/TorznabTests/TorznabSettingFixture.cs +++ /dev/null @@ -1,58 +0,0 @@ -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Core.Indexers.Torznab; -using NzbDrone.Core.Test.Framework; - -namespace NzbDrone.Core.Test.IndexerTests.TorznabTests -{ - public class TorznabSettingFixture : CoreTest - { - - [TestCase("http://hdaccess.net")] - public void requires_apikey(string url) - { - var setting = new TorznabSettings() - { - ApiKey = "", - Url = url - }; - - - setting.Validate().IsValid.Should().BeFalse(); - setting.Validate().Errors.Should().Contain(c => c.PropertyName == "ApiKey"); - - } - - [TestCase("")] - [TestCase(" ")] - [TestCase(null)] - public void invalid_url_should_not_apikey(string url) - { - var setting = new TorznabSettings - { - ApiKey = "", - Url = url - }; - - - setting.Validate().IsValid.Should().BeFalse(); - setting.Validate().Errors.Should().NotContain(c => c.PropertyName == "ApiKey"); - setting.Validate().Errors.Should().Contain(c => c.PropertyName == "Url"); - - } - - - [TestCase("http://myfancytracker.net")] - public void doesnt_requires_apikey(string url) - { - var setting = new TorznabSettings() - { - ApiKey = "", - Url = url - }; - - - setting.Validate().IsValid.Should().BeTrue(); - } - } -} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 59ab1b43c..28fab06e6 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -226,8 +226,6 @@ - - diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetEpisodesFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetEpisodesFixture.cs index 898f70cb7..1c473a396 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetEpisodesFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetEpisodesFixture.cs @@ -82,7 +82,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests GivenDailySeries(); GivenDailyParseResult(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny()), Times.Once()); @@ -94,7 +94,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests GivenDailySeries(); GivenDailyParseResult(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny()), Times.Never()); @@ -106,7 +106,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests GivenDailySeries(); _parsedEpisodeInfo.AirDate = DateTime.Today.AddDays(-5).ToString(Episode.AIR_DATE_FORMAT); ; - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny()), Times.Once()); @@ -117,7 +117,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenAbsoluteNumberingSeries(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny()), Times.Never()); @@ -128,7 +128,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenSceneNumberingSeries(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindEpisodesBySceneNumbering(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once()); @@ -139,7 +139,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenSceneNumberingSeries(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisodesBySceneNumbering(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); @@ -151,7 +151,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests GivenSceneNumberingSeries(); _episodes.First().SceneEpisodeNumber = 10; - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisodesBySceneNumbering(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once()); @@ -160,7 +160,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests [Test] public void should_find_episode() { - Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once()); @@ -169,7 +169,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests [Test] public void should_match_episode_with_search_criteria() { - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); @@ -180,7 +180,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { _episodes.First().EpisodeNumber = 10; - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindEpisode(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once()); diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs index c237f5d46..2357472ce 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs @@ -59,6 +59,13 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests .Returns(_series); } + private void GivenMatchByTvdbId() + { + Mocker.GetMock() + .Setup(s => s.FindByTvdbId(It.IsAny())) + .Returns(_series); + } + private void GivenMatchByTvRageId() { Mocker.GetMock() @@ -76,18 +83,29 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenMatchBySeriesTitle(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindByTitle(It.IsAny()), Times.Once()); } + [Test] + public void should_use_tvdbid_when_series_title_lookup_fails() + { + GivenMatchByTvdbId(); + + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); + + Mocker.GetMock() + .Verify(v => v.FindByTvdbId(It.IsAny()), Times.Once()); + } + [Test] public void should_use_tvrageid_when_series_title_lookup_fails() { GivenMatchByTvRageId(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + Subject.Map(_parsedEpisodeInfo, 0, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindByTvRageId(It.IsAny()), Times.Once()); @@ -102,7 +120,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests .Setup(v => v.FindTvdbId(It.IsAny())) .Returns(10); - var result = Subject.Map(_parsedEpisodeInfo, _series.TvRageId); + var result = Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId); Mocker.GetMock() .Verify(v => v.FindByTvRageId(It.IsAny()), Times.Never()); @@ -115,7 +133,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenMatchBySeriesTitle(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindByTitle(It.IsAny()), Times.Never()); @@ -126,18 +144,29 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenParseResultSeriesDoesntMatchSearchCriteria(); - Subject.Map(_parsedEpisodeInfo, 10, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, 10, 10, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindByTitle(It.IsAny()), Times.Once()); } + [Test] + public void should_FindByTvdbId_when_search_criteria_and_FindByTitle_matching_fails() + { + GivenParseResultSeriesDoesntMatchSearchCriteria(); + + Subject.Map(_parsedEpisodeInfo, 10, 10, _singleEpisodeSearchCriteria); + + Mocker.GetMock() + .Verify(v => v.FindByTvdbId(It.IsAny()), Times.Once()); + } + [Test] public void should_FindByTvRageId_when_search_criteria_and_FindByTitle_matching_fails() { GivenParseResultSeriesDoesntMatchSearchCriteria(); - Subject.Map(_parsedEpisodeInfo, 10, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, 10, 10, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindByTvRageId(It.IsAny()), Times.Once()); @@ -150,7 +179,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests .Setup(s => s.FindTvdbId(It.IsAny())) .Returns(_series.TvdbId); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindByTitle(It.IsAny()), Times.Never()); @@ -161,7 +190,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests { GivenParseResultSeriesDoesntMatchSearchCriteria(); - Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); + Subject.Map(_parsedEpisodeInfo, _series.TvdbId, _series.TvRageId, _singleEpisodeSearchCriteria); Mocker.GetMock() .Verify(v => v.FindByTitle(It.IsAny()), Times.Never()); diff --git a/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs b/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs index 566380e6c..39adf6d87 100644 --- a/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs +++ b/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs @@ -65,7 +65,7 @@ namespace NzbDrone.Core.DecisionEngine if (parsedEpisodeInfo == null || parsedEpisodeInfo.IsPossibleSpecialEpisode) { - var specialEpisodeInfo = _parsingService.ParseSpecialEpisodeTitle(report.Title, report.TvRageId, searchCriteria); + var specialEpisodeInfo = _parsingService.ParseSpecialEpisodeTitle(report.Title, report.TvdbId, report.TvRageId, searchCriteria); if (specialEpisodeInfo != null) { @@ -75,7 +75,7 @@ namespace NzbDrone.Core.DecisionEngine if (parsedEpisodeInfo != null && !parsedEpisodeInfo.SeriesTitle.IsNullOrWhiteSpace()) { - var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvRageId, searchCriteria); + var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvdbId, report.TvRageId, searchCriteria); remoteEpisode.Release = report; if (remoteEpisode.Series != null) diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs index 9375f2f6c..00bad1ab8 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs @@ -62,7 +62,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads if (parsedEpisodeInfo != null) { - trackedDownload.RemoteEpisode = _parsingService.Map(parsedEpisodeInfo); + trackedDownload.RemoteEpisode = _parsingService.Map(parsedEpisodeInfo, 0, 0); } if (historyItems.Any()) diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index 4fee5e753..1db2e7778 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -151,6 +151,7 @@ namespace NzbDrone.Core.History history.Data.Add("Size", message.Episode.Release.Size.ToString()); history.Data.Add("DownloadUrl", message.Episode.Release.DownloadUrl); history.Data.Add("Guid", message.Episode.Release.Guid); + history.Data.Add("TvdbId", message.Episode.Release.TvdbId.ToString()); history.Data.Add("TvRageId", message.Episode.Release.TvRageId.ToString()); history.Data.Add("Protocol", ((int)message.Episode.Release.DownloadProtocol).ToString()); diff --git a/src/NzbDrone.Core/Indexers/BroadcastheNet/BroadcastheNetParser.cs b/src/NzbDrone.Core/Indexers/BroadcastheNet/BroadcastheNetParser.cs index 8379869b8..3b8d6fe44 100644 --- a/src/NzbDrone.Core/Indexers/BroadcastheNet/BroadcastheNetParser.cs +++ b/src/NzbDrone.Core/Indexers/BroadcastheNet/BroadcastheNetParser.cs @@ -56,6 +56,10 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet torrentInfo.DownloadUrl = RegexProtocol.Replace(torrent.DownloadURL, protocol); torrentInfo.InfoUrl = string.Format("{0}//broadcasthe.net/torrents.php?id={1}&torrentid={2}", protocol, torrent.GroupID, torrent.TorrentID); //torrentInfo.CommentUrl = + if (torrent.TvdbID.HasValue) + { + torrentInfo.TvdbId = torrent.TvdbID.Value; + } if (torrent.TvrageID.HasValue) { torrentInfo.TvRageId = torrent.TvrageID.Value; diff --git a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs index 82d83ecb6..ff18d65dc 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using FluentValidation.Results; using NLog; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; using NzbDrone.Core.Parser; @@ -11,6 +13,8 @@ namespace NzbDrone.Core.Indexers.Newznab { public class Newznab : HttpIndexerBase { + private readonly INewznabCapabilitiesProvider _capabilitiesProvider; + public override string Name { get @@ -24,7 +28,7 @@ namespace NzbDrone.Core.Indexers.Newznab public override IIndexerRequestGenerator GetRequestGenerator() { - return new NewznabRequestGenerator() + return new NewznabRequestGenerator(_capabilitiesProvider) { PageSize = PageSize, Settings = Settings @@ -50,10 +54,10 @@ namespace NzbDrone.Core.Indexers.Newznab } } - public Newznab(IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger) + public Newznab(INewznabCapabilitiesProvider capabilitiesProvider, IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger) : base(httpClient, indexerStatusService, configService, parsingService, logger) { - + _capabilitiesProvider = capabilitiesProvider; } private IndexerDefinition GetDefinition(string name, NewznabSettings settings) @@ -82,5 +86,42 @@ namespace NzbDrone.Core.Indexers.Newznab return settings; } + + protected override void Test(List failures) + { + base.Test(failures); + + failures.AddIfNotNull(TestCapabilities()); + } + + protected virtual ValidationFailure TestCapabilities() + { + try + { + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + if (capabilities.SupportedSearchParameters != null && capabilities.SupportedSearchParameters.Contains("q")) + { + return null; + } + + if (capabilities.SupportedTvSearchParameters != null && + new[] { "q", "tvdbid", "rid" }.Any(v => capabilities.SupportedTvSearchParameters.Contains(v)) && + new[] { "season", "ep" }.All(v => capabilities.SupportedTvSearchParameters.Contains(v))) + { + return null; + } + + return new ValidationFailure(string.Empty, "Indexer does not support required search parameters"); + } + catch (Exception ex) + { + _logger.WarnException("Unable to connect to indexer: " + ex.Message, ex); + + return new ValidationFailure(string.Empty, "Unable to connect to indexer, check the log for more details"); + } + + return null; + } } } diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilities.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilities.cs new file mode 100644 index 000000000..0c25c7802 --- /dev/null +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilities.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace NzbDrone.Core.Indexers.Newznab +{ + public class NewznabCapabilities + { + public string[] SupportedSearchParameters { get; set; } + public string[] SupportedTvSearchParameters { get; set; } + public List Categories { get; set; } + + public NewznabCapabilities() + { + SupportedSearchParameters = new[] { "q" }; + SupportedTvSearchParameters = new[] { "q", "rid", "season", "ep" }; // This should remain 'rid' for older newznab installs. + Categories = new List(); + } + } + + public class NewznabCategory + { + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + + public List Subcategories { get; set; } + } +} diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilitiesProvider.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs similarity index 81% rename from src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilitiesProvider.cs rename to src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs index 3d5cef1ca..d415074a4 100644 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilitiesProvider.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs @@ -7,27 +7,27 @@ using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Common.Serializer; -namespace NzbDrone.Core.Indexers.Torznab +namespace NzbDrone.Core.Indexers.Newznab { - public interface ITorznabCapabilitiesProvider + public interface INewznabCapabilitiesProvider { - TorznabCapabilities GetCapabilities(TorznabSettings settings); + NewznabCapabilities GetCapabilities(NewznabSettings settings); } - public class TorznabCapabilitiesProvider : ITorznabCapabilitiesProvider + public class NewznabCapabilitiesProvider : INewznabCapabilitiesProvider { - private readonly ICached _capabilitiesCache; + private readonly ICached _capabilitiesCache; private readonly IHttpClient _httpClient; private readonly Logger _logger; - public TorznabCapabilitiesProvider(ICacheManager cacheManager, IHttpClient httpClient, Logger logger) + public NewznabCapabilitiesProvider(ICacheManager cacheManager, IHttpClient httpClient, Logger logger) { - _capabilitiesCache = cacheManager.GetCache(GetType()); + _capabilitiesCache = cacheManager.GetCache(GetType()); _httpClient = httpClient; _logger = logger; } - public TorznabCapabilities GetCapabilities(TorznabSettings indexerSettings) + public NewznabCapabilities GetCapabilities(NewznabSettings indexerSettings) { var key = indexerSettings.ToJson(); var capabilities = _capabilitiesCache.Get(key, () => FetchCapabilities(indexerSettings), TimeSpan.FromDays(7)); @@ -35,9 +35,9 @@ namespace NzbDrone.Core.Indexers.Torznab return capabilities; } - private TorznabCapabilities FetchCapabilities(TorznabSettings indexerSettings) + private NewznabCapabilities FetchCapabilities(NewznabSettings indexerSettings) { - var capabilities = new TorznabCapabilities(); + var capabilities = new NewznabCapabilities(); var url = string.Format("{0}/api?t=caps", indexerSettings.Url.TrimEnd('/')); @@ -62,9 +62,9 @@ namespace NzbDrone.Core.Indexers.Torznab return capabilities; } - private TorznabCapabilities ParseCapabilities(HttpResponse response) + private NewznabCapabilities ParseCapabilities(HttpResponse response) { - var capabilities = new TorznabCapabilities(); + var capabilities = new NewznabCapabilities(); var xmlRoot = XDocument.Parse(response.Content).Element("caps"); @@ -97,17 +97,17 @@ namespace NzbDrone.Core.Indexers.Torznab { foreach (var xmlCategory in xmlCategories.Elements("category")) { - var cat = new TorznabCategory + var cat = new NewznabCategory { Id = int.Parse(xmlCategory.Attribute("id").Value), Name = xmlCategory.Attribute("name").Value, Description = xmlCategory.Attribute("description") != null ? xmlCategory.Attribute("description").Value : string.Empty, - Subcategories = new List() + Subcategories = new List() }; foreach (var xmlSubcat in xmlCategory.Elements("subcat")) { - cat.Subcategories.Add(new TorznabCategory + cat.Subcategories.Add(new NewznabCategory { Id = int.Parse(xmlSubcat.Attribute("id").Value), Name = xmlSubcat.Attribute("name").Value, diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabRequestGenerator.cs index 1091f948b..61ca41b12 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabRequestGenerator.cs @@ -9,21 +9,79 @@ namespace NzbDrone.Core.Indexers.Newznab { public class NewznabRequestGenerator : IIndexerRequestGenerator { - public Int32 MaxPages { get; set; } - public Int32 PageSize { get; set; } + private readonly INewznabCapabilitiesProvider _capabilitiesProvider; + public int MaxPages { get; set; } + public int PageSize { get; set; } public NewznabSettings Settings { get; set; } - public NewznabRequestGenerator() + public NewznabRequestGenerator(INewznabCapabilitiesProvider capabilitiesProvider) { + _capabilitiesProvider = capabilitiesProvider; + MaxPages = 30; PageSize = 100; } + private bool SupportsSearch + { + get + { + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + return capabilities.SupportedSearchParameters != null && + capabilities.SupportedSearchParameters.Contains("q"); + } + } + + private bool SupportsTvSearch + { + get + { + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + return capabilities.SupportedTvSearchParameters != null && + capabilities.SupportedTvSearchParameters.Contains("q") && + capabilities.SupportedTvSearchParameters.Contains("season") && + capabilities.SupportedTvSearchParameters.Contains("ep"); + } + } + + private bool SupportsTvdbSearch + { + get + { + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + return capabilities.SupportedTvSearchParameters != null && + capabilities.SupportedTvSearchParameters.Contains("tvdbid") && + capabilities.SupportedTvSearchParameters.Contains("season") && + capabilities.SupportedTvSearchParameters.Contains("ep"); + } + } + + private bool SupportsTvRageSearch + { + get + { + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + return capabilities.SupportedTvSearchParameters != null && + capabilities.SupportedTvSearchParameters.Contains("rid") && + capabilities.SupportedTvSearchParameters.Contains("season") && + capabilities.SupportedTvSearchParameters.Contains("ep"); + } + } + public virtual IList> GetRecentRequests() { var pageableRequests = new List>(); - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "tvsearch", "")); + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); + + if (capabilities.SupportedTvSearchParameters != null) + { + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "tvsearch", "")); + } return pageableRequests; } @@ -32,20 +90,28 @@ namespace NzbDrone.Core.Indexers.Newznab { var pageableRequests = new List>(); - if (searchCriteria.Series.TvRageId > 0) + if (searchCriteria.Series.TvdbId > 0 && SupportsTvdbSearch) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&rid={0}&season={1}&ep={2}", + string.Format("&tvdbid={0}&season={1}&ep={2}", + searchCriteria.Series.TvdbId, + searchCriteria.SeasonNumber, + searchCriteria.EpisodeNumber))); + } + else if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) + { + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", + string.Format("&rid={0}&season={1}&ep={2}", searchCriteria.Series.TvRageId, searchCriteria.SeasonNumber, searchCriteria.EpisodeNumber))); } - else + else if (SupportsTvSearch) { foreach (var queryTitle in searchCriteria.QueryTitles) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&q={0}&season={1}&ep={2}", + string.Format("&q={0}&season={1}&ep={2}", NewsnabifyTitle(queryTitle), searchCriteria.SeasonNumber, searchCriteria.EpisodeNumber))); @@ -59,19 +125,26 @@ namespace NzbDrone.Core.Indexers.Newznab { var pageableRequests = new List>(); - if (searchCriteria.Series.TvRageId > 0) + if (searchCriteria.Series.TvdbId > 0 && SupportsTvdbSearch) + { + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", + string.Format("&tvdbid={0}&season={1}", + searchCriteria.Series.TvdbId, + searchCriteria.SeasonNumber))); + } + else if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&rid={0}&season={1}", + string.Format("&rid={0}&season={1}", searchCriteria.Series.TvRageId, searchCriteria.SeasonNumber))); } - else + else if (SupportsTvSearch) { foreach (var queryTitle in searchCriteria.QueryTitles) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&q={0}&season={1}", + string.Format("&q={0}&season={1}", NewsnabifyTitle(queryTitle), searchCriteria.SeasonNumber))); } @@ -84,19 +157,26 @@ namespace NzbDrone.Core.Indexers.Newznab { var pageableRequests = new List>(); - if (searchCriteria.Series.TvRageId > 0) + if (searchCriteria.Series.TvdbId > 0 && SupportsTvdbSearch) + { + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", + string.Format("&tvdbid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", + searchCriteria.Series.TvdbId, + searchCriteria.AirDate))); + } + else if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", + string.Format("&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", searchCriteria.Series.TvRageId, searchCriteria.AirDate))); } - else + else if (SupportsTvSearch) { foreach (var queryTitle in searchCriteria.QueryTitles) { pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - String.Format("&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", + string.Format("&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", NewsnabifyTitle(queryTitle), searchCriteria.AirDate))); } @@ -109,12 +189,15 @@ namespace NzbDrone.Core.Indexers.Newznab { var pageableRequests = new List>(); - foreach (var queryTitle in searchCriteria.QueryTitles) + if (SupportsSearch) { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.AnimeCategories, "search", - String.Format("&q={0}+{1:00}", - NewsnabifyTitle(queryTitle), - searchCriteria.AbsoluteEpisodeNumber))); + foreach (var queryTitle in searchCriteria.QueryTitles) + { + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.AnimeCategories, "search", + string.Format("&q={0}+{1:00}", + NewsnabifyTitle(queryTitle), + searchCriteria.AbsoluteEpisodeNumber))); + } } return pageableRequests; @@ -124,29 +207,32 @@ namespace NzbDrone.Core.Indexers.Newznab { var pageableRequests = new List>(); - foreach (var queryTitle in searchCriteria.EpisodeQueryTitles) + if (SupportsSearch) { - var query = queryTitle.Replace('+', ' '); - query = System.Web.HttpUtility.UrlEncode(query); + foreach (var queryTitle in searchCriteria.EpisodeQueryTitles) + { + var query = queryTitle.Replace('+', ' '); + query = System.Web.HttpUtility.UrlEncode(query); - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "search", - String.Format("&q={0}", - query))); + pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "search", + string.Format("&q={0}", + query))); + } } return pageableRequests; } - private IEnumerable GetPagedRequests(Int32 maxPages, IEnumerable categories, String searchType, String parameters) + private IEnumerable GetPagedRequests(int maxPages, IEnumerable categories, string searchType, string parameters) { if (categories.Empty()) { yield break; } - var categoriesQuery = String.Join(",", categories.Distinct()); + var categoriesQuery = string.Join(",", categories.Distinct()); - var baseUrl = String.Format("{0}/api?t={1}&cat={2}&extended=1{3}", Settings.Url.TrimEnd('/'), searchType, categoriesQuery, Settings.AdditionalParameters); + var baseUrl = string.Format("{0}/api?t={1}&cat={2}&extended=1{3}", Settings.Url.TrimEnd('/'), searchType, categoriesQuery, Settings.AdditionalParameters); if (Settings.ApiKey.IsNotNullOrWhiteSpace()) { @@ -155,18 +241,18 @@ namespace NzbDrone.Core.Indexers.Newznab if (PageSize == 0) { - yield return new IndexerRequest(String.Format("{0}{1}", baseUrl, parameters), HttpAccept.Rss); + yield return new IndexerRequest(string.Format("{0}{1}", baseUrl, parameters), HttpAccept.Rss); } else { for (var page = 0; page < maxPages; page++) { - yield return new IndexerRequest(String.Format("{0}&offset={1}&limit={2}{3}", baseUrl, page * PageSize, PageSize, parameters), HttpAccept.Rss); + yield return new IndexerRequest(string.Format("{0}&offset={1}&limit={2}{3}", baseUrl, page * PageSize, PageSize, parameters), HttpAccept.Rss); } } } - private static String NewsnabifyTitle(String title) + private static string NewsnabifyTitle(string title) { return title.Replace("+", "%20"); } diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs index cee878b04..77c110faf 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabRssParser.cs @@ -44,6 +44,7 @@ namespace NzbDrone.Core.Indexers.Newznab { releaseInfo = base.ProcessItem(item, releaseInfo); + releaseInfo.TvdbId = GetTvdbId(item); releaseInfo.TvRageId = GetTvRageId(item); return releaseInfo; @@ -97,6 +98,19 @@ namespace NzbDrone.Core.Indexers.Newznab return url; } + protected virtual int GetTvdbId(XElement item) + { + var tvdbIdString = TryGetNewznabAttribute(item, "tvdbid"); + int tvdbId; + + if (!tvdbIdString.IsNullOrWhiteSpace() && int.TryParse(tvdbIdString, out tvdbId)) + { + return tvdbId; + } + + return 0; + } + protected virtual int GetTvRageId(XElement item) { var tvRageIdString = TryGetNewznabAttribute(item, "rageid"); diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs index 534ed366f..d590e0c5b 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs @@ -61,24 +61,24 @@ namespace NzbDrone.Core.Indexers.Newznab public NewznabSettings() { - Categories = new[] {5030, 5040}; - AnimeCategories = Enumerable.Empty(); + Categories = new[] { 5030, 5040 }; + AnimeCategories = Enumerable.Empty(); } [FieldDefinition(0, Label = "URL")] - public String Url { get; set; } + public string Url { get; set; } [FieldDefinition(1, Label = "API Key")] - public String ApiKey { get; set; } + public string ApiKey { get; set; } [FieldDefinition(2, Label = "Categories", HelpText = "Comma Separated list, leave blank to disable standard/daily shows", Advanced = true)] - public IEnumerable Categories { get; set; } + public IEnumerable Categories { get; set; } [FieldDefinition(3, Label = "Anime Categories", HelpText = "Comma Separated list, leave blank to disable anime", Advanced = true)] - public IEnumerable AnimeCategories { get; set; } + public IEnumerable AnimeCategories { get; set; } - [FieldDefinition(4, Label = "Additional Parameters", HelpText = "Additional newznab parameters", Advanced = true)] - public String AdditionalParameters { get; set; } + [FieldDefinition(4, Label = "Additional Parameters", HelpText = "Additional Newznab parameters", Advanced = true)] + public string AdditionalParameters { get; set; } public NzbDroneValidationResult Validate() { diff --git a/src/NzbDrone.Core/Indexers/Rarbg/RarbgParser.cs b/src/NzbDrone.Core/Indexers/Rarbg/RarbgParser.cs index 93ea2e8fe..58d0b1957 100644 --- a/src/NzbDrone.Core/Indexers/Rarbg/RarbgParser.cs +++ b/src/NzbDrone.Core/Indexers/Rarbg/RarbgParser.cs @@ -57,10 +57,18 @@ namespace NzbDrone.Core.Indexers.Rarbg torrentInfo.PublishDate = torrent.pubdate; torrentInfo.Seeders = torrent.seeders; torrentInfo.Peers = torrent.leechers + torrent.seeders; - - if (torrent.episode_info != null && torrent.episode_info.tvrage != null) + + if (torrent.episode_info != null) { - torrentInfo.TvRageId = torrent.episode_info.tvrage.Value; + if (torrent.episode_info.tvdb != null) + { + torrentInfo.TvdbId = torrent.episode_info.tvdb.Value; + } + + if (torrent.episode_info.tvrage != null) + { + torrentInfo.TvRageId = torrent.episode_info.tvrage.Value; + } } results.Add(torrentInfo); diff --git a/src/NzbDrone.Core/Indexers/Torznab/Torznab.cs b/src/NzbDrone.Core/Indexers/Torznab/Torznab.cs index e4550e8df..cfeb5e6c6 100644 --- a/src/NzbDrone.Core/Indexers/Torznab/Torznab.cs +++ b/src/NzbDrone.Core/Indexers/Torznab/Torznab.cs @@ -6,6 +6,7 @@ using NLog; using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Parser; using NzbDrone.Core.ThingiProvider; @@ -13,7 +14,7 @@ namespace NzbDrone.Core.Indexers.Torznab { public class Torznab : HttpIndexerBase { - private readonly ITorznabCapabilitiesProvider _torznabCapabilitiesProvider; + private readonly INewznabCapabilitiesProvider _capabilitiesProvider; public override string Name { @@ -28,7 +29,7 @@ namespace NzbDrone.Core.Indexers.Torznab public override IIndexerRequestGenerator GetRequestGenerator() { - return new TorznabRequestGenerator(_torznabCapabilitiesProvider) + return new NewznabRequestGenerator(_capabilitiesProvider) { PageSize = PageSize, Settings = Settings @@ -48,10 +49,10 @@ namespace NzbDrone.Core.Indexers.Torznab } } - public Torznab(ITorznabCapabilitiesProvider torznabCapabilitiesProvider, IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger) + public Torznab(INewznabCapabilitiesProvider capabilitiesProvider, IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger) : base(httpClient, indexerStatusService, configService, parsingService, logger) { - _torznabCapabilitiesProvider = torznabCapabilitiesProvider; + _capabilitiesProvider = capabilitiesProvider; } private IndexerDefinition GetDefinition(string name, TorznabSettings settings) @@ -92,7 +93,7 @@ namespace NzbDrone.Core.Indexers.Torznab { try { - var capabilities = _torznabCapabilitiesProvider.GetCapabilities(Settings); + var capabilities = _capabilitiesProvider.GetCapabilities(Settings); if (capabilities.SupportedSearchParameters != null && capabilities.SupportedSearchParameters.Contains("q")) { @@ -100,8 +101,8 @@ namespace NzbDrone.Core.Indexers.Torznab } if (capabilities.SupportedTvSearchParameters != null && - (capabilities.SupportedSearchParameters.Contains("q") || capabilities.SupportedSearchParameters.Contains("rid")) && - capabilities.SupportedTvSearchParameters.Contains("season") && capabilities.SupportedTvSearchParameters.Contains("ep")) + new[] { "q", "tvdbid", "rid" }.Any(v => capabilities.SupportedTvSearchParameters.Contains(v)) && + new[] { "season", "ep" }.All(v => capabilities.SupportedTvSearchParameters.Contains(v))) { return null; } diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilities.cs b/src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilities.cs deleted file mode 100644 index f09c36477..000000000 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabCapabilities.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace NzbDrone.Core.Indexers.Torznab -{ - public class TorznabCapabilities - { - public string[] SupportedSearchParameters { get; set; } - public string[] SupportedTvSearchParameters { get; set; } - public List Categories { get; set; } - - public TorznabCapabilities() - { - SupportedSearchParameters = new[] { "q", "offset", "limit" }; - SupportedTvSearchParameters = new[] { "q", "rid", "season", "ep", "offset", "limit" }; - Categories = new List(); - } - } - - public class TorznabCategory - { - public int Id { get; set; } - public string Name { get; set; } - public string Description { get; set; } - - public List Subcategories { get; set; } - } -} diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabException.cs b/src/NzbDrone.Core/Indexers/Torznab/TorznabException.cs index 7f0f3205a..258a12492 100644 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabException.cs +++ b/src/NzbDrone.Core/Indexers/Torznab/TorznabException.cs @@ -8,8 +8,7 @@ namespace NzbDrone.Core.Indexers.Torznab { } - public TorznabException(string message) - : base(message) + public TorznabException(string message) : base(message) { } } diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Torznab/TorznabRequestGenerator.cs deleted file mode 100644 index 6d9315c12..000000000 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabRequestGenerator.cs +++ /dev/null @@ -1,228 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using NzbDrone.Common.Extensions; -using NzbDrone.Common.Http; -using NzbDrone.Core.IndexerSearch.Definitions; - -namespace NzbDrone.Core.Indexers.Torznab -{ - public class TorznabRequestGenerator : IIndexerRequestGenerator - { - private readonly ITorznabCapabilitiesProvider _capabilitiesProvider; - - public int MaxPages { get; set; } - public int PageSize { get; set; } - - public TorznabSettings Settings { get; set; } - - public TorznabRequestGenerator(ITorznabCapabilitiesProvider capabilitiesProvider) - { - _capabilitiesProvider = capabilitiesProvider; - - MaxPages = 30; - PageSize = 100; - } - - private bool SupportsSearch - { - get - { - var capabilities = _capabilitiesProvider.GetCapabilities(Settings); - - return capabilities.SupportedSearchParameters != null - && capabilities.SupportedSearchParameters.Contains("q"); - } - } - - private bool SupportsTvSearch - { - get - { - var capabilities = _capabilitiesProvider.GetCapabilities(Settings); - - return capabilities.SupportedTvSearchParameters != null - && capabilities.SupportedTvSearchParameters.Contains("q") - && capabilities.SupportedTvSearchParameters.Contains("season") - && capabilities.SupportedTvSearchParameters.Contains("ep"); - } - } - - private bool SupportsTvRageSearch - { - get - { - var capabilities = _capabilitiesProvider.GetCapabilities(Settings); - - return capabilities.SupportedTvSearchParameters != null - && capabilities.SupportedTvSearchParameters.Contains("rid") - && capabilities.SupportedTvSearchParameters.Contains("season") - && capabilities.SupportedTvSearchParameters.Contains("ep") - && Settings.EnableRageIDLookup; - } - } - - public virtual IList> GetRecentRequests() - { - var pageableRequests = new List>(); - - var capabilities = _capabilitiesProvider.GetCapabilities(Settings); - - if (capabilities.SupportedTvSearchParameters != null) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "tvsearch", "")); - } - - return pageableRequests; - } - - public virtual IList> GetSearchRequests(SingleEpisodeSearchCriteria searchCriteria) - { - var pageableRequests = new List>(); - - if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&rid={0}&season={1}&ep={2}", - searchCriteria.Series.TvRageId, - searchCriteria.SeasonNumber, - searchCriteria.EpisodeNumber))); - } - else if (SupportsTvSearch) - { - foreach (var queryTitle in searchCriteria.QueryTitles) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&q={0}&season={1}&ep={2}", - NewsnabifyTitle(queryTitle), - searchCriteria.SeasonNumber, - searchCriteria.EpisodeNumber))); - } - } - - return pageableRequests; - } - - public virtual IList> GetSearchRequests(SeasonSearchCriteria searchCriteria) - { - var pageableRequests = new List>(); - - if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&rid={0}&season={1}", - searchCriteria.Series.TvRageId, - searchCriteria.SeasonNumber))); - } - else if (SupportsTvSearch) - { - foreach (var queryTitle in searchCriteria.QueryTitles) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&q={0}&season={1}", - NewsnabifyTitle(queryTitle), - searchCriteria.SeasonNumber))); - } - } - - return pageableRequests; - } - - public virtual IList> GetSearchRequests(DailyEpisodeSearchCriteria searchCriteria) - { - var pageableRequests = new List>(); - - if (searchCriteria.Series.TvRageId > 0 && SupportsTvRageSearch) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", - searchCriteria.Series.TvRageId, - searchCriteria.AirDate))); - } - else if (SupportsTvSearch) - { - foreach (var queryTitle in searchCriteria.QueryTitles) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch", - string.Format("&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}", - NewsnabifyTitle(queryTitle), - searchCriteria.AirDate))); - } - } - - return pageableRequests; - } - - public virtual IList> GetSearchRequests(AnimeEpisodeSearchCriteria searchCriteria) - { - var pageableRequests = new List>(); - - if (SupportsSearch) - { - foreach (var queryTitle in searchCriteria.QueryTitles) - { - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.AnimeCategories, "search", - string.Format("&q={0}+{1:00}", - NewsnabifyTitle(queryTitle), - searchCriteria.AbsoluteEpisodeNumber))); - } - } - - return pageableRequests; - } - - public virtual IList> GetSearchRequests(SpecialEpisodeSearchCriteria searchCriteria) - { - var pageableRequests = new List>(); - - if (SupportsSearch) - { - foreach (var queryTitle in searchCriteria.EpisodeQueryTitles) - { - var query = queryTitle.Replace('+', ' '); - query = System.Web.HttpUtility.UrlEncode(query); - - pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "search", - string.Format("&q={0}", - query))); - } - } - - return pageableRequests; - } - - private IEnumerable GetPagedRequests(int maxPages, IEnumerable categories, string searchType, string parameters) - { - if (categories.Empty()) - { - yield break; - } - - var categoriesQuery = string.Join(",", categories.Distinct()); - - var baseUrl = string.Format("{0}/api?t={1}&cat={2}&extended=1{3}", Settings.Url.TrimEnd('/'), searchType, categoriesQuery, Settings.AdditionalParameters); - - if (Settings.ApiKey.IsNotNullOrWhiteSpace()) - { - baseUrl += "&apikey=" + Settings.ApiKey; - } - - if (PageSize == 0) - { - yield return new IndexerRequest(string.Format("{0}{1}", baseUrl, parameters), HttpAccept.Rss); - } - else - { - for (var page = 0; page < maxPages; page++) - { - yield return new IndexerRequest(string.Format("{0}&offset={1}&limit={2}{3}", baseUrl, page * PageSize, PageSize, parameters), HttpAccept.Rss); - } - } - } - - private static string NewsnabifyTitle(string title) - { - return title.Replace("+", "%20"); - } - } -} diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabRssParser.cs b/src/NzbDrone.Core/Indexers/Torznab/TorznabRssParser.cs index 0c82a7ab5..246056e66 100644 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabRssParser.cs +++ b/src/NzbDrone.Core/Indexers/Torznab/TorznabRssParser.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Core.Indexers.Torznab protected override bool PreProcess(IndexerResponse indexerResponse) { - var xdoc = XDocument.Parse(indexerResponse.Content); + var xdoc = LoadXmlDocument(indexerResponse); var error = xdoc.Descendants("error").FirstOrDefault(); if (error == null) return true; @@ -40,6 +40,7 @@ namespace NzbDrone.Core.Indexers.Torznab { var torrentInfo = base.ProcessItem(item, releaseInfo) as TorrentInfo; + torrentInfo.TvdbId = GetTvdbId(item); torrentInfo.TvRageId = GetTvRageId(item); return torrentInfo; @@ -67,12 +68,12 @@ namespace NzbDrone.Core.Indexers.Torznab return ParseUrl(item.TryGetValue("comments")); } - protected override Int64 GetSize(XElement item) + protected override long GetSize(XElement item) { - Int64 size; + long size; var sizeString = TryGetTorznabAttribute(item, "size"); - if (!sizeString.IsNullOrWhiteSpace() && Int64.TryParse(sizeString, out size)) + if (!sizeString.IsNullOrWhiteSpace() && long.TryParse(sizeString, out size)) { return size; } @@ -99,12 +100,25 @@ namespace NzbDrone.Core.Indexers.Torznab return url; } - protected virtual Int32 GetTvRageId(XElement item) + protected virtual int GetTvdbId(XElement item) + { + var tvdbIdString = TryGetTorznabAttribute(item, "tvdbid"); + int tvdbId; + + if (!tvdbIdString.IsNullOrWhiteSpace() && int.TryParse(tvdbIdString, out tvdbId)) + { + return tvdbId; + } + + return 0; + } + + protected virtual int GetTvRageId(XElement item) { var tvRageIdString = TryGetTorznabAttribute(item, "rageid"); - Int32 tvRageId; + int tvRageId; - if (!tvRageIdString.IsNullOrWhiteSpace() && Int32.TryParse(tvRageIdString, out tvRageId)) + if (!tvRageIdString.IsNullOrWhiteSpace() && int.TryParse(tvRageIdString, out tvRageId)) { return tvRageId; } @@ -121,25 +135,25 @@ namespace NzbDrone.Core.Indexers.Torznab return TryGetTorznabAttribute(item, "magneturl"); } - protected override Int32? GetSeeders(XElement item) + protected override int? GetSeeders(XElement item) { var seeders = TryGetTorznabAttribute(item, "seeders"); if (seeders.IsNotNullOrWhiteSpace()) { - return Int32.Parse(seeders); + return int.Parse(seeders); } return base.GetSeeders(item); } - protected override Int32? GetPeers(XElement item) + protected override int? GetPeers(XElement item) { var peers = TryGetTorznabAttribute(item, "peers"); if (peers.IsNotNullOrWhiteSpace()) { - return Int32.Parse(peers); + return int.Parse(peers); } var seeders = TryGetTorznabAttribute(item, "seeders"); @@ -147,7 +161,7 @@ namespace NzbDrone.Core.Indexers.Torznab if (seeders.IsNotNullOrWhiteSpace() && leechers.IsNotNullOrWhiteSpace()) { - return Int32.Parse(seeders) + Int32.Parse(leechers); + return int.Parse(seeders) + int.Parse(leechers); } return base.GetPeers(item); diff --git a/src/NzbDrone.Core/Indexers/Torznab/TorznabSettings.cs b/src/NzbDrone.Core/Indexers/Torznab/TorznabSettings.cs index 55be40bed..e20d7d0d9 100644 --- a/src/NzbDrone.Core/Indexers/Torznab/TorznabSettings.cs +++ b/src/NzbDrone.Core/Indexers/Torznab/TorznabSettings.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using FluentValidation; +using FluentValidation.Results; using NzbDrone.Common.Extensions; using NzbDrone.Core.Annotations; +using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; @@ -14,7 +16,6 @@ namespace NzbDrone.Core.Indexers.Torznab { private static readonly string[] ApiKeyWhiteList = { - "hdaccess.net", "hd4free.xyz", }; @@ -32,46 +33,27 @@ namespace NzbDrone.Core.Indexers.Torznab public TorznabSettingsValidator() { + Custom(newznab => + { + if (newznab.Categories.Empty() && newznab.AnimeCategories.Empty()) + { + return new ValidationFailure("", "Either 'Categories' or 'Anime Categories' must be provided"); + } + + return null; + }); + RuleFor(c => c.Url).ValidRootUrl(); RuleFor(c => c.ApiKey).NotEmpty().When(ShouldHaveApiKey); - RuleFor(c => c.Categories).NotEmpty().When(c => !c.AnimeCategories.Any()); - RuleFor(c => c.AnimeCategories).NotEmpty().When(c => !c.Categories.Any()); - RuleFor(c => c.AdditionalParameters) - .Matches(AdditionalParametersRegex) - .When(c => !c.AdditionalParameters.IsNullOrWhiteSpace()); + RuleFor(c => c.AdditionalParameters).Matches(AdditionalParametersRegex) + .When(c => !c.AdditionalParameters.IsNullOrWhiteSpace()); } } - public class TorznabSettings : IProviderConfig + public class TorznabSettings : NewznabSettings { private static readonly TorznabSettingsValidator Validator = new TorznabSettingsValidator(); - public TorznabSettings() - { - Categories = new[] { 5030, 5040 }; - AnimeCategories = Enumerable.Empty(); - EnableRageIDLookup = true; - } - - [FieldDefinition(0, Label = "URL")] - public string Url { get; set; } - - [FieldDefinition(1, Label = "API Key")] - public string ApiKey { get; set; } - - [FieldDefinition(2, Label = "Categories", HelpText = "Comma Separated list, leave blank to disable standard/daily shows", Advanced = true)] - public IEnumerable Categories { get; set; } - - [FieldDefinition(3, Label = "Anime Categories", HelpText = "Comma Separated list, leave blank to disable anime", Advanced = true)] - public IEnumerable AnimeCategories { get; set; } - - [FieldDefinition(4, Label = "Additional Parameters", HelpText = "Additional Torznab parameters", Advanced = true)] - public string AdditionalParameters { get; set; } - - // TODO: To be removed in the next version. - [FieldDefinition(5, Type = FieldType.Checkbox, Label = "Enable RageID Lookup", HelpText = "Disable this if your tracker doesn't have tvrage ids, Sonarr will then use (more expensive) title queries.", Advanced = true)] - public bool EnableRageIDLookup { get; set; } - public NzbDroneValidationResult Validate() { return new NzbDroneValidationResult(Validator.Validate(this)); diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 75c4b246f..620ae5dc8 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -519,6 +519,8 @@ + + @@ -558,11 +560,8 @@ - - - diff --git a/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs b/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs index 35ab34d27..6846dc882 100644 --- a/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs +++ b/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs @@ -15,6 +15,7 @@ namespace NzbDrone.Core.Parser.Model public int IndexerId { get; set; } public string Indexer { get; set; } public DownloadProtocol DownloadProtocol { get; set; } + public int TvdbId { get; set; } public int TvRageId { get; set; } public DateTime PublishDate { get; set; } @@ -73,6 +74,7 @@ namespace NzbDrone.Core.Parser.Model stringBuilder.AppendLine("Indexer: " + Indexer ?? "Empty"); stringBuilder.AppendLine("CommentUrl: " + CommentUrl ?? "Empty"); stringBuilder.AppendLine("DownloadProtocol: " + DownloadProtocol ?? "Empty"); + stringBuilder.AppendLine("TvdbId: " + TvdbId ?? "Empty"); stringBuilder.AppendLine("TvRageId: " + TvRageId ?? "Empty"); stringBuilder.AppendLine("PublishDate: " + PublishDate ?? "Empty"); return stringBuilder.ToString(); diff --git a/src/NzbDrone.Core/Parser/ParsingService.cs b/src/NzbDrone.Core/Parser/ParsingService.cs index de5e5f63f..b703d09d2 100644 --- a/src/NzbDrone.Core/Parser/ParsingService.cs +++ b/src/NzbDrone.Core/Parser/ParsingService.cs @@ -16,10 +16,10 @@ namespace NzbDrone.Core.Parser LocalEpisode GetLocalEpisode(string filename, Series series); LocalEpisode GetLocalEpisode(string filename, Series series, ParsedEpisodeInfo folderInfo, bool sceneSource); Series GetSeries(string title); - RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, Int32 tvRageId = 0, SearchCriteriaBase searchCriteria = null); - RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, Int32 seriesId, IEnumerable episodeIds); + RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvdbId, int tvRageId, SearchCriteriaBase searchCriteria = null); + RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int seriesId, IEnumerable episodeIds); List GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series, bool sceneSource, SearchCriteriaBase searchCriteria = null); - ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, int tvRageId, SearchCriteriaBase searchCriteria = null); + ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, int tvdbId, int tvRageId, SearchCriteriaBase searchCriteria = null); } public class ParsingService : IParsingService @@ -109,14 +109,14 @@ namespace NzbDrone.Core.Parser return series; } - public RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, Int32 tvRageId, SearchCriteriaBase searchCriteria = null) + public RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvdbId, int tvRageId, SearchCriteriaBase searchCriteria = null) { var remoteEpisode = new RemoteEpisode { ParsedEpisodeInfo = parsedEpisodeInfo, }; - var series = GetSeries(parsedEpisodeInfo, tvRageId, searchCriteria); + var series = GetSeries(parsedEpisodeInfo, tvdbId, tvRageId, searchCriteria); if (series == null) { @@ -286,20 +286,19 @@ namespace NzbDrone.Core.Parser return result; } - public ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, int tvRageId, SearchCriteriaBase searchCriteria = null) + public ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, int tvdbId, int tvRageId, SearchCriteriaBase searchCriteria = null) { if (searchCriteria != null) { - var tvdbId = _sceneMappingService.FindTvdbId(title); - if (tvdbId.HasValue) + if (tvdbId == 0) + tvdbId = _sceneMappingService.FindTvdbId(title) ?? 0; + + if (tvdbId != 0 && tvdbId == searchCriteria.Series.TvdbId) { - if (searchCriteria.Series.TvdbId == tvdbId) - { - return ParseSpecialEpisodeTitle(title, searchCriteria.Series); - } + return ParseSpecialEpisodeTitle(title, searchCriteria.Series); } - if (tvRageId == searchCriteria.Series.TvRageId) + if (tvRageId != 0 && tvRageId == searchCriteria.Series.TvRageId) { return ParseSpecialEpisodeTitle(title, searchCriteria.Series); } @@ -310,6 +309,10 @@ namespace NzbDrone.Core.Parser { series = _seriesService.FindByTitleInexact(title); } + if (series == null && tvdbId > 0) + { + series = _seriesService.FindByTvdbId(tvdbId); + } if (series == null && tvRageId > 0) { series = _seriesService.FindByTvRageId(tvRageId); @@ -351,20 +354,19 @@ namespace NzbDrone.Core.Parser return null; } - private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria) + private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvdbId, int tvRageId, SearchCriteriaBase searchCriteria) { Series series = null; - var tvdbId = _sceneMappingService.FindTvdbId(parsedEpisodeInfo.SeriesTitle); - - if (tvdbId.HasValue) + var sceneMappingTvdbId = _sceneMappingService.FindTvdbId(parsedEpisodeInfo.SeriesTitle); + if (sceneMappingTvdbId.HasValue) { - if (searchCriteria != null && searchCriteria.Series.TvdbId == tvdbId) + if (searchCriteria != null && searchCriteria.Series.TvdbId == sceneMappingTvdbId.Value) { return searchCriteria.Series; } - series = _seriesService.FindByTvdbId(tvdbId.Value); + series = _seriesService.FindByTvdbId(sceneMappingTvdbId.Value); if (series == null) { @@ -382,6 +384,12 @@ namespace NzbDrone.Core.Parser return searchCriteria.Series; } + if (tvdbId > 0 && tvdbId == searchCriteria.Series.TvdbId) + { + //TODO: If series is found by TvdbId, we should report it as a scene naming exception, since it will fail to import + return searchCriteria.Series; + } + if (tvRageId > 0 && tvRageId == searchCriteria.Series.TvRageId) { //TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import @@ -391,6 +399,12 @@ namespace NzbDrone.Core.Parser series = _seriesService.FindByTitle(parsedEpisodeInfo.SeriesTitle); + if (series == null && tvdbId > 0) + { + //TODO: If series is found by TvdbId, we should report it as a scene naming exception, since it will fail to import + series = _seriesService.FindByTvdbId(tvdbId); + } + if (series == null && tvRageId > 0) { //TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import