From c58d607349f51309bd80d744bf006f6942df377e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 9 Apr 2015 16:48:13 -0700 Subject: [PATCH] Don't throw error when episode title matching doesn't find a match --- .../EpisodeServiceTests/FindEpisodeByTitleFixture.cs | 10 ++++++++++ src/NzbDrone.Core/Tv/EpisodeService.cs | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs index a3eb4cb8d..579076cea 100644 --- a/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs +++ b/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs @@ -59,5 +59,15 @@ public void should_prefer_longer_match() .Should() .Be(expectedTitle); } + + [Test] + public void should_return_null_when_no_match_is_found() + { + GivenEpisodesWithTitles(); + + Subject.FindEpisodeByTitle(1, 1, "The.Walking.Dead.S04.Special.Inside.The.Walking.Dead.Walker.University.720p.HDTV.x264-W4F") + .Should() + .BeNull(); + } } } diff --git a/src/NzbDrone.Core/Tv/EpisodeService.cs b/src/NzbDrone.Core/Tv/EpisodeService.cs index 7c6932aa2..edda6ce28 100644 --- a/src/NzbDrone.Core/Tv/EpisodeService.cs +++ b/src/NzbDrone.Core/Tv/EpisodeService.cs @@ -109,7 +109,7 @@ public Episode FindEpisodeByTitle(int seriesId, int seasonNumber, string release var normalizedReleaseTitle = Parser.Parser.NormalizeEpisodeTitle(releaseTitle).Replace(".", " "); var episodes = _episodeRepository.GetEpisodes(seriesId, seasonNumber); - var query = episodes.Select( + var matches = episodes.Select( episode => new { Position = normalizedReleaseTitle.IndexOf(Parser.Parser.NormalizeEpisodeTitle(episode.Title), StringComparison.CurrentCultureIgnoreCase), @@ -121,7 +121,12 @@ public Episode FindEpisodeByTitle(int seriesId, int seasonNumber, string release .ThenByDescending(e => e.Length) .ToList(); - return query.First().Episode; + if (matches.Any()) + { + return matches.First().Episode; + } + + return null; } public List EpisodesWithFiles(int seriesId)