New: Setting to prevent download of early releases (#485)
* New: Setting to prevent download of early releases * Fixup! Test and Wordingpull/6/head
parent
9b0a7c60ed
commit
e08f39ebe0
@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.DecisionEngine.Specifications;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.Indexers.TorrentRss;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Music;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.DecisionEngineTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class EarlyReleaseSpecificationFixture : TestBase<EarlyReleaseSpecification>
|
||||
{
|
||||
private Artist _artist;
|
||||
private Album _album1;
|
||||
private Album _album2;
|
||||
private RemoteAlbum _remoteAlbum;
|
||||
private IndexerDefinition _indexerDefinition;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_artist = Builder<Artist>.CreateNew().With(s => s.Id = 1).Build();
|
||||
_album1 = Builder<Album>.CreateNew().With(s => s.ReleaseDate = DateTime.Today).Build();
|
||||
_album2 = Builder<Album>.CreateNew().With(s => s.ReleaseDate = DateTime.Today).Build();
|
||||
|
||||
_remoteAlbum = new RemoteAlbum
|
||||
{
|
||||
Artist = _artist,
|
||||
Albums = new List<Album>{_album1},
|
||||
Release = new TorrentInfo
|
||||
{
|
||||
IndexerId = 1,
|
||||
Title = "Artist - Album [FLAC-RlsGrp]",
|
||||
PublishDate = DateTime.Today
|
||||
}
|
||||
};
|
||||
|
||||
_indexerDefinition = new IndexerDefinition
|
||||
{
|
||||
Settings = new TorrentRssIndexerSettings { EarlyReleaseLimit = 5 }
|
||||
};
|
||||
|
||||
Mocker.GetMock<IIndexerFactory>()
|
||||
.Setup(v => v.Get(1))
|
||||
.Returns(_indexerDefinition);
|
||||
|
||||
}
|
||||
|
||||
private void GivenPublishDateFromToday(int days)
|
||||
{
|
||||
(_remoteAlbum.Release).PublishDate = DateTime.Today.AddDays(days);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_true_if_indexer_not_specified()
|
||||
{
|
||||
_remoteAlbum.Release.IndexerId = 0;
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_true_if_release_contains_multiple_albums()
|
||||
{
|
||||
_remoteAlbum.Albums.Add(_album2);
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_true_if_indexer_no_longer_exists()
|
||||
{
|
||||
Mocker.GetMock<IIndexerFactory>()
|
||||
.Setup(v => v.Get(It.IsAny<int>()))
|
||||
.Callback<int>(i => { throw new ModelNotFoundException(typeof(IndexerDefinition), i); });
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestCase(-2)]
|
||||
[TestCase(-5)]
|
||||
public void should_return_true_if_publish_date_above_or_equal_to_limit(int days)
|
||||
{
|
||||
GivenPublishDateFromToday(days);
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestCase(-10)]
|
||||
[TestCase(-20)]
|
||||
public void should_return_false_if_publish_date_belove_limit(int days)
|
||||
{
|
||||
GivenPublishDateFromToday(days);
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase(-10)]
|
||||
[TestCase(-100)]
|
||||
public void should_return_true_if_limit_null(int days)
|
||||
{
|
||||
GivenPublishDateFromToday(days);
|
||||
|
||||
_indexerDefinition.Settings = new TorrentRssIndexerSettings{EarlyReleaseLimit = null};
|
||||
|
||||
Subject.IsSatisfiedBy(_remoteAlbum, null).Accepted.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class EarlyReleaseSpecification : IDecisionEngineSpecification
|
||||
{
|
||||
private readonly IIndexerFactory _indexerFactory;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public EarlyReleaseSpecification(IIndexerFactory indexerFactory, Logger logger)
|
||||
{
|
||||
_indexerFactory = indexerFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public SpecificationPriority Priority => SpecificationPriority.Default;
|
||||
public RejectionType Type => RejectionType.Permanent;
|
||||
|
||||
public Decision IsSatisfiedBy(RemoteAlbum subject, SearchCriteriaBase searchCriteria)
|
||||
{
|
||||
var releaseInfo = subject.Release;
|
||||
|
||||
if (releaseInfo == null || releaseInfo.IndexerId == 0)
|
||||
{
|
||||
return Decision.Accept();
|
||||
}
|
||||
|
||||
IndexerDefinition indexer;
|
||||
try
|
||||
{
|
||||
indexer = _indexerFactory.Get(subject.Release.IndexerId);
|
||||
}
|
||||
catch (ModelNotFoundException)
|
||||
{
|
||||
_logger.Debug("Indexer with id {0} does not exist, skipping early release check", subject.Release.IndexerId);
|
||||
return Decision.Accept();
|
||||
}
|
||||
|
||||
var indexerSettings = indexer.Settings as IIndexerSettings;
|
||||
|
||||
if (subject.Albums.Count != 1 || indexerSettings?.EarlyReleaseLimit == null)
|
||||
{
|
||||
return Decision.Accept();
|
||||
}
|
||||
|
||||
var releaseDate = subject.Albums.First().ReleaseDate;
|
||||
|
||||
if (releaseDate == null) return Decision.Accept();
|
||||
|
||||
var isEarly = (releaseDate.Value > subject.Release.PublishDate.AddDays(indexerSettings.EarlyReleaseLimit.Value));
|
||||
|
||||
if (isEarly)
|
||||
{
|
||||
var message = $"Release published date, {subject.Release.PublishDate.ToShortDateString()}, is outside of {indexerSettings.EarlyReleaseLimit.Value} day early grab limit allowed by user";
|
||||
|
||||
_logger.Debug(message);
|
||||
return Decision.Reject(message);
|
||||
}
|
||||
|
||||
return Decision.Accept();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue