Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fiso64 committed Jun 24, 2024
1 parent 93fb18f commit b0fc223
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Options:
'name-precise' (default): Use filenames and check conditions
'tag': Use file tags (slower)
'tag-precise': Use file tags and check file conditions
'm3u': Skip all tracks that don't have a fail entry in m3u
--music-dir <path> Specify to also skip downloading tracks found in a music
library. Use with --skip-existing
--skip-not-found Skip searching for tracks that weren't found on Soulseek
Expand Down
104 changes: 69 additions & 35 deletions slsk-batchdl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static class Program
static string timeUnit = "s";
static string displayStyle = "single";
static string input = "";
static bool preciseSkip = true;
static string nameFormat = "";
static string invalidReplaceStr = " ";
static bool skipNotFound = false;
Expand All @@ -114,7 +113,6 @@ static class Program
static string ytdlpArgument = "";
static bool skipExisting = false;
static string m3uOption = "fails";
static bool useTagsCheckExisting = false;
static bool removeTracksFromSource = false;
static bool getDeleted = false;
static bool deletedOnly = false;
Expand Down Expand Up @@ -168,6 +166,17 @@ public enum InputType
CSV
};

static SkipMode skipMode = SkipMode.NamePrecise;

public enum SkipMode
{
Name,
NamePrecise,
Tag,
TagPrecise,
M3u,
};

static void PrintHelp()
{
Console.WriteLine("Usage: sldl <input> [OPTIONS]" +
Expand Down Expand Up @@ -280,6 +289,7 @@ static void PrintHelp()
"\n 'name-precise' (default): Use filenames and check conditions" +
"\n 'tag': Use file tags (slower)" +
"\n 'tag-precise': Use file tags and check file conditions" +
"\n 'm3u': Skip all tracks that don't have a fail entry in m3u" +
"\n --music-dir <path> Specify to also skip downloading tracks found in a music" +
"\n library. Use with --skip-existing" +
"\n --skip-not-found Skip searching for tracks that weren't found on Soulseek" +
Expand Down Expand Up @@ -706,7 +716,7 @@ static async Task Main(string[] args)
albumArtOption = "";
break;
default:
throw new ArgumentException($"Invalid album art download mode \'{args[i]}\'");
throw new ArgumentException($"Invalid album art download mode '{args[i]}'");
}
break;
case "--aao":
Expand Down Expand Up @@ -852,11 +862,6 @@ static async Task Main(string[] args)
case "--no-modify-share-count":
noModifyShareCount = true;
break;
case "--seut":
case "--skip-existing-use-tags":
skipExisting = true;
useTagsCheckExisting = true;
break;
case "-d":
case "--desperate":
desperateSearch = true;
Expand All @@ -872,23 +877,20 @@ static async Task Main(string[] args)
displayStyle = args[i];
break;
default:
throw new ArgumentException($"Invalid display style \"{args[i]}\"");
throw new ArgumentException($"Invalid display style '{args[i]}'");
}
break;
case "--sm":
case "--skip-mode":
switch (args[++i])
skipMode = args[++i].ToLower().Trim() switch
{
case "name":
case "name-precise":
case "tag":
case "tag-precise":
useTagsCheckExisting = args[i].Contains("tag");
preciseSkip = args[i].Contains("-precise");
break;
default:
throw new ArgumentException($"Invalid skip mode \'{args[i]}\'");
}
"name" => SkipMode.Name,
"name-precise" => SkipMode.NamePrecise,
"tag" => SkipMode.Tag,
"tag-precise" => SkipMode.TagPrecise,
"m3u" => SkipMode.M3u,
_ => throw new ArgumentException($"Invalid skip mode '{args[i]}'"),
};
break;
case "--nrsc":
case "--no-remove-special-chars":
Expand Down Expand Up @@ -1497,19 +1499,27 @@ static void PrintTracksTbd(List<Track> tracks, List<Track> existing, List<Track>
static List<Track> DoSkipExisting(List<Track> tracks, bool print, bool useCache)
{
var existing = new Dictionary<Track, string>();
if (!(musicDir != "" && outputFolder.StartsWith(musicDir, StringComparison.OrdinalIgnoreCase)) && System.IO.Directory.Exists(outputFolder))

if (skipMode == SkipMode.M3u)
{
var d = SkipExisting(tracks, outputFolder, necessaryCond, useTagsCheckExisting, preciseSkip, useCache);
d.ToList().ForEach(x => existing.TryAdd(x.Key, x.Value));
existing = SkipExistingM3u(tracks);
}
if (musicDir != "" && System.IO.Directory.Exists(musicDir))
else
{
if (print) Console.WriteLine($"Checking if tracks exist in library..");
var d = SkipExisting(tracks, musicDir, necessaryCond, useTagsCheckExisting, preciseSkip, useCache);
d.ToList().ForEach(x => existing.TryAdd(x.Key, x.Value));
if (!(musicDir != "" && outputFolder.StartsWith(musicDir, StringComparison.OrdinalIgnoreCase)) && System.IO.Directory.Exists(outputFolder))
{
var d = SkipExisting(tracks, outputFolder, necessaryCond, skipMode, useCache);
d.ToList().ForEach(x => existing.TryAdd(x.Key, x.Value));
}
if (musicDir != "" && System.IO.Directory.Exists(musicDir))
{
if (print) Console.WriteLine($"Checking if tracks exist in library..");
var d = SkipExisting(tracks, musicDir, necessaryCond, skipMode, useCache);
d.ToList().ForEach(x => existing.TryAdd(x.Key, x.Value));
}
else if (musicDir != "" && !System.IO.Directory.Exists(musicDir))
if (print) Console.WriteLine($"Music dir does not exist: {musicDir}");
}
else if (musicDir != "" && !System.IO.Directory.Exists(musicDir))
if (print) Console.WriteLine($"Musid dir does not exist: {musicDir}");

return existing.Select(x => x.Key).ToList();
}
Expand Down Expand Up @@ -3950,11 +3960,13 @@ static bool TrackExistsInCollection(Track track, FileConditions conditions, IEnu
return false;
}

static Dictionary<Track, string> SkipExisting(List<Track> tracks, string dir, FileConditions necessaryCond, bool useTags, bool precise, bool useCache)
static Dictionary<Track, string> SkipExisting(List<Track> tracks, string dir, FileConditions necessaryCond, SkipMode mode, bool useCache)
{
var existing = new Dictionary<Track, string>();
List<string> musicFiles;
List<TagLib.File> musicIndex;
bool useTags = mode == SkipMode.Tag || mode == SkipMode.TagPrecise;
bool precise = mode == SkipMode.NamePrecise || mode == SkipMode.TagPrecise;

if (useCache && MusicCache.TryGetValue(dir, out var cached))
{
Expand Down Expand Up @@ -3991,6 +4003,22 @@ static Dictionary<Track, string> SkipExisting(List<Track> tracks, string dir, Fi
return existing;
}

static Dictionary<Track, string> SkipExistingM3u(List<Track> tracks)
{
var existing = new Dictionary<Track, string>();

for (int i = 0; i < tracks.Count; i++)
{
if (!m3uEditor.HasFail(tracks[i], out _))
{
existing.TryAdd(tracks[i], "");
tracks[i] = new Track(tracks[i]) { TrackState = Track.State.Exists, DownloadPath = "" };
}
}

return existing;
}

static List<TagLib.File> BuildMusicIndex(List<string> musicFiles)
{
var musicIndex = new List<TagLib.File>();
Expand Down Expand Up @@ -4761,18 +4789,18 @@ public M3UEditor(string m3uPath, string outputFolder, TrackLists trackLists, int
this.offset = offset;
this.option = option;
path = Path.GetFullPath(m3uPath);
m3uListLabels = false;/*trackLists.lists.Any(x => x.type != TrackLists.ListType.Normal);*/
fails = ReadAllLines()
.Where(x => x.StartsWith("# Failed: "))

var lines = ReadAllLines();
fails = lines.Where(x => x.StartsWith("# Failed: "))
.Select(line =>
{
var lastBracketIndex = line.LastIndexOf('[');
lastBracketIndex = lastBracketIndex == -1 ? line.Length : lastBracketIndex;
var key = line.Substring("# Failed: ".Length, lastBracketIndex - "# Failed: ".Length).Trim();
var value = lastBracketIndex != line.Length ? line.Substring(lastBracketIndex + 1).Trim().TrimEnd(']') : "";
return new { Key = key, Value = value };
return new { Key = key, Reason = value };
})
.ToSafeDictionary(pair => pair.Key, pair => pair.Value);
.ToSafeDictionary(pair => pair.Key, pair => pair.Reason);
}

public void Update()
Expand Down Expand Up @@ -4829,6 +4857,12 @@ void updateLine(string newLine)
if (type != TrackLists.ListType.Normal)
index++;
}
else if (option == "fails" && track.TrackState == Track.State.Downloaded && index < lines.Count && lines[index].StartsWith($"# Failed: {track}"))
{
lines[index] = "";
needUpdate = true;
}

if (type == TrackLists.ListType.Normal)
index++;
}
Expand Down
2 changes: 1 addition & 1 deletion slsk-batchdl/slsk-batchdl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.63.0.3205" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.54" />
<PackageReference Include="SmallestCSVParser" Version="1.1.1" />
<PackageReference Include="Soulseek" Version="6.4.1" />
<PackageReference Include="Soulseek" Version="6.5.0" />
<PackageReference Include="SpotifyAPI.Web" Version="7.1.1" />
<PackageReference Include="SpotifyAPI.Web.Auth" Version="7.1.1" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
Expand Down

0 comments on commit b0fc223

Please sign in to comment.