From a851da96e05ba6cae4867ced9e406053e644e5a7 Mon Sep 17 00:00:00 2001 From: fiso64 Date: Thu, 21 Dec 2023 17:35:04 +0100 Subject: [PATCH] stuff --- README.md | 2 +- slsk-batchdl/Program.cs | 57 +++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 997bef1..801483a 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ slsk-batchdl "title=MC MENTAL @ HIS BEST,length=242" --pref-format "flac,wav" #### Find an artist's songs which aren't in your library: ``` -slsk-batchdl "artist=MC MENTAL" --aggregate --print tracks --skip-existing --music-dir "path\to\music" +slsk-batchdl "artist=MC MENTAL" --aggregate --print tracks-full --skip-existing --music-dir "path\to\music" ``` ### Options: diff --git a/slsk-batchdl/Program.cs b/slsk-batchdl/Program.cs index 7f3107f..f2b0e8f 100644 --- a/slsk-batchdl/Program.cs +++ b/slsk-batchdl/Program.cs @@ -1257,9 +1257,9 @@ static class Program }, fileFilter: (file) => { return IsMusicFile(file.Filename) && necessaryCond.FileSatisfies(file, track, null) - && (relax || FileConditions.StrictString(file.Filename, track.ArtistName, ignoreCase: true) - && FileConditions.StrictString(file.Filename, track.TrackTitle, ignoreCase: true) - && FileConditions.StrictString(file.Filename, track.Album, ignoreCase: true)); + && FileConditions.StrictString(file.Filename, track.ArtistName, ignoreCase: true) + && FileConditions.StrictString(file.Filename, track.TrackTitle, ignoreCase: true) + && FileConditions.StrictString(file.Filename, track.Album, ignoreCase: true); }); Action handler = (r) => { if (r.Files.Count() > 0) @@ -1288,7 +1288,17 @@ static class Program var fileResponses = results.Select(x => x.Value); - var equivalentFiles = EquivalentFiles(track, fileResponses); + var equivalentFiles = EquivalentFiles(track, fileResponses).ToList(); + + if (!relax) + { + equivalentFiles = equivalentFiles + .Where(x => FileConditions.StrictString(x.Item1.TrackTitle, track.TrackTitle, ignoreCase: true) + && (FileConditions.StrictString(x.Item1.ArtistName, track.ArtistName, ignoreCase: true) + || FileConditions.StrictString(x.Item1.TrackTitle, track.ArtistName, ignoreCase: true) + && x.Item1.TrackTitle.ContainsInBrackets(track.ArtistName, ignoreCase: true))) + .ToList(); + } var tracks = equivalentFiles .Select(kvp => { @@ -1449,12 +1459,16 @@ static class Program Track t = new Track(defaultTrack); filename = GetFileNameWithoutExtSlsk(filename).Replace(" — ", " - ").Replace("_", " ").RemoveConsecutiveWs().Trim(); - var trackNumStart = @"^(?:(?:[0-9][-\.])?\d{2,3}[. -]|\b\d\.\s|\b\d\s-\s)(?=.+\S)"; - var trackNumMiddle = @"(-\s*(\d-)?\d{2,3}|\d{2,3}\.?)\s+"; + var trackNumStart = new Regex(@"^(?:(?:[0-9][-\.])?\d{2,3}[. -]|\b\d\.\s|\b\d\s-\s)(?=.+\S)"); + var trackNumMiddle = new Regex(@"(?<=- )((\d-)?\d{2,3}|\d{2,3}\.?)\s+"); - filename = Regex.Replace(filename, trackNumStart, "").Trim(); - filename = Regex.Replace(filename, trackNumMiddle, "").Trim(); - if (filename.StartsWith("- ")) filename = filename.Substring(2).Trim(); + if (trackNumStart.IsMatch(filename)) + filename = trackNumStart.Replace(filename, "", 1).Trim(); + else + filename = trackNumMiddle.Replace(filename, "", 1).Trim(); + + if (filename.StartsWith("- ")) + filename = filename.Substring(2).Trim(); string aname = t.ArtistName.Trim(); string tname = t.TrackTitle.Trim(); @@ -1487,11 +1501,11 @@ static class Program if (!parts[0].ContainsIgnoreCase(aname) || !parts[1].ContainsIgnoreCase(tname)) { t.ArtistMaybeWrong = true; - if (!maybeRemix && parts[0].ContainsIgnoreCase(tname) && parts[1].ContainsIgnoreCase(aname)) - { - t.ArtistName = realParts[1]; - t.TrackTitle = realParts[0]; - } + //if (!maybeRemix && parts[0].ContainsIgnoreCase(tname) && parts[1].ContainsIgnoreCase(aname)) + //{ + // t.ArtistName = realParts[1]; + // t.TrackTitle = realParts[0]; + //} } } @@ -2960,6 +2974,21 @@ public static class Utils return Regex.IsMatch(str, pattern, options); } + public static bool ContainsInBrackets(this string str, string searchTerm, bool ignoreCase=false) + { + var regex = new Regex(@"\[(.*?)\]|\((.*?)\)"); + var matches = regex.Matches(str); + var comp = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + foreach (Match match in matches) + { + if (match.Value.Contains(searchTerm, comp)) + return true; + } + + return false; + } + public static bool RemoveRegexIfExist(this string s, string reg, out string res) { res = Regex.Replace(s, reg, string.Empty);