Fixed: Files with lower preferred word scores are imported

Closes #4212
pull/4288/head
Mark McDowall 4 years ago
parent 8047e5aa67
commit 21fafb895f

@ -2,6 +2,7 @@
using FizzWare.NBuilder;
using FluentAssertions;
using Marr.Data;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles;
@ -13,6 +14,7 @@ using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Profiles.Languages;
using NzbDrone.Core.Profiles.Releases;
namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications
{
@ -278,6 +280,39 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse();
}
[Test]
public void should_return_false_if_it_is_not_a_preferred_word_upgrade()
{
Mocker.GetMock<IConfigService>()
.Setup(s => s.DownloadPropersAndRepacks)
.Returns(ProperDownloadTypes.DoNotPrefer);
Mocker.GetMock<IPreferredWordService>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<string>(), 0))
.Returns(5);
Mocker.GetMock<IEpisodeFilePreferredWordCalculator>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<EpisodeFile>()))
.Returns(10);
_localEpisode.Quality = new QualityModel(Quality.Bluray1080p);
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Quality = new QualityModel(Quality.Bluray1080p)
}))
.Build()
.ToList();
_localEpisode.FileEpisodeInfo = Builder<ParsedEpisodeInfo>.CreateNew().Build();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse();
}
[Test]
public void should_return_true_if_not_a_revision_upgrade_and_does_not_prefer_propers()
{
@ -322,6 +357,72 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_return_true_if_it_is_a_preferred_word_upgrade()
{
Mocker.GetMock<IConfigService>()
.Setup(s => s.DownloadPropersAndRepacks)
.Returns(ProperDownloadTypes.DoNotPrefer);
Mocker.GetMock<IPreferredWordService>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<string>(), 0))
.Returns(5);
Mocker.GetMock<IEpisodeFilePreferredWordCalculator>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<EpisodeFile>()))
.Returns(1);
_localEpisode.Quality = new QualityModel(Quality.Bluray1080p);
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Quality = new QualityModel(Quality.Bluray1080p)
}))
.Build()
.ToList();
_localEpisode.FileEpisodeInfo = Builder<ParsedEpisodeInfo>.CreateNew().Build();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_return_true_if_it_has_an_equal_preferred_word_score()
{
Mocker.GetMock<IConfigService>()
.Setup(s => s.DownloadPropersAndRepacks)
.Returns(ProperDownloadTypes.DoNotPrefer);
Mocker.GetMock<IPreferredWordService>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<string>(), 0))
.Returns(5);
Mocker.GetMock<IEpisodeFilePreferredWordCalculator>()
.Setup(s => s.Calculate(It.IsAny<Series>(), It.IsAny<EpisodeFile>()))
.Returns(5);
_localEpisode.Quality = new QualityModel(Quality.Bluray1080p);
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Quality = new QualityModel(Quality.Bluray1080p)
}))
.Build()
.ToList();
_localEpisode.FileEpisodeInfo = Builder<ParsedEpisodeInfo>.CreateNew().Build();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_return_true_if_episode_file_is_null()
{

@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.DecisionEngine;
@ -6,17 +7,25 @@ using NzbDrone.Core.Download;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Profiles.Releases;
namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
{
public class UpgradeSpecification : IImportDecisionEngineSpecification
{
private readonly IConfigService _configService;
private readonly IPreferredWordService _preferredWordService;
private readonly IEpisodeFilePreferredWordCalculator _episodeFilePreferredWordCalculator;
private readonly Logger _logger;
public UpgradeSpecification(IConfigService configService, Logger logger)
public UpgradeSpecification(IConfigService configService,
IPreferredWordService preferredWordService,
IEpisodeFilePreferredWordCalculator episodeFilePreferredWordCalculator,
Logger logger)
{
_configService = configService;
_preferredWordService = preferredWordService;
_episodeFilePreferredWordCalculator = episodeFilePreferredWordCalculator;
_logger = logger;
}
@ -25,6 +34,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
var downloadPropersAndRepacks = _configService.DownloadPropersAndRepacks;
var qualityComparer = new QualityModelComparer(localEpisode.Series.QualityProfile);
var languageComparer = new LanguageComparer(localEpisode.Series.LanguageProfile);
var preferredWordScore = GetPreferredWordScore(localEpisode);
foreach (var episode in localEpisode.Episodes.Where(e => e.EpisodeFileId > 0))
{
@ -45,7 +55,6 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
return Decision.Reject("Not an upgrade for existing episode file(s)");
}
// Same quality, is not a language upgrade, propers/repacks are preferred and it is not a revision update
// This will allow language upgrades of a lower revision to be imported, which are allowed to be grabbed,
// they just don't import automatically.
@ -56,17 +65,48 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
localEpisode.Quality.Revision.CompareTo(episodeFile.Quality.Revision) < 0)
{
_logger.Debug("This file isn't a quality revision upgrade for all episodes. Skipping {0}", localEpisode.Path);
return Decision.Reject("Not an upgrade for existing episode file(s)");
return Decision.Reject("Not a quality revision upgrade for existing episode file(s)");
}
if (languageCompare < 0 && qualityCompare == 0)
{
_logger.Debug("This file isn't a language upgrade for all episodes. Skipping {0}", localEpisode.Path);
return Decision.Reject("Not an upgrade for existing episode file(s)");
return Decision.Reject("Not a language upgrade for existing episode file(s)");
}
var episodeFilePreferredWordScore = _episodeFilePreferredWordCalculator.Calculate(localEpisode.Series, episodeFile);
if (qualityCompare == 0 && preferredWordScore < episodeFilePreferredWordScore)
{
_logger.Debug("This file isn't a preferred word upgrade for all episodes. Skipping {0}", localEpisode.Path);
return Decision.Reject("Not a preferred word upgrade for existing episode file(s)");
}
}
return Decision.Accept();
}
private int GetPreferredWordScore(LocalEpisode localEpisode)
{
var series = localEpisode.Series;
var scores = new List<int>();
if (localEpisode.FileEpisodeInfo != null)
{
scores.Add(_preferredWordService.Calculate(series, localEpisode.FileEpisodeInfo.ReleaseTitle, 0));
}
if (localEpisode.FolderEpisodeInfo != null)
{
scores.Add(_preferredWordService.Calculate(series, localEpisode.FolderEpisodeInfo.ReleaseTitle, 0));
}
if (localEpisode.DownloadClientEpisodeInfo != null)
{
scores.Add(_preferredWordService.Calculate(series, localEpisode.DownloadClientEpisodeInfo.ReleaseTitle, 0));
}
return scores.MaxOrDefault();
}
}
}

Loading…
Cancel
Save