1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

New: Use filename for preferred word score if it's higher than scene name

This commit is contained in:
Mark McDowall 2020-05-09 16:45:35 -07:00
parent 36088ef49d
commit 1199ae4e4f
4 changed files with 136 additions and 8 deletions

View File

@ -0,0 +1,80 @@
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Profiles.Releases;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.MediaFiles
{
[TestFixture]
public class EpisodeFilePreferredWordCalculatorFixture : CoreTest<EpisodeFilePreferredWordCalculator>
{
private Series _series;
private EpisodeFile _episodeFile;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew().Build();
_episodeFile = Builder<EpisodeFile>.CreateNew().Build();
}
private void GivenPreferredWordScore(string title, int score)
{
Mocker.GetMock<IPreferredWordService>()
.Setup(s => s.Calculate(It.IsAny<Series>(), title, 0))
.Returns(score);
}
[Test]
public void should_return_score_for_relative_file_name_when_it_is_higher_than_scene_name()
{
GivenPreferredWordScore(_episodeFile.SceneName, 10);
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
Subject.Calculate(_series, _episodeFile).Should().Be(20);
}
[Test]
public void should_return_score_for_full_file_name_when_relative_file_name_is_not_available()
{
_episodeFile.SceneName = null;
_episodeFile.RelativePath = null;
GivenPreferredWordScore(_episodeFile.Path, 20);
Subject.Calculate(_series, _episodeFile).Should().Be(20);
}
[Test]
public void should_return_score_for_relative_file_name_when_scene_name_is_null()
{
_episodeFile.SceneName = null;
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
Subject.Calculate(_series, _episodeFile).Should().Be(20);
}
[Test]
public void should_return_score_for_scene_name_when_higher_than_relative_file_name()
{
GivenPreferredWordScore(_episodeFile.SceneName, 50);
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
Subject.Calculate(_series, _episodeFile).Should().Be(50);
}
[Test]
public void should_return_score_for_relative_file_if_available()
{
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
GivenPreferredWordScore(_episodeFile.Path, 50);
Subject.Calculate(_series, _episodeFile).Should().Be(20);
}
}
}

View File

@ -1,6 +1,7 @@
using System.Linq;
using NLog;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Profiles.Releases;
@ -9,13 +10,13 @@ namespace NzbDrone.Core.DecisionEngine.Specifications
public class CutoffSpecification : IDecisionEngineSpecification
{
private readonly UpgradableSpecification _upgradableSpecification;
private readonly IPreferredWordService _preferredWordServiceCalculator;
private readonly IEpisodeFilePreferredWordCalculator _episodeFilePreferredWordCalculator;
private readonly Logger _logger;
public CutoffSpecification(UpgradableSpecification upgradableSpecification, IPreferredWordService preferredWordServiceCalculator, Logger logger)
public CutoffSpecification(UpgradableSpecification upgradableSpecification, IEpisodeFilePreferredWordCalculator episodeFilePreferredWordCalculator, Logger logger)
{
_upgradableSpecification = upgradableSpecification;
_preferredWordServiceCalculator = preferredWordServiceCalculator;
_episodeFilePreferredWordCalculator = episodeFilePreferredWordCalculator;
_logger = logger;
}
@ -41,7 +42,7 @@ public virtual Decision IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase
languageProfile,
file.Quality,
file.Language,
_preferredWordServiceCalculator.Calculate(subject.Series, file.GetSceneOrFileName(), subject.Release.IndexerId),
_episodeFilePreferredWordCalculator.Calculate(subject.Series, file),
subject.ParsedEpisodeInfo.Quality,
subject.PreferredWordScore))
{

View File

@ -1,6 +1,7 @@
using System.Linq;
using NLog;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Profiles.Releases;
@ -9,13 +10,13 @@ namespace NzbDrone.Core.DecisionEngine.Specifications
public class UpgradeDiskSpecification : IDecisionEngineSpecification
{
private readonly UpgradableSpecification _upgradableSpecification;
private readonly IPreferredWordService _preferredWordServiceCalculator;
private readonly IEpisodeFilePreferredWordCalculator _episodeFilePreferredWordCalculator;
private readonly Logger _logger;
public UpgradeDiskSpecification(UpgradableSpecification upgradableSpecification, IPreferredWordService preferredWordServiceCalculator, Logger logger)
public UpgradeDiskSpecification(UpgradableSpecification upgradableSpecification, IEpisodeFilePreferredWordCalculator episodeFilePreferredWordCalculator, Logger logger)
{
_upgradableSpecification = upgradableSpecification;
_preferredWordServiceCalculator = preferredWordServiceCalculator;
_episodeFilePreferredWordCalculator = episodeFilePreferredWordCalculator;
_logger = logger;
}
@ -38,7 +39,7 @@ public virtual Decision IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase
subject.Series.LanguageProfile,
file.Quality,
file.Language,
_preferredWordServiceCalculator.Calculate(subject.Series, file.GetSceneOrFileName(), subject.Release?.IndexerId ?? 0),
_episodeFilePreferredWordCalculator.Calculate(subject.Series, file),
subject.ParsedEpisodeInfo.Quality,
subject.ParsedEpisodeInfo.Language,
subject.PreferredWordScore))

View File

@ -0,0 +1,46 @@
using System.Collections.Generic;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Profiles.Releases;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles
{
public interface IEpisodeFilePreferredWordCalculator
{
int Calculate(Series series, EpisodeFile episodeFile);
}
public class EpisodeFilePreferredWordCalculator : IEpisodeFilePreferredWordCalculator
{
private readonly IPreferredWordService _preferredWordService;
public EpisodeFilePreferredWordCalculator(IPreferredWordService preferredWordService)
{
_preferredWordService = preferredWordService;
}
public int Calculate(Series series, EpisodeFile episodeFile)
{
var scores = new List<int>();
if (episodeFile.SceneName.IsNotNullOrWhiteSpace())
{
scores.Add(_preferredWordService.Calculate(series, episodeFile.SceneName, 0));
}
// Calculate using RelativePath or Path, but not both
if (episodeFile.RelativePath.IsNotNullOrWhiteSpace())
{
scores.Add(_preferredWordService.Calculate(series, episodeFile.RelativePath, 0));
}
else if (episodeFile.Path.IsNotNullOrWhiteSpace())
{
scores.Add(_preferredWordService.Calculate(series, episodeFile.Path, 0));
}
// Return the highest score, this will allow media info in file names to be used to improve preferred word scoring.
// TODO: A full map of preferred words should be de-duped and used to create an aggregated score using the scene name and the file name.
return scores.MaxOrDefault();
}
}
}