diff --git a/src/NzbDrone.Core.Test/MediaFiles/EpisodeFilePreferredWordCalculatorFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeFilePreferredWordCalculatorFixture.cs index 6a0e0548e..4c42975a5 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/EpisodeFilePreferredWordCalculatorFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeFilePreferredWordCalculatorFixture.cs @@ -1,3 +1,4 @@ +using System.IO; using FizzWare.NBuilder; using FluentAssertions; using Moq; @@ -76,5 +77,37 @@ namespace NzbDrone.Core.Test.MediaFiles Subject.Calculate(_series, _episodeFile).Should().Be(20); } + + [Test] + public void should_return_score_for_original_path_folder_name_if_highest() + { + var folderName = "folder-name"; + var fileName = "file-name"; + + _episodeFile.OriginalFilePath = Path.Combine(folderName, fileName); + + GivenPreferredWordScore(_episodeFile.RelativePath, 20); + GivenPreferredWordScore(_episodeFile.Path, 50); + GivenPreferredWordScore(folderName, 60); + GivenPreferredWordScore(fileName, 50); + + Subject.Calculate(_series, _episodeFile).Should().Be(60); + } + + [Test] + public void should_return_score_for_original_path_file_name_if_highest() + { + var folderName = "folder-name"; + var fileName = "file-name"; + + _episodeFile.OriginalFilePath = Path.Combine(folderName, fileName); + + GivenPreferredWordScore(_episodeFile.RelativePath, 20); + GivenPreferredWordScore(_episodeFile.Path, 50); + GivenPreferredWordScore(folderName, 40); + GivenPreferredWordScore(fileName, 50); + + Subject.Calculate(_series, _episodeFile).Should().Be(50); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeFilePreferredWordCalculator.cs b/src/NzbDrone.Core/MediaFiles/EpisodeFilePreferredWordCalculator.cs index d4659b91c..afc2febb8 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeFilePreferredWordCalculator.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeFilePreferredWordCalculator.cs @@ -1,4 +1,8 @@ using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Web.UI; +using Newtonsoft.Json.Serialization; using NLog; using NzbDrone.Common.Extensions; using NzbDrone.Core.Profiles.Releases; @@ -21,6 +25,7 @@ namespace NzbDrone.Core.MediaFiles _preferredWordService = preferredWordService; _logger = logger; } + public int Calculate(Series series, EpisodeFile episodeFile) { var scores = new List(); @@ -34,6 +39,24 @@ namespace NzbDrone.Core.MediaFiles _logger.Trace("No stored scene name for {0}", episodeFile); } + // The file may not have a screen name if the file/folder name contained spaces, but the original path is still store and valuable. + if (episodeFile.OriginalFilePath.IsNotNullOrWhiteSpace()) + { + var segments = episodeFile.OriginalFilePath.Split(Path.DirectorySeparatorChar).ToList(); + + for (int i = 0; i < segments.Count; i++) + { + var isLast = i == segments.Count - 1; + var segment = isLast ? Path.GetFileNameWithoutExtension(segments[i]) : segments[i]; + + scores.Add(_preferredWordService.Calculate(series, segment, 0)); + } + } + else + { + _logger.Trace("No stored scene name for {0}", episodeFile); + } + // Calculate using RelativePath or Path, but not both if (episodeFile.RelativePath.IsNotNullOrWhiteSpace()) {