From 194c66f36a5aab833626b604b7bb972670730df5 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 25 Apr 2011 14:23:02 -0700 Subject: [PATCH] start support for daily show file import --- NzbDrone.Core.Test/MediaFileProviderTests.cs | 53 +++++++++++ NzbDrone.Core.Test/ParserTest.cs | 5 + NzbDrone.Core/Providers/EpisodeProvider.cs | 9 +- NzbDrone.Core/Providers/MediaFileProvider.cs | 99 +++++++++++--------- 4 files changed, 121 insertions(+), 45 deletions(-) diff --git a/NzbDrone.Core.Test/MediaFileProviderTests.cs b/NzbDrone.Core.Test/MediaFileProviderTests.cs index b55c28f00..42c705ee5 100644 --- a/NzbDrone.Core.Test/MediaFileProviderTests.cs +++ b/NzbDrone.Core.Test/MediaFileProviderTests.cs @@ -71,6 +71,59 @@ namespace NzbDrone.Core.Test Assert.AreNotEqual(new DateTime(), result.DateAdded); } + [Test] + [Description("Verifies that a new file imported properly")] + public void import_new_daily_file() + { + //Arrange + ///////////////////////////////////////// + + //Constants + const string fileName = @"2011.01.10 - Denis Leary - 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.IsNotNull(result); + mocker.GetMock().VerifyAll(); + mocker.GetMock().Verify(r => r.Add(result), Times.Once()); + mocker.GetMock().VerifyAll(); + mocker.GetMock().VerifyAll(); + + //Currently can't verify this since the list of episodes are loaded + //Dynamically by SubSonic + //Assert.AreEqual(fakeEpisode, result.Episodes[0]); + + Assert.AreEqual(fakeEpisode.SeriesId, result.SeriesId); + Assert.AreEqual(QualityTypes.BDRip, result.Quality); + Assert.AreEqual(Parser.NormalizePath(fileName), result.Path); + Assert.AreEqual(size, result.Size); + Assert.AreEqual(false, result.Proper); + Assert.AreNotEqual(new DateTime(), result.DateAdded); + } + [Test] [Description("Verifies that an existing file will skip import")] diff --git a/NzbDrone.Core.Test/ParserTest.cs b/NzbDrone.Core.Test/ParserTest.cs index e93540ad3..7ac5bbb47 100644 --- a/NzbDrone.Core.Test/ParserTest.cs +++ b/NzbDrone.Core.Test/ParserTest.cs @@ -50,6 +50,7 @@ namespace NzbDrone.Core.Test [Row(@"z:\tv shows\modern marvels\Season 16\S16E03 - The Potato.mkv", 16, 3)] [Row(@"z:\tv shows\robot chicken\Specials\S00E16 - Dear Consumer - SD TV.avi", 0, 16)] [Row(@"D:\shares\TV Shows\Parks And Recreation\Season 2\S02E21 - 94 Meetings - 720p TV.mkv", 2, 21)] + [Row(@"D:\shares\TV Shows\Battlestar Galactica (2003)\Season 2\S02E21.avi", 2, 21)] public void file_path_parse(string path, int season, int episode) { var result = Parser.ParseEpisodeInfo(path); @@ -111,12 +112,16 @@ namespace NzbDrone.Core.Test [Row("The Tonight Show With Jay Leno 2011 04 15 1080i HDTV DD5 1 MPEG2 TrollHD", "The Tonight Show With Jay Leno", 2011, 04, 15)] [Row("The.Daily.Show.2010.10.11.Johnny.Knoxville.iTouch-MW", "The.Daily.Show", 2010, 10, 11)] [Row("The Daily Show - 2011-04-12 - Gov. Deval Patrick", "The.Daily.Show", 2011, 04, 12)] + [Row("2011.01.10 - Denis Leary - HD TV.mkv", 2011, 1, 10)] + [Row("2011.03.13 - Denis Leary - HD TV.mkv", 2011, 3, 13)] + [Row("2011-03-13 - Denis Leary - HD TV.mkv", 2011, 3, 13)] public void episode_daily_parse(string postTitle, string title, int year, int month, int day) { var result = Parser.ParseEpisodeInfo(postTitle); var airDate = new DateTime(year, month, day); Assert.AreEqual(Parser.NormalizeTitle(title), result.CleanTitle); Assert.AreEqual(airDate, result.AirDate); + Assert.IsEmpty(result.Episodes); } [Test] diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index 2dc7982f5..6870c35b3 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -45,6 +45,13 @@ namespace NzbDrone.Core.Providers c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber); } + public virtual Episode GetEpisode(int seriesId, DateTime date) + { + return + _sonicRepo.Single( + c => c.SeriesId == seriesId && c.AirDate == date.Date); + } + public virtual IList GetEpisodeBySeries(long seriesId) { return _sonicRepo.Find(e => e.SeriesId == seriesId); @@ -168,7 +175,7 @@ namespace NzbDrone.Core.Providers Logger.Trace("Updating info for [{0}] - S{1}E{2}", targetSeries.SeriesName, episode.SeasonNumber, episode.EpisodeNumber); var newEpisode = new Episode { - AirDate = episode.FirstAired, + AirDate = episode.FirstAired.Date, TvDbEpisodeId = episode.Id, EpisodeNumber = episode.EpisodeNumber, Language = episode.Language.Abbriviation, diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index b265e35ca..768cbeac2 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -69,65 +69,76 @@ namespace NzbDrone.Core.Providers { Logger.Trace("Importing file to database [{0}]", filePath); - if (!_repository.Exists(e => e.Path == Parser.NormalizePath(filePath))) + try { - var episodesInFile = Parser.ParseEpisodeInfo(filePath); - //Stores the list of episodes to add to the EpisodeFile - var episodes = new List(); - foreach (var episodeNumber in episodesInFile.Episodes) + + if (!_repository.Exists(e => e.Path == Parser.NormalizePath(filePath))) { - var episode = _episodeProvider.GetEpisode(series.SeriesId, episodesInFile.SeasonNumber, - episodeNumber); + var episodesInFile = Parser.ParseEpisodeInfo(filePath); + + //Stores the list of episodes to add to the EpisodeFile + var episodes = new List(); - if (episode != null) + foreach (var episodeNumber in episodesInFile.Episodes) { - episodes.Add(episode); + var episode = _episodeProvider.GetEpisode(series.SeriesId, episodesInFile.SeasonNumber, + episodeNumber); + + if (episode != null) + { + episodes.Add(episode); + } + + else + Logger.Warn("Unable to find Series:{0} Season:{1} Episode:{2} in the database. File:{3}", + series.Title, episodesInFile.SeasonNumber, episodeNumber, filePath); } - else - Logger.Warn("Unable to find Series:{0} Season:{1} Episode:{2} in the database. File:{3}", - series.Title, episodesInFile.SeasonNumber, episodeNumber, filePath); - } + //Return null if no Episodes exist in the DB for the parsed episodes from file + if (episodes.Count < 1) + return null; - //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); - 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; + } - //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.Proper = Parser.ParseProper(filePath); + var fileId = (int)_repository.Add(episodeFile); + + //This is for logging + updating the episodes that are linked to this EpisodeFile + string episodeList = String.Empty; + foreach (var ep in episodes) + { + ep.EpisodeFileId = fileId; + _episodeProvider.UpdateEpisode(ep); + episodeList += String.Format(", {0}", ep.EpisodeId).Trim(' ', ','); + } + Logger.Trace("File {0}:{1} attached to episode(s): '{2}'", episodeFile.EpisodeFileId, filePath, + episodeList); - 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.Proper = Parser.ParseProper(filePath); - var fileId = (int)_repository.Add(episodeFile); - - //This is for logging + updating the episodes that are linked to this EpisodeFile - string episodeList = String.Empty; - foreach (var ep in episodes) - { - ep.EpisodeFileId = fileId; - _episodeProvider.UpdateEpisode(ep); - episodeList += String.Format(", {0}", ep.EpisodeId).Trim(' ', ','); + return episodeFile; } - Logger.Trace("File {0}:{1} attached to episode(s): '{2}'", episodeFile.EpisodeFileId, filePath, - episodeList); - return episodeFile; + Logger.Trace("[{0}] already exists in the database. skipping.", filePath); + } + catch (Exception ex) + { + Logger.ErrorException("An error has occurred while importing file " + filePath, ex); + throw; } - - Logger.Trace("[{0}] already exists in the database. skipping.", filePath); return null; }