New: Don't reject imports when quality doesn't match release quality New: Reject grab when release was grabbed and imported already Closes #2783pull/2786/head
parent
b73b99df8d
commit
44048207f2
@ -0,0 +1,163 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using FizzWare.NBuilder;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.History;
|
||||||
|
using NzbDrone.Core.Parser.Model;
|
||||||
|
using NzbDrone.Core.Qualities;
|
||||||
|
using NzbDrone.Core.Tv;
|
||||||
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
||||||
|
using NzbDrone.Core.Indexers;
|
||||||
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.DecisionEngineTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class AlreadyImportedSpecificationFixture : CoreTest<AlreadyImportedSpecification>
|
||||||
|
{
|
||||||
|
private const int FIRST_EPISODE_ID = 1;
|
||||||
|
private const string TITLE = "Series.Title.S01E01.720p.HDTV.x264-Sonarr";
|
||||||
|
|
||||||
|
private Series _series;
|
||||||
|
private QualityModel _hdtv720p;
|
||||||
|
private QualityModel _hdtv1080p;
|
||||||
|
private RemoteEpisode _remoteEpisode;
|
||||||
|
private List<History.History> _history;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
var singleEpisodeList = new List<Episode>
|
||||||
|
{
|
||||||
|
new Episode
|
||||||
|
{
|
||||||
|
Id = FIRST_EPISODE_ID,
|
||||||
|
SeasonNumber = 12,
|
||||||
|
EpisodeNumber = 3,
|
||||||
|
EpisodeFileId = 1
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_series = Builder<Series>.CreateNew()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
_hdtv720p = new QualityModel(Quality.HDTV720p, new Revision(version: 1));
|
||||||
|
_hdtv1080p = new QualityModel(Quality.HDTV1080p, new Revision(version: 1));
|
||||||
|
|
||||||
|
_remoteEpisode = new RemoteEpisode
|
||||||
|
{
|
||||||
|
Series = _series,
|
||||||
|
ParsedEpisodeInfo = new ParsedEpisodeInfo { Quality = _hdtv720p },
|
||||||
|
Episodes = singleEpisodeList,
|
||||||
|
Release = Builder<ReleaseInfo>.CreateNew()
|
||||||
|
.Build()
|
||||||
|
};
|
||||||
|
|
||||||
|
_history = new List<History.History>();
|
||||||
|
|
||||||
|
Mocker.GetMock<IConfigService>()
|
||||||
|
.SetupGet(s => s.EnableCompletedDownloadHandling)
|
||||||
|
.Returns(true);
|
||||||
|
|
||||||
|
Mocker.GetMock<IHistoryService>()
|
||||||
|
.Setup(s => s.FindByEpisodeId(It.IsAny<int>()))
|
||||||
|
.Returns(_history);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenCdhDisabled()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>()
|
||||||
|
.SetupGet(s => s.EnableCompletedDownloadHandling)
|
||||||
|
.Returns(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenHistoryItem(string downloadId, string sourceTitle, QualityModel quality, HistoryEventType eventType)
|
||||||
|
{
|
||||||
|
_history.Add(new History.History
|
||||||
|
{
|
||||||
|
DownloadId = downloadId,
|
||||||
|
SourceTitle = sourceTitle,
|
||||||
|
Quality = quality,
|
||||||
|
Date = DateTime.UtcNow,
|
||||||
|
EventType = eventType
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_accepted_if_CDH_is_disabled()
|
||||||
|
{
|
||||||
|
GivenCdhDisabled();
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_accepted_if_episode_does_not_have_a_file()
|
||||||
|
{
|
||||||
|
_remoteEpisode.Episodes.First().EpisodeFileId = 0;
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_accepted_if_episode_does_not_have_grabbed_event()
|
||||||
|
{
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_accepted_if_episode_does_not_have_imported_event()
|
||||||
|
{
|
||||||
|
GivenHistoryItem(Guid.NewGuid().ToString().ToUpper(), TITLE, _hdtv720p, HistoryEventType.Grabbed);
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_accepted_if_grabbed_and_imported_quality_is_the_same()
|
||||||
|
{
|
||||||
|
var downloadId = Guid.NewGuid().ToString().ToUpper();
|
||||||
|
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv720p, HistoryEventType.Grabbed);
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv720p, HistoryEventType.DownloadFolderImported);
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_rejected_if_grabbed_download_id_matches_release_torrent_hash()
|
||||||
|
{
|
||||||
|
var downloadId = Guid.NewGuid().ToString().ToUpper();
|
||||||
|
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv720p, HistoryEventType.Grabbed);
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv1080p, HistoryEventType.DownloadFolderImported);
|
||||||
|
|
||||||
|
_remoteEpisode.Release = Builder<TorrentInfo>.CreateNew()
|
||||||
|
.With(t => t.DownloadProtocol = DownloadProtocol.Torrent)
|
||||||
|
.With(t => t.InfoHash = downloadId)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_be_rejected_if_release_title_matches_grabbed_event_source_title()
|
||||||
|
{
|
||||||
|
var downloadId = Guid.NewGuid().ToString().ToUpper();
|
||||||
|
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv720p, HistoryEventType.Grabbed);
|
||||||
|
GivenHistoryItem(downloadId, TITLE, _hdtv1080p, HistoryEventType.DownloadFolderImported);
|
||||||
|
|
||||||
|
_remoteEpisode.Release = Builder<TorrentInfo>.CreateNew()
|
||||||
|
.With(t => t.DownloadProtocol = DownloadProtocol.Torrent)
|
||||||
|
.With(t => t.InfoHash = downloadId)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.History;
|
||||||
|
using NzbDrone.Core.Indexers;
|
||||||
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||||
|
using NzbDrone.Core.Parser.Model;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||||
|
{
|
||||||
|
public class AlreadyImportedSpecification : IDecisionEngineSpecification
|
||||||
|
{
|
||||||
|
private readonly IHistoryService _historyService;
|
||||||
|
private readonly IConfigService _configService;
|
||||||
|
private readonly Logger _logger;
|
||||||
|
|
||||||
|
public AlreadyImportedSpecification(IHistoryService historyService,
|
||||||
|
IConfigService configService,
|
||||||
|
Logger logger)
|
||||||
|
{
|
||||||
|
_historyService = historyService;
|
||||||
|
_configService = configService;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpecificationPriority Priority => SpecificationPriority.Database;
|
||||||
|
public RejectionType Type => RejectionType.Permanent;
|
||||||
|
|
||||||
|
public Decision IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
|
||||||
|
{
|
||||||
|
var cdhEnabled = _configService.EnableCompletedDownloadHandling;
|
||||||
|
|
||||||
|
if (!cdhEnabled)
|
||||||
|
{
|
||||||
|
_logger.Debug("Skipping already imported check because CDH is disabled");
|
||||||
|
return Decision.Accept();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Debug("Performing alerady imported check on report");
|
||||||
|
foreach (var episode in subject.Episodes)
|
||||||
|
{
|
||||||
|
if (!episode.HasFile)
|
||||||
|
{
|
||||||
|
_logger.Debug("Skipping already imported check for episode without file");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var historyForEpisode = _historyService.FindByEpisodeId(episode.Id);
|
||||||
|
var lastGrabbed = historyForEpisode.FirstOrDefault(h => h.EventType == HistoryEventType.Grabbed);
|
||||||
|
|
||||||
|
if (lastGrabbed == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var imported = historyForEpisode.FirstOrDefault(h =>
|
||||||
|
h.EventType == HistoryEventType.DownloadFolderImported &&
|
||||||
|
h.DownloadId == lastGrabbed.DownloadId);
|
||||||
|
|
||||||
|
if (imported == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is really only a guard against redownloading the same release over
|
||||||
|
// and over when the grabbed and imported qualities do not match, if they do
|
||||||
|
// match skip this check.
|
||||||
|
if (lastGrabbed.Quality.Equals(imported.Quality))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var release = subject.Release;
|
||||||
|
|
||||||
|
if (release.DownloadProtocol == DownloadProtocol.Torrent)
|
||||||
|
{
|
||||||
|
var torrentInfo = release as TorrentInfo;
|
||||||
|
|
||||||
|
if (torrentInfo != null && torrentInfo.InfoHash.ToUpper() == lastGrabbed.DownloadId)
|
||||||
|
{
|
||||||
|
_logger.Debug("Has same torrent hash as a grabbed and imported release");
|
||||||
|
return Decision.Reject("Has same torrent hash as a grabbed and imported release");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only based on title because a release with the same title on another indexer/released at
|
||||||
|
// a different time very likely has the exact same content and we don't need to also try it.
|
||||||
|
|
||||||
|
if (release.Title.Equals(lastGrabbed.SourceTitle, StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
_logger.Debug("Has same release name as a grabbed and imported release");
|
||||||
|
return Decision.Reject("Has same release name as a grabbed and imported release");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Decision.Accept();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue