diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetLocalTrackFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetLocalTrackFixture.cs index 01a52991f..055c9d022 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetLocalTrackFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/GetLocalTrackFixture.cs @@ -65,7 +65,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests .Returns(_fakeAlbum); Mocker.GetMock() - .Setup(s => s.FindTrackByTitle(_fakeArtist.Id, _fakeAlbum.Id, It.IsAny(), _fakeTrack.Title)) + .Setup(s => s.FindTrackByTitle(_fakeArtist.Id, _fakeAlbum.Id, It.IsAny(), It.IsAny(), _fakeTrack.Title)) .Returns(_fakeTrack); } diff --git a/src/NzbDrone.Core/Music/TrackService.cs b/src/NzbDrone.Core/Music/TrackService.cs index 59010c097..93a36c826 100644 --- a/src/NzbDrone.Core/Music/TrackService.cs +++ b/src/NzbDrone.Core/Music/TrackService.cs @@ -17,7 +17,7 @@ namespace NzbDrone.Core.Music Track GetTrack(int id); List GetTracks(IEnumerable ids); Track FindTrack(int artistId, int albumId, int mediumNumber, int trackNumber); - Track FindTrackByTitle(int artistId, int albumId, int mediumNumber, string releaseTitle); + Track FindTrackByTitle(int artistId, int albumId, int mediumNumber, int trackNumber, string releaseTitle); List GetTracksByArtist(int artistId); List GetTracksByAlbum(int albumId); //List GetTracksByAlbumTitle(string artistId, string albumTitle); @@ -76,7 +76,7 @@ namespace NzbDrone.Core.Music return _trackRepository.GetTracksByAlbum(albumId); } - public Track FindTrackByTitle(int artistId, int albumId, int mediumNumber, string releaseTitle) + public Track FindTrackByTitle(int artistId, int albumId, int mediumNumber, int trackNumber, string releaseTitle) { // TODO: can replace this search mechanism with something smarter/faster/better var normalizedReleaseTitle = Parser.Parser.NormalizeEpisodeTitle(releaseTitle).Replace(".", " "); @@ -88,11 +88,19 @@ namespace NzbDrone.Core.Music Position = normalizedReleaseTitle.IndexOf(Parser.Parser.NormalizeEpisodeTitle(track.Title), StringComparison.CurrentCultureIgnoreCase), Length = Parser.Parser.NormalizeEpisodeTitle(track.Title).Length, Track = track - }) - .Where(e => e.Track.Title.Length > 0 && e.Position >= 0) - .OrderBy(e => e.Position) - .ThenByDescending(e => e.Length) - .ToList(); + }); + + if (trackNumber == 0) + { + matches = matches.Where(e => e.Track.Title.Length > 0 && e.Position >= 0); + } else + { + matches = matches.Where(e => e.Track.Title.Length > 0 && e.Position >= 0 && e.Track.AbsoluteTrackNumber == trackNumber); + } + + matches.OrderBy(e => e.Position) + .ThenByDescending(e => e.Length) + .ToList(); if (matches.Any()) { diff --git a/src/NzbDrone.Core/Parser/ParsingService.cs b/src/NzbDrone.Core/Parser/ParsingService.cs index 98f1291d3..459dc285c 100644 --- a/src/NzbDrone.Core/Parser/ParsingService.cs +++ b/src/NzbDrone.Core/Parser/ParsingService.cs @@ -308,10 +308,16 @@ namespace NzbDrone.Core.Parser if (parsedTrackInfo.Title.IsNotNullOrWhiteSpace()) { + Track trackInfo; var cleanTrackTitle = Parser.CleanTrackTitle(parsedTrackInfo.Title); _logger.Debug("Cleaning Track title of common matching issues. Cleaned track title is '{0}'", cleanTrackTitle); - var trackInfo = _trackService.FindTrackByTitle(artist.Id, album.Id, parsedTrackInfo.DiscNumber, cleanTrackTitle); + trackInfo = _trackService.FindTrackByTitle(artist.Id, album.Id, parsedTrackInfo.DiscNumber, parsedTrackInfo.TrackNumbers.FirstOrDefault(), cleanTrackTitle); + + if (trackInfo == null) + { + trackInfo = _trackService.FindTrackByTitle(artist.Id, album.Id, parsedTrackInfo.DiscNumber, parsedTrackInfo.TrackNumbers.FirstOrDefault(), parsedTrackInfo.Title); + } if (trackInfo != null) {