From 410bf0d846ebca6712e77ef21863389d80c8f947 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 11 Nov 2013 16:49:43 -0800 Subject: [PATCH] Use folder quality when it is better than file quality Fixed: Better handling of downloads that don't have the quality in the filename --- .../DownloadedEpisodesImportServiceFixture.cs | 2 +- .../ImportDecisionMakerFixture.cs | 43 ++++++++++++++++++- .../DownloadedEpisodesImportService.cs | 9 ++-- .../EpisodeImport/ImportDecisionMaker.cs | 14 ++++-- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs index 09e51ac3a..f00c6830a 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs @@ -124,7 +124,7 @@ namespace NzbDrone.Core.Test.MediaFiles imported.Add(new ImportDecision(localEpisode)); Mocker.GetMock() - .Setup(s => s.GetImportDecisions(It.IsAny>(), It.IsAny(), true)) + .Setup(s => s.GetImportDecisions(It.IsAny>(), It.IsAny(), true, null)) .Returns(imported); Mocker.GetMock() diff --git a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/ImportDecisionMakerFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/ImportDecisionMakerFixture.cs index 68dfdc5db..edcfa1a14 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/ImportDecisionMakerFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/ImportDecisionMakerFixture.cs @@ -9,6 +9,7 @@ using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; using NzbDrone.Test.Common; @@ -21,6 +22,7 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport private List _videoFiles; private LocalEpisode _localEpisode; private Series _series; + private QualityModel _quality; private Mock _pass1; private Mock _pass2; @@ -62,7 +64,13 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport _videoFiles = new List { @"C:\Test\Unsorted\The.Office.S03E115.DVDRip.XviD-OSiTV.avi" }; _series = new Series(); - _localEpisode = new LocalEpisode { Series = _series, Path = @"C:\Test\Unsorted\The.Office.S03E115.DVDRip.XviD-OSiTV.avi" }; + _quality = new QualityModel(Quality.DVD); + _localEpisode = new LocalEpisode + { + Series = _series, + Quality = _quality, + Path = @"C:\Test\Unsorted\The.Office.S03E115.DVDRip.XviD-OSiTV.avi" + }; Mocker.GetMock() .Setup(c => c.GetEpisodes(It.IsAny(), It.IsAny(), It.IsAny())) @@ -161,5 +169,38 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport ExceptionVerification.ExpectedErrors(3); } + + [Test] + public void should_use_file_quality_if_folder_quality_is_null() + { + GivenSpecifications(_pass1, _pass2, _pass3); + var expectedQuality = QualityParser.ParseQuality(_videoFiles.Single()); + + var result = Subject.GetImportDecisions(_videoFiles, new Series(), false, null); + + result.Single().LocalEpisode.Quality.Should().Be(expectedQuality); + } + + [Test] + public void should_use_file_quality_if_folder_quality_is_lower_than_file_quality() + { + GivenSpecifications(_pass1, _pass2, _pass3); + var expectedQuality = QualityParser.ParseQuality(_videoFiles.Single()); + + var result = Subject.GetImportDecisions(_videoFiles, new Series(), false, new QualityModel(Quality.SDTV)); + + result.Single().LocalEpisode.Quality.Should().Be(expectedQuality); + } + + [Test] + public void should_use_folder_quality_when_it_is_greater_than_file_quality() + { + GivenSpecifications(_pass1, _pass2, _pass3); + var expectedQuality = new QualityModel(Quality.Bluray1080p); + + var result = Subject.GetImportDecisions(_videoFiles, new Series(), false, expectedQuality); + + result.Single().LocalEpisode.Quality.Should().Be(expectedQuality); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs b/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs index 4cb226005..194aaeef3 100644 --- a/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs @@ -99,6 +99,7 @@ namespace NzbDrone.Core.MediaFiles { var cleanedUpName = GetCleanedUpFolderName(subfolderInfo.Name); var series = _parsingService.GetSeries(cleanedUpName); + var quality = QualityParser.ParseQuality(cleanedUpName); if (series == null) { @@ -108,7 +109,7 @@ namespace NzbDrone.Core.MediaFiles var videoFiles = _diskScanService.GetVideoFiles(subfolderInfo.FullName); - return ProcessFiles(series, videoFiles); + return ProcessFiles(series, quality, videoFiles); } private void ProcessVideoFile(string videoFile) @@ -127,12 +128,12 @@ namespace NzbDrone.Core.MediaFiles return; } - ProcessFiles(series, videoFile); + ProcessFiles(series, null, videoFile); } - private List ProcessFiles(Series series, params string[] videoFiles) + private List ProcessFiles(Series series, QualityModel quality, params string[] videoFiles) { - var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series, true); + var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series, true, quality); return _importApprovedEpisodes.Import(decisions, true); } diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportDecisionMaker.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportDecisionMaker.cs index 1566acbe0..8263533ea 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportDecisionMaker.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportDecisionMaker.cs @@ -6,6 +6,7 @@ using NzbDrone.Common; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Qualities; using NzbDrone.Core.Tv; @@ -13,7 +14,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport { public interface IMakeImportDecision { - List GetImportDecisions(IEnumerable videoFiles, Series series, bool sceneSource); + List GetImportDecisions(IEnumerable videoFiles, Series series, bool sceneSource, QualityModel quality = null); } public class ImportDecisionMaker : IMakeImportDecision @@ -38,16 +39,16 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport _logger = logger; } - public List GetImportDecisions(IEnumerable videoFiles, Series series, bool sceneSource) + public List GetImportDecisions(IEnumerable videoFiles, Series series, bool sceneSource, QualityModel quality = null) { var newFiles = _mediaFileService.FilterExistingFiles(videoFiles.ToList(), series.Id); _logger.Debug("Analysing {0}/{1} files.", newFiles.Count, videoFiles.Count()); - return GetDecisions(newFiles, series, sceneSource).ToList(); + return GetDecisions(newFiles, series, sceneSource, quality).ToList(); } - private IEnumerable GetDecisions(IEnumerable videoFiles, Series series, bool sceneSource) + private IEnumerable GetDecisions(IEnumerable videoFiles, Series series, bool sceneSource, QualityModel quality = null) { foreach (var file in videoFiles) { @@ -59,6 +60,11 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport if (parsedEpisode != null) { + if (quality != null && quality > parsedEpisode.Quality) + { + parsedEpisode.Quality = quality; + } + parsedEpisode.Size = _diskProvider.GetFileSize(file); decision = GetDecision(parsedEpisode); }