From 5faeccf09801c7df554855fce143be3cc45e1ec5 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 25 Apr 2011 14:37:53 -0700 Subject: [PATCH] added support for daily episode file import --- NzbDrone.Core.Test/MediaFileProviderTests.cs | 40 ++++++++++++++++- NzbDrone.Core/Providers/MediaFileProvider.cs | 45 ++++++++++++-------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/NzbDrone.Core.Test/MediaFileProviderTests.cs b/NzbDrone.Core.Test/MediaFileProviderTests.cs index 42c705ee5..8fbeef10d 100644 --- a/NzbDrone.Core.Test/MediaFileProviderTests.cs +++ b/NzbDrone.Core.Test/MediaFileProviderTests.cs @@ -117,7 +117,7 @@ public void import_new_daily_file() //Assert.AreEqual(fakeEpisode, result.Episodes[0]); Assert.AreEqual(fakeEpisode.SeriesId, result.SeriesId); - Assert.AreEqual(QualityTypes.BDRip, result.Quality); + Assert.AreEqual(QualityTypes.HDTV, result.Quality); Assert.AreEqual(Parser.NormalizePath(fileName), result.Path); Assert.AreEqual(size, result.Size); Assert.AreEqual(false, result.Proper); @@ -125,6 +125,44 @@ public void import_new_daily_file() } + [Test] + [Description("Verifies that a new file imported properly")] + public void import_sample_file() + { + //Arrange + ///////////////////////////////////////// + + //Constants + const string fileName = @"2011.01.10 - Denis Leary - sample - HD TV.mkv"; + DateTime airDate = new DateTime(2011, 01, 10); + const int size = 12345; + //Fakes + var fakeSeries = Builder.CreateNew().Build(); + var fakeEpisode = Builder.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build(); + + //Mocks + var mocker = new AutoMoqer(); + + mocker.GetMock() + .Setup(r => r.Exists(It.IsAny>>())).Returns(false).Verifiable(); + mocker.GetMock() + .Setup(r => r.Add(It.IsAny())).Returns(0).Verifiable(); + + mocker.GetMock() + .Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode). + Verifiable(); + + mocker.GetMock() + .Setup(e => e.GetSize(fileName)).Returns(size).Verifiable(); + + + //Act + var result = mocker.Resolve().ImportFile(fakeSeries, fileName); + + //Assert + Assert.IsNull(result); + } + [Test] [Description("Verifies that an existing file will skip import")] public void import_existing_file() diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index 768cbeac2..fabf65837 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -71,20 +71,25 @@ public EpisodeFile ImportFile(Series series, string filePath) try { + var size = _diskProvider.GetSize(filePath); - + //If Size is less than 50MB and contains sample. Check for Size to ensure its not an episode with sample in the title + if (size < 40000000 && filePath.ToLower().Contains("sample")) + { + Logger.Trace("[{0}] appears to be a sample... skipping.", filePath); + return null; + } if (!_repository.Exists(e => e.Path == Parser.NormalizePath(filePath))) { - var episodesInFile = Parser.ParseEpisodeInfo(filePath); + var parseResult = Parser.ParseEpisodeInfo(filePath); //Stores the list of episodes to add to the EpisodeFile var episodes = new List(); - foreach (var episodeNumber in episodesInFile.Episodes) + if (parseResult.Episodes == null) { - var episode = _episodeProvider.GetEpisode(series.SeriesId, episodesInFile.SeasonNumber, - episodeNumber); + var episode = _episodeProvider.GetEpisode(series.SeriesId, parseResult.AirDate.Date); if (episode != null) { @@ -92,29 +97,35 @@ public EpisodeFile ImportFile(Series series, string filePath) } else - Logger.Warn("Unable to find Series:{0} Season:{1} Episode:{2} in the database. File:{3}", - series.Title, episodesInFile.SeasonNumber, episodeNumber, filePath); + Logger.Warn("Unable to find '{0}' in the database. File:{1}", parseResult, filePath); + } + else + { + foreach (var episodeNumber in parseResult.Episodes) + { + var episode = _episodeProvider.GetEpisode(series.SeriesId, parseResult.SeasonNumber, + episodeNumber); + + if (episode != null) + { + episodes.Add(episode); + } + + else + Logger.Warn("Unable to find '{0}' in the database. File:{1}", parseResult, filePath); + } } //Return null if no Episodes exist in the DB for the parsed episodes from file if (episodes.Count < 1) return null; - var size = _diskProvider.GetSize(filePath); - - //If Size is less than 50MB and contains sample. Check for Size to ensure its not an episode with sample in the title - if (size < 50000000 && filePath.ToLower().Contains("sample")) - { - Logger.Trace("[{0}] appears to be a sample... skipping.", filePath); - return null; - } - var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.Now; episodeFile.SeriesId = series.SeriesId; episodeFile.Path = Parser.NormalizePath(filePath); episodeFile.Size = size; - episodeFile.Quality = episodesInFile.Quality; + episodeFile.Quality = parseResult.Quality; episodeFile.Proper = Parser.ParseProper(filePath); var fileId = (int)_repository.Add(episodeFile);