From 218059e08d36cc4f9e2d6beca743316353d4fdaf Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 15 Jan 2012 20:12:47 -0800 Subject: [PATCH] Added the ability to auto-ignore episodes for files that are deleted, good for people that delete after watching. Option is not exposed in the UI and is disabled by default (obviously). --- .../ProviderTests/EpisodeProviderTest.cs | 34 +++++++++++++ NzbDrone.Core/Jobs/AutoIgnoreJob.cs | 51 +++++++++++++++++++ NzbDrone.Core/Jobs/DiskScanJob.cs | 9 +++- NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Providers/Core/ConfigProvider.cs | 6 +++ NzbDrone.Core/Providers/EpisodeProvider.cs | 8 +++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 NzbDrone.Core/Jobs/AutoIgnoreJob.cs diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs index a476f8bfc..08d57a801 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTest.cs @@ -1590,5 +1590,39 @@ public void RefreshEpisodeInfo_should_ignore_episode_zero_except_if_season_one() result.Where(e => e.Ignored).Should().HaveCount(episodeCount - 1); result.Single(e => e.SeasonNumber == 1).Ignored.Should().BeFalse(); } + + [Test] + public void SetPreviouslyDownloadedToIgnored_should_set_only_episodes_with_no_episode_file_and_postdownload_status_noError_to_ignored() + { + WithRealDb(); + + var postDownloadStatus = PostDownloadStatusType.NoError; + + var fakeEpisodes = Builder.CreateListOfSize(10) + .All() + .With(c => c.Ignored = false) + .TheFirst(2) + .With(c => c.PostDownloadStatus = PostDownloadStatusType.NoError) + .With(c => c.EpisodeFileId = 0) + .TheNext(3) + .With(c => c.PostDownloadStatus = PostDownloadStatusType.Unknown) + .With(c => c.EpisodeFileId = 0) + .TheNext(4) + .With(c => c.PostDownloadStatus = PostDownloadStatusType.NoError) + .TheNext(1) + .With(c => c.PostDownloadStatus = PostDownloadStatusType.NoError) + .With(c => c.Ignored = true) + .Build(); + + Db.InsertMany(fakeEpisodes); + + //Act + Mocker.Resolve().SetPreviouslyDownloadedToIgnored(); + + //Assert + var result = Db.Fetch(); + result.Should().HaveCount(10); + result.Where(e => e.Ignored).Count().Should().Be(3); + } } } diff --git a/NzbDrone.Core/Jobs/AutoIgnoreJob.cs b/NzbDrone.Core/Jobs/AutoIgnoreJob.cs new file mode 100644 index 000000000..4853b2a84 --- /dev/null +++ b/NzbDrone.Core/Jobs/AutoIgnoreJob.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Ninject; +using NLog; +using NzbDrone.Core.Helpers; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Repository; + +namespace NzbDrone.Core.Jobs +{ + public class AutoIgnoreJob : IJob + { + private readonly ConfigProvider _configProvider; + private readonly EpisodeProvider _episodeProvider; + + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + [Inject] + public AutoIgnoreJob(ConfigProvider configProvider, EpisodeProvider episodeProvider) + { + _configProvider = configProvider; + _episodeProvider = episodeProvider; + } + + public AutoIgnoreJob() + { + } + + public string Name + { + get { return "Auto Ignore Episodes"; } + } + + public TimeSpan DefaultInterval + { + get { return TimeSpan.FromTicks(0); } + } + + public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId) + { + if (_configProvider.AutoIgnorePreviouslyDownloadedEpisodes) + { + Logger.Info("Ignoring Previously Downloaded Episodes"); + _episodeProvider.SetPreviouslyDownloadedToIgnored(); + } + } + } +} diff --git a/NzbDrone.Core/Jobs/DiskScanJob.cs b/NzbDrone.Core/Jobs/DiskScanJob.cs index b9f5719a4..e09b1896b 100644 --- a/NzbDrone.Core/Jobs/DiskScanJob.cs +++ b/NzbDrone.Core/Jobs/DiskScanJob.cs @@ -6,6 +6,7 @@ using NzbDrone.Core.Helpers; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; namespace NzbDrone.Core.Jobs @@ -14,13 +15,16 @@ public class DiskScanJob : IJob { private readonly SeriesProvider _seriesProvider; private readonly DiskScanProvider _diskScanProvider; + private readonly AutoIgnoreJob _autoIgnoreJob; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); [Inject] - public DiskScanJob(SeriesProvider seriesProvider, DiskScanProvider diskScanProvider) + public DiskScanJob(SeriesProvider seriesProvider, DiskScanProvider diskScanProvider, + AutoIgnoreJob autoIgnoreJob) { _seriesProvider = seriesProvider; _diskScanProvider = diskScanProvider; + _autoIgnoreJob = autoIgnoreJob; } public DiskScanJob() @@ -62,6 +66,9 @@ public virtual void Start(ProgressNotification notification, int targetId, int s Logger.ErrorException("An error has occurred while scanning " + series.Title, e); } } + + //Start the Auto Ignore Job + _autoIgnoreJob.Start(notification, 0 , 0); } } } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index d3b987d6f..6b1418a14 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -225,6 +225,7 @@ + diff --git a/NzbDrone.Core/Providers/Core/ConfigProvider.cs b/NzbDrone.Core/Providers/Core/ConfigProvider.cs index dc74e7c7f..d72a47f6e 100644 --- a/NzbDrone.Core/Providers/Core/ConfigProvider.cs +++ b/NzbDrone.Core/Providers/Core/ConfigProvider.cs @@ -402,6 +402,12 @@ public virtual bool EnableBacklogSearching set { SetValue("EnableBacklogSearching", value); } } + public virtual bool AutoIgnorePreviouslyDownloadedEpisodes + { + get { return GetValueBoolean("AutoIgnorePreviouslyDownloadedEpisodes"); } + set { SetValue("AutoIgnorePreviouslyDownloadedEpisodes", value); } + } + private string GetValue(string key) { return GetValue(key, String.Empty); diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index fcaca6cfe..f347fb3d3 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -463,5 +463,13 @@ public virtual void SetPostDownloadStatus(List episodeIds, PostDownloadStat Logger.Trace("Updating PostDownloadStatus for all episodeIds in {0}", episodeIdString); _database.Execute(episodeIdQuery); } + + /// + /// Sets Ignored to true if the episode successfully downloaded (PostDownloadStatus = 5), but EpisodeFileId = 0 + /// + public virtual void SetPreviouslyDownloadedToIgnored() + { + _database.Execute("UPDATE Episodes SET Ignored = 1, PostDownloadStatus = 0 WHERE PostDownloadStatus = 5 AND EpisodeFileId = 0"); + } } } \ No newline at end of file