mirror of
https://github.com/lidarr/Lidarr
synced 2025-01-03 13:34:54 +00:00
Fixed: Escape regex in ParseAlbumWithSearchCriteria (#244)
* Add regex escape to fix #231 * Add escape to artist name * Fix test case * Use single album parameter and add test cases * Add artist test cases * Add qualities to release titles * Create albums in ParserFixture * Added missing case in QualityParser. Handle escaping regex better for artists/albums that are just symbols. * Removed custom code to escape slashes. Enhanced regex to support more test cases. * Fixed Regex for other test cases. * Small enhancements to code. Removed log statement. * Tweaked one of my regex to account for not stripping ? from SimpleTitleRegex.
This commit is contained in:
parent
f6a1f5142a
commit
116d3d22bb
3 changed files with 44 additions and 11 deletions
|
@ -16,7 +16,7 @@ namespace NzbDrone.Core.Test.ParserTests
|
||||||
public class ParserFixture : CoreTest
|
public class ParserFixture : CoreTest
|
||||||
{
|
{
|
||||||
Artist _artist = new Artist();
|
Artist _artist = new Artist();
|
||||||
private List<Album> _albums = new List<Album>{new Album()};
|
private List<Album> _albums = new List<Album> { new Album() };
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
|
@ -24,12 +24,17 @@ public void Setup()
|
||||||
_artist = Builder<Artist>
|
_artist = Builder<Artist>
|
||||||
.CreateNew()
|
.CreateNew()
|
||||||
.Build();
|
.Build();
|
||||||
|
_albums = Builder<List<Album>>
|
||||||
|
.CreateNew()
|
||||||
|
.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenSearchCriteria(string artistName, string albumTitle)
|
private void GivenSearchCriteria(string artistName, string albumTitle)
|
||||||
{
|
{
|
||||||
_artist.Name = artistName;
|
_artist.Name = artistName;
|
||||||
_albums.First().Title = albumTitle;
|
var a = new Album();
|
||||||
|
a.Title = albumTitle;
|
||||||
|
_albums.Add(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("Bad Format", "badformat")]
|
[TestCase("Bad Format", "badformat")]
|
||||||
|
@ -43,7 +48,7 @@ public void should_parse_artist_name(string postTitle, string title)
|
||||||
public void should_remove_accents_from_title()
|
public void should_remove_accents_from_title()
|
||||||
{
|
{
|
||||||
const string title = "Carniv\u00E0le";
|
const string title = "Carniv\u00E0le";
|
||||||
|
|
||||||
title.CleanArtistName().Should().Be("carnivale");
|
title.CleanArtistName().Should().Be("carnivale");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,16 +87,16 @@ public void should_remove_request_info_from_title(string postTitle, string title
|
||||||
Parser.Parser.ParseAlbumTitle(postTitle).ArtistName.Should().Be(title);
|
Parser.Parser.ParseAlbumTitle(postTitle).ArtistName.Should().Be(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("02 Unchained.flac")]
|
[TestCase("02 Unchained.flac")] // This isn't valid on any regex we have. We must always have an artist
|
||||||
[TestCase("Fall Out Boy - 02 - Title.wav")]
|
[TestCase("Fall Out Boy - 02 - Title.wav")] // This isn't valid on any regex we have. We don't support Artist - Track - TrackName
|
||||||
public void should_parse_quality_from_extension(string title)
|
public void should_parse_quality_from_extension(string title)
|
||||||
{
|
{
|
||||||
Parser.Parser.ParseAlbumTitle(title).Quality.Quality.Should().NotBe(Quality.Unknown);
|
Parser.Parser.ParseAlbumTitle(title).Quality.Quality.Should().NotBe(Quality.Unknown);
|
||||||
Parser.Parser.ParseAlbumTitle(title).Quality.QualitySource.Should().Be(QualitySource.Extension);
|
Parser.Parser.ParseAlbumTitle(title).Quality.QualitySource.Should().Be(QualitySource.Extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("of Montreal-Hissing Fauna, Are You The Destroyer? 2007", "Hissing Fauna, Are You The Destroyer", "of Montreal", "2007")]
|
[TestCase("of Montreal-Hissing Fauna, Are You The Destroyer? 2007", "Hissing Fauna, Are You The Destroyer?", "of Montreal", "2007")]
|
||||||
[TestCase("of Montreal - 2007 - Hissing Fauna, Are You The Destroyer?", "Hissing Fauna, Are You The Destroyer", "of Montreal", "2007")]
|
[TestCase("of Montreal - 2007 - Hissing Fauna, Are You The Destroyer?", "Hissing Fauna, Are You The Destroyer?", "of Montreal", "2007")]
|
||||||
public void should_parse_album(string title, string correctAlbum, string correctArtist, string correctYear)
|
public void should_parse_album(string title, string correctAlbum, string correctArtist, string correctYear)
|
||||||
{
|
{
|
||||||
ParsedAlbumInfo result = Parser.Parser.ParseAlbumTitle(title);
|
ParsedAlbumInfo result = Parser.Parser.ParseAlbumTitle(title);
|
||||||
|
@ -203,5 +208,29 @@ public void should_not_parse_artist_name_and_album_title_by_incorrect_search_cri
|
||||||
var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria("Black Sabbath Black Sabbath FLAC", _artist, _albums);
|
var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria("Black Sabbath Black Sabbath FLAC", _artist, _albums);
|
||||||
parseResult.Should().BeNull();
|
parseResult.Should().BeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase("Ed Sheeran", "I See Fire", "Ed Sheeran I See Fire[Mimp3.eu].mp3 FLAC")]
|
||||||
|
[TestCase("Ed Sheeran", "Divide", "Ed Sheeran ? Divide FLAC")]
|
||||||
|
[TestCase("Ed Sheeran", "+", "Ed Sheeran + FLAC")]
|
||||||
|
//[TestCase("Glasvegas", @"EUPHORIC /// HEARTBREAK \\\", @"EUPHORIC /// HEARTBREAK \\\ FLAC")] // slashes not being escaped properly
|
||||||
|
[TestCase("XXXTENTACION", "?", "XXXTENTACION ? FLAC")]
|
||||||
|
[TestCase("Hey", "BŁYSK", "Hey - BŁYSK FLAC")]
|
||||||
|
public void should_escape_albums(string artist, string album, string releaseTitle)
|
||||||
|
{
|
||||||
|
GivenSearchCriteria(artist, album);
|
||||||
|
var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria(releaseTitle, _artist, _albums);
|
||||||
|
parseResult.AlbumTitle.Should().Be(album);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("???", "Album", "??? Album FLAC")]
|
||||||
|
[TestCase("+", "Album", "+ Album FLAC")]
|
||||||
|
[TestCase(@"/\", "Album", @"/\ Album FLAC")]
|
||||||
|
[TestCase("+44", "When Your Heart Stops Beating", "+44 When Your Heart Stops Beating FLAC")]
|
||||||
|
public void should_escape_artists(string artist, string album, string releaseTitle)
|
||||||
|
{
|
||||||
|
GivenSearchCriteria(artist, album);
|
||||||
|
var parseResult = Parser.Parser.ParseAlbumTitleWithSearchCriteria(releaseTitle, _artist, _albums);
|
||||||
|
parseResult.ArtistName.Should().Be(artist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ public static class Parser
|
||||||
|
|
||||||
//Artist-Album Year
|
//Artist-Album Year
|
||||||
//Hyphen no space between artist and album
|
//Hyphen no space between artist and album
|
||||||
new Regex(@"^(?:(?<artist>.+?)(?:-)+)(?<album>.+?)\W*(?<releaseyear>\d{4})",
|
new Regex(@"^(?:(?<artist>.+?)(?:-)+)(?<album>.+?)\b(?<releaseyear>\d{4})",
|
||||||
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
||||||
|
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ public static class Parser
|
||||||
private static readonly Regex FileExtensionRegex = new Regex(@"\.[a-z0-9]{2,4}$",
|
private static readonly Regex FileExtensionRegex = new Regex(@"\.[a-z0-9]{2,4}$",
|
||||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||||
|
|
||||||
private static readonly Regex SimpleTitleRegex = new Regex(@"(?:(480|720|1080|2160|320)[ip]|[xh][\W_]?26[45]|DD\W?5\W1|[<>?*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?)\s*",
|
private static readonly Regex SimpleTitleRegex = new Regex(@"(?:(480|720|1080|2160|320)[ip]|[xh][\W_]?26[45]|DD\W?5\W1|[<>*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?)\s*",
|
||||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||||
|
|
||||||
private static readonly Regex WebsitePrefixRegex = new Regex(@"^\[\s*[a-z]+(\.[a-z]+)+\s*\][- ]*",
|
private static readonly Regex WebsitePrefixRegex = new Regex(@"^\[\s*[a-z]+(\.[a-z]+)+\s*\][- ]*",
|
||||||
|
@ -351,8 +351,11 @@ public static ParsedAlbumInfo ParseAlbumTitleWithSearchCriteria(string title, Ar
|
||||||
|
|
||||||
simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty);
|
simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty);
|
||||||
|
|
||||||
var releaseRegex = new Regex(@"\b(?<artist>" + artist.Name + @")\b.*\b(?<album>"+ string.Join("|",album.Select(s=>s.Title).ToList()) + @")\b",
|
var escapedArtist = Regex.Escape(artist.Name);
|
||||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
var escapedAlbums = Regex.Escape(string.Join("|", album.Select(s => s.Title).ToList()));
|
||||||
|
|
||||||
|
var releaseRegex = new Regex(@"^(\W*|\b)(?<artist>" + escapedArtist + @")(\W*|\b).*(\W*|\b)(?<album>" + escapedAlbums + @")(\W*|\b)", RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
|
||||||
var match = releaseRegex.Matches(simpleTitle);
|
var match = releaseRegex.Matches(simpleTitle);
|
||||||
|
|
||||||
|
|
|
@ -122,6 +122,7 @@ public static QualityModel ParseQuality(string name, string desc, int fileBitrat
|
||||||
else if (bitrate == BitRate.B256) { result.Quality = Quality.VORBIS_Q8; }
|
else if (bitrate == BitRate.B256) { result.Quality = Quality.VORBIS_Q8; }
|
||||||
else if (bitrate == BitRate.B320) { result.Quality = Quality.VORBIS_Q9; }
|
else if (bitrate == BitRate.B320) { result.Quality = Quality.VORBIS_Q9; }
|
||||||
else if (bitrate == BitRate.B500) { result.Quality = Quality.VORBIS_Q10; }
|
else if (bitrate == BitRate.B500) { result.Quality = Quality.VORBIS_Q10; }
|
||||||
|
else { result.Quality = Quality.Unknown; }
|
||||||
break;
|
break;
|
||||||
case Codec.Unknown:
|
case Codec.Unknown:
|
||||||
if (bitrate == BitRate.B192) { result.Quality = Quality.MP3_192; }
|
if (bitrate == BitRate.B192) { result.Quality = Quality.MP3_192; }
|
||||||
|
|
Loading…
Reference in a new issue