From 1f5bcfeb75b65897991eac1c9b6e0e8d0306f431 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 22 Oct 2013 23:36:31 -0700 Subject: [PATCH] Blacklist is now used when processing results --- .../UpgradeHistorySpecificationFixture.cs | 15 ++++++- src/NzbDrone.Core/Blacklisting/Blacklist.cs | 15 +++++++ .../Blacklisting/BlacklistRepository.cs | 23 ++++++++++ .../Blacklisting/BlacklistService.cs | 43 +++++++++++++++++++ .../Migration/028_add_blacklist_table.cs | 19 ++++++++ src/NzbDrone.Core/Datastore/TableMapping.cs | 3 ++ .../Specifications/BlacklistSpecification.cs | 39 +++++++++++++++++ .../RssSync/UpgradeHistorySpecification.cs | 15 ++++++- src/NzbDrone.Core/History/History.cs | 2 - src/NzbDrone.Core/NzbDrone.Core.csproj | 5 +++ 10 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Core/Blacklisting/Blacklist.cs create mode 100644 src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs create mode 100644 src/NzbDrone.Core/Blacklisting/BlacklistService.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/028_add_blacklist_table.cs create mode 100644 src/NzbDrone.Core/DecisionEngine/Specifications/BlacklistSpecification.cs diff --git a/src/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs b/src/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs index 70e81dc8a..fcb932d00 100644 --- a/src/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs @@ -3,6 +3,8 @@ using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.DecisionEngine.Specifications.RssSync; +using NzbDrone.Core.Download; +using NzbDrone.Core.Download.Clients.Sabnzbd; using NzbDrone.Core.History; using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.Parser.Model; @@ -64,6 +66,9 @@ namespace NzbDrone.Core.Test.DecisionEngineTests Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(_notupgradableQuality); Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(2)).Returns(_notupgradableQuality); Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(3)).Returns(null); + + Mocker.GetMock() + .Setup(c => c.GetDownloadClient()).Returns(Mocker.GetMock().Object); } private void WithFirstReportUpgradable() @@ -76,7 +81,6 @@ namespace NzbDrone.Core.Test.DecisionEngineTests Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(2)).Returns(_upgradableQuality); } - [Test] public void should_be_upgradable_if_only_episode_is_upgradable() { @@ -129,5 +133,14 @@ namespace NzbDrone.Core.Test.DecisionEngineTests { _upgradeHistory.IsSatisfiedBy(_parseResultMulti, new SeasonSearchCriteria()).Should().BeTrue(); } + + [Test] + public void should_return_true_if_using_sabnzbd() + { + Mocker.GetMock() + .Setup(c => c.GetDownloadClient()).Returns(Mocker.Resolve()); + + _upgradeHistory.IsSatisfiedBy(_parseResultMulti, new SeasonSearchCriteria()).Should().BeTrue(); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Blacklisting/Blacklist.cs b/src/NzbDrone.Core/Blacklisting/Blacklist.cs new file mode 100644 index 000000000..444818d8e --- /dev/null +++ b/src/NzbDrone.Core/Blacklisting/Blacklist.cs @@ -0,0 +1,15 @@ +using System; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Blacklisting +{ + public class Blacklist : ModelBase + { + public int EpisodeId { get; set; } + public int SeriesId { get; set; } + public string SourceTitle { get; set; } + public QualityModel Quality { get; set; } + public DateTime Date { get; set; } + } +} diff --git a/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs b/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs new file mode 100644 index 000000000..90d026325 --- /dev/null +++ b/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs @@ -0,0 +1,23 @@ +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Messaging.Events; + +namespace NzbDrone.Core.Blacklisting +{ + public interface IBlacklistRepository : IBasicRepository + { + bool Blacklisted(string sourceTitle); + } + + public class BlacklistRepository : BasicRepository, IBlacklistRepository + { + public BlacklistRepository(IDatabase database, IEventAggregator eventAggregator) : + base(database, eventAggregator) + { + } + + public bool Blacklisted(string sourceTitle) + { + return Query.Any(e => e.SourceTitle == sourceTitle); + } + } +} diff --git a/src/NzbDrone.Core/Blacklisting/BlacklistService.cs b/src/NzbDrone.Core/Blacklisting/BlacklistService.cs new file mode 100644 index 000000000..756c6673c --- /dev/null +++ b/src/NzbDrone.Core/Blacklisting/BlacklistService.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Core.Download; +using NzbDrone.Core.Messaging.Events; + +namespace NzbDrone.Core.Blacklisting +{ + public interface IBlacklistService + { + bool Blacklisted(string sourceTitle); + } + + public class BlacklistService : IBlacklistService, IHandle + { + private readonly IBlacklistRepository _blacklistRepository; + + public BlacklistService(IBlacklistRepository blacklistRepository) + { + _blacklistRepository = blacklistRepository; + } + + public bool Blacklisted(string sourceTitle) + { + return _blacklistRepository.Blacklisted(sourceTitle); + } + + public void Handle(DownloadFailedEvent message) + { + var blacklist = new Blacklist + { + SeriesId = message.Series.Id, + EpisodeId = message.Episode.Id, + SourceTitle = message.SourceTitle, + Quality = message.Quality, + Date = DateTime.UtcNow + }; + + _blacklistRepository.Insert(blacklist); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/028_add_blacklist_table.cs b/src/NzbDrone.Core/Datastore/Migration/028_add_blacklist_table.cs new file mode 100644 index 000000000..94a3dda93 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/028_add_blacklist_table.cs @@ -0,0 +1,19 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(28)] + public class add_blacklist_table : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Create.TableForModel("Blacklist") + .WithColumn("SeriesId").AsInt32() + .WithColumn("EpisodeId").AsInt32() + .WithColumn("SourceTitle").AsString() + .WithColumn("Quality").AsString() + .WithColumn("Date").AsDateTime(); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index ec341d6dc..4d4c9bc38 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -4,6 +4,7 @@ using System.Linq; using Marr.Data; using Marr.Data.Mapping; using NzbDrone.Common.Reflection; +using NzbDrone.Core.Blacklisting; using NzbDrone.Core.Configuration; using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.Datastore.Converters; @@ -67,6 +68,8 @@ namespace NzbDrone.Core.Datastore Mapper.Entity().RegisterModel("NamingConfig"); Mapper.Entity().MapResultSet(); + + Mapper.Entity().RegisterModel("Blacklist"); } private static void RegisterMappers() diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/BlacklistSpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/BlacklistSpecification.cs new file mode 100644 index 000000000..cea341a2e --- /dev/null +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/BlacklistSpecification.cs @@ -0,0 +1,39 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.Blacklisting; +using NzbDrone.Core.IndexerSearch.Definitions; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.DecisionEngine.Specifications +{ + public class BlacklistSpecification : IDecisionEngineSpecification + { + private readonly IBlacklistService _blacklistService; + private readonly Logger _logger; + + public BlacklistSpecification(IBlacklistService blacklistService, Logger logger) + { + _blacklistService = blacklistService; + _logger = logger; + } + + public string RejectionReason + { + get + { + return "Release is blacklisted"; + } + } + + public virtual bool IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria) + { + if (_blacklistService.Blacklisted(subject.Release.Title)) + { + _logger.Trace("Release is blacklisted"); + return false; + } + + return true; + } + } +} diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/UpgradeHistorySpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/UpgradeHistorySpecification.cs index e844cb91d..28cd5bbb2 100644 --- a/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/UpgradeHistorySpecification.cs +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/RssSync/UpgradeHistorySpecification.cs @@ -1,4 +1,6 @@ using NLog; +using NzbDrone.Core.Download; +using NzbDrone.Core.Download.Clients.Sabnzbd; using NzbDrone.Core.History; using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.Parser.Model; @@ -9,12 +11,17 @@ namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync { private readonly IHistoryService _historyService; private readonly QualityUpgradableSpecification _qualityUpgradableSpecification; + private readonly IProvideDownloadClient _downloadClientProvider; private readonly Logger _logger; - public UpgradeHistorySpecification(IHistoryService historyService, QualityUpgradableSpecification qualityUpgradableSpecification, Logger logger) + public UpgradeHistorySpecification(IHistoryService historyService, + QualityUpgradableSpecification qualityUpgradableSpecification, + IProvideDownloadClient downloadClientProvider, + Logger logger) { _historyService = historyService; _qualityUpgradableSpecification = qualityUpgradableSpecification; + _downloadClientProvider = downloadClientProvider; _logger = logger; } @@ -34,6 +41,12 @@ namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync return true; } + if (_downloadClientProvider.GetDownloadClient().GetType() == typeof (SabnzbdClient)) + { + _logger.Trace("Skipping history check in favour of blacklist"); + return true; + } + foreach (var episode in subject.Episodes) { var bestQualityInHistory = _historyService.GetBestQualityInHistory(episode.Id); diff --git a/src/NzbDrone.Core/History/History.cs b/src/NzbDrone.Core/History/History.cs index 6be5f97a9..94b345e59 100644 --- a/src/NzbDrone.Core/History/History.cs +++ b/src/NzbDrone.Core/History/History.cs @@ -23,7 +23,6 @@ namespace NzbDrone.Core.History public Dictionary Data { get; set; } } - public enum HistoryEventType { Unknown = 0, @@ -32,5 +31,4 @@ namespace NzbDrone.Core.History DownloadFolderImported = 3, DownloadFailed = 4 } - } \ No newline at end of file diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index c73a36f09..1c60b1d90 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -119,6 +119,9 @@ Properties\SharedAssemblyInfo.cs + + + @@ -181,6 +184,7 @@ Code + @@ -200,6 +204,7 @@ +