diff --git a/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs b/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs index 77f804383..3cf97cce8 100644 --- a/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs +++ b/src/NzbDrone.Core.Test/TvTests/EpisodeServiceTests/FindEpisodeByTitleFixture.cs @@ -93,5 +93,19 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeServiceTests episode.Should().NotBeNull(); episode.Title.Should().Be(expectedTitle); } + + [Test] + public void should_handle_special_with_apostrophe_in_title() + { + var releaseTitle = "The.Profit.S06E00.An.Inside.Look-Sweet.Petes.720p.HDTV."; + var title = "An Inside Look- Sweet Petes"; + + GivenEpisodesWithTitles(title); + + var episode = Subject.FindEpisodeByTitle(1, 0, releaseTitle); + + episode.Should().NotBeNull(); + episode.Title.Should().Be(title); + } } } diff --git a/src/NzbDrone.Core/Tv/EpisodeService.cs b/src/NzbDrone.Core/Tv/EpisodeService.cs index 2e308ff8d..fe1ebc404 100644 --- a/src/NzbDrone.Core/Tv/EpisodeService.cs +++ b/src/NzbDrone.Core/Tv/EpisodeService.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.Datastore; using NzbDrone.Core.MediaFiles; @@ -108,15 +109,27 @@ namespace NzbDrone.Core.Tv { // TODO: can replace this search mechanism with something smarter/faster/better var normalizedReleaseTitle = Parser.Parser.NormalizeEpisodeTitle(releaseTitle); + var cleanNormalizedReleaseTitle = Parser.Parser.CleanSeriesTitle(normalizedReleaseTitle); var episodes = _episodeRepository.GetEpisodes(seriesId, seasonNumber); - var matches = episodes.Select( - episode => new - { - Position = normalizedReleaseTitle.IndexOf(Parser.Parser.NormalizeEpisodeTitle(episode.Title), StringComparison.CurrentCultureIgnoreCase), - Length = Parser.Parser.NormalizeEpisodeTitle(episode.Title).Length, - Episode = episode - }) + var possibleMatches = episodes.SelectMany( + episode => new[] + { + new + { + Position = normalizedReleaseTitle.IndexOf(Parser.Parser.NormalizeEpisodeTitle(episode.Title), StringComparison.CurrentCultureIgnoreCase), + Length = Parser.Parser.NormalizeEpisodeTitle(episode.Title).Length, + Episode = episode + }, + new + { + Position = cleanNormalizedReleaseTitle.IndexOf(Parser.Parser.CleanSeriesTitle(Parser.Parser.NormalizeEpisodeTitle(episode.Title)), StringComparison.CurrentCultureIgnoreCase), + Length = Parser.Parser.NormalizeEpisodeTitle(episode.Title).Length, + Episode = episode + } + }); + + var matches = possibleMatches .Where(e => e.Episode.Title.Length > 0 && e.Position >= 0) .OrderBy(e => e.Position) .ThenByDescending(e => e.Length)