Parallel downloads. Much fast.

This commit is contained in:
matt
2024-03-30 12:26:32 +01:00
parent c1a79f1124
commit fd461a2629

View File

@@ -1,6 +1,7 @@
using PowerArgs;
using Newtonsoft.Json.Linq;
using System.Net;
using System;
namespace SBT_Downloader
{
@@ -85,22 +86,25 @@ namespace SBT_Downloader
private static void DownloadMp3s(List<string> mp3Urls, List<string> chapters, MyArgs arguments)
{
int index = 0;
foreach (string url in mp3Urls)
{
string chapterName = chapters[index];
index++;
string fileName = $"{index} {chapterName}.mp3";
using var client = new WebClient();
Console.WriteLine("Downloading file #" + index);
client.DownloadFile(url, fileName);
TagFile(chapterName, index, chapters.Count, arguments);
}
Parallel.ForEach(
mp3Urls,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
(url, state, index) =>
{
int chapterNumber = (int)index + 1;
string chapterName = chapters[(int)index];
string fileName = $"{chapterNumber} {chapterName}.mp3";
using var client = new WebClient();
Console.WriteLine("Downloading file #" + index);
client.DownloadFile(url, fileName);
TagFile(chapterName, chapterNumber, chapters.Count, arguments);
});
}
private static void TagFile(string chapterName, int index, int chapterCount, MyArgs arguments)
private static void TagFile(string chapterName, int chapterNumber, int chapterCount, MyArgs arguments)
{
var file = TagLib.File.Create($"{index} {chapterName}.mp3");
Console.WriteLine($"Tagging chapter {chapterNumber} ({chapterCount})");
var file = TagLib.File.Create($"{chapterNumber} {chapterName}.mp3");
file.Tag.Album = arguments.Title.IsNullOrEmpty() ? string.Empty : arguments.Title;
List<string> artistsList = new List<string>();
@@ -108,7 +112,7 @@ namespace SBT_Downloader
if (!arguments.Narrator.IsNullOrEmpty()) artistsList.Add(arguments.Narrator);
file.Tag.AlbumArtists = artistsList.ToArray();
file.Tag.Title = chapterName;
file.Tag.Track = (uint)index;
file.Tag.Track = (uint)chapterNumber;
file.Tag.TrackCount = (uint)chapterCount;
file.Save();
@@ -132,5 +136,6 @@ namespace SBT_Downloader
}
}
}
}