From 66d3fd17e9df8e0e94e097bcfdc4110e26b551d6 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 21 Dec 2017 23:16:36 -0500 Subject: [PATCH] Fixed: Search Criteria parsing returns bad results (Abba/Black Sabbath) --- .../ParserTests/ParserFixture.cs | 41 +++++++++++++++ src/NzbDrone.Core/Parser/Parser.cs | 50 ++++++++++++------- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/NzbDrone.Core.Test/ParserTests/ParserFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParserFixture.cs index 13e15ee8a..c5650869b 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParserFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParserFixture.cs @@ -1,5 +1,9 @@ +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Core.Music; using NzbDrone.Core.Parser; using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Framework; @@ -10,6 +14,23 @@ namespace NzbDrone.Core.Test.ParserTests [TestFixture] public class ParserFixture : CoreTest { + Artist _artist = new Artist(); + private List _albums = new List{new Album()}; + + [SetUp] + public void Setup() + { + _artist = Builder + .CreateNew() + .Build(); + } + + private void GivenSearchCriteria(string artistName, string albumTitle) + { + _artist.Name = artistName; + _albums.First().Title = albumTitle; + } + [TestCase("Bad Format", "badformat")] public void should_parse_artist_name(string postTitle, string title) { @@ -102,5 +123,25 @@ namespace NzbDrone.Core.Test.ParserTests parseResult.AlbumTitle.Should().Be(title); parseResult.Discography.Should().Be(discography); } + + [TestCase("Black Sabbath - Black Sabbath FLAC")] + [TestCase("Black Sabbath Black Sabbath FLAC")] + [TestCase("BlaCk SabBaTh Black SabBatH FLAC")] + [TestCase("Black Sabbath FLAC Black Sabbath")] + public void should_parse_artist_name_and_album_title_by_search_criteria(string releaseTitle) + { + GivenSearchCriteria("Black Sabbath", "Black Sabbath"); + var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria(releaseTitle, _artist, _albums); + parseResult.ArtistName.ToLowerInvariant().Should().Be("black sabbath"); + parseResult.AlbumTitle.ToLowerInvariant().Should().Be("black sabbath"); + } + + [Test] + public void should_not_parse_artist_name_and_album_title_by_incorrect_search_criteria() + { + GivenSearchCriteria("Abba", "Abba"); + var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria("Black Sabbath Black Sabbath FLAC", _artist, _albums); + parseResult.Should().BeNull(); + } } } diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index 924c1f231..291cdb706 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -346,32 +346,48 @@ namespace NzbDrone.Core.Parser simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty); - var result = new ParsedAlbumInfo(); + var releaseRegex = new Regex(@"\b(?" + artist.Name + @")\b.*\b(?"+ string.Join("|",album.Select(s=>s.Title).ToList()) + @")\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); - if (simpleTitle.ToLowerInvariant().Contains(artist.Name.ToLowerInvariant())) + var match = releaseRegex.Matches(simpleTitle); + + if (match.Count != 0) { - result.ArtistName = artist.Name; - var artistRegex = new Regex(Regex.Escape(artist.Name.ToLowerInvariant())); - var albumReleaseString = artistRegex.Replace(simpleTitle.ToLowerInvariant(), string.Empty, 1); + try + { + var result = ParseAlbumMatchCollection(match); - var selectedAlbum = album.FirstOrDefault(s => albumReleaseString.Contains(s.Title.ToLowerInvariant())); + if (result != null) + { + result.Language = LanguageParser.ParseLanguage(releaseTitle); + Logger.Debug("Language parsed: {0}", result.Language); - if (selectedAlbum != null) - { - result.AlbumTitle = selectedAlbum.Title; - } + result.Quality = QualityParser.ParseQuality(title, null, 0); + Logger.Debug("Quality parsed: {0}", result.Quality); - result.Language = LanguageParser.ParseLanguage(releaseTitle); - Logger.Debug("Language parsed: {0}", result.Language); + result.ReleaseGroup = ParseReleaseGroup(releaseTitle); - result.Quality = QualityParser.ParseQuality(title, null, 0); - Logger.Debug("Quality parsed: {0}", result.Quality); + var subGroup = GetSubGroup(match); + if (!subGroup.IsNullOrWhiteSpace()) + { + result.ReleaseGroup = subGroup; + } - result.ReleaseGroup = ParseReleaseGroup(releaseTitle); + Logger.Debug("Release Group parsed: {0}", result.ReleaseGroup); - Logger.Debug("Release Group parsed: {0}", result.ReleaseGroup); + result.ReleaseHash = GetReleaseHash(match); + if (!result.ReleaseHash.IsNullOrWhiteSpace()) + { + Logger.Debug("Release Hash parsed: {0}", result.ReleaseHash); + } - return result; + return result; + } + } + catch (InvalidDateException ex) + { + Logger.Debug(ex, ex.Message); + } } } catch (Exception e)