Files now named for chapter. Prepended with chapter number for easy sorting and playing.

This commit is contained in:
matt
2024-03-30 10:44:26 +01:00
parent f34b6d5ab9
commit cf38b7f83c
2 changed files with 64 additions and 25 deletions

View File

@@ -13,7 +13,8 @@ namespace SBT_Downloader
{
var parsed = Args.Parse<MyArgs>(args);
Console.WriteLine("Downloading book now...");
DownloadBook(parsed.Har);
(List<string> mp3Urls, List<string> chapters) = ParseHar(parsed.Har);
DownloadMp3s(mp3Urls, chapters);
}
catch (ArgException ex)
{
@@ -22,23 +23,74 @@ namespace SBT_Downloader
}
}
static void DownloadBook(string fileName)
static (List<string> mp3Urls, List<string> chapters) ParseHar(string har)
{
string harContents = LoadHar(fileName);
JObject harObject = JObject.Parse(harContents);
List<string> mp3Urls = ParseUrls(harObject);
_ = DownloadMp3sAsync(mp3Urls);
try
{
string harContents = LoadHar(har);
JObject harObject = JObject.Parse(harContents);
List<JToken?> requestObjects = ParseRequests(harObject);
List<string> mp3Urls = ParseUrls(requestObjects);
List<string> chapters = ParseChapters(mp3Urls);
return (mp3Urls, chapters);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(0);
return (new List<string>(), new List<string>());
}
}
private static async Task DownloadMp3sAsync(List<string> mp3Urls)
private static List<JToken?> ParseRequests(JObject harObject)
{
int fileNumber = 1;
List<JToken?> requests = harObject["log"]["entries"]
.Where(x => x.SelectToken("_resourceType").ToString() == "media")
.Select(y => y.SelectToken("request"))
.DistinctBy(z => z.SelectToken("url")).ToList();
return requests;
}
private static List<string> ParseChapters(List<string> mp3Urls)
{
List<string> chapters = new();
foreach (string url in mp3Urls)
{
Uri uri = new(url);
string fixedUri = uri.AbsoluteUri.Replace(uri.Query, string.Empty);
List<string> urlTokens = fixedUri.Split("/").ToList();
string series = urlTokens[5];
string title = urlTokens[6];
string episode = urlTokens[7];
var chapterTokens = urlTokens.Last().Split("_").ToList();
chapterTokens.RemoveRange(0,5);
string chapterLastToken = chapterTokens.Last().Split(".").First();
chapterTokens[chapterTokens.Count - 1] = chapterLastToken;
chapters.Add(string.Join(" ", chapterTokens));
}
return chapters;
}
static List<string> ParseUrls(List<JToken> requestList)
{
List<string> urls = requestList.Select(x => x.SelectToken("url").ToString()).ToList();
Console.WriteLine(urls.Count + " total requests");
return urls;
}
private static void DownloadMp3s(List<string> mp3Urls, List<string> chapters)
{
int index = 0;
foreach (string url in mp3Urls)
{
using var client = new WebClient();
client.DownloadFile(url, "file" + fileNumber + ".mp3");
Console.WriteLine("Downloading file #" + fileNumber);
fileNumber++;
client.DownloadFile(url, $"{index} {chapters[index]}.mp3");
Console.WriteLine("Downloading file #" + ++index);
}
}
@@ -60,18 +112,5 @@ namespace SBT_Downloader
}
}
static List<string> ParseUrls(JObject harObject)
{
List<string> urls = harObject["log"]["entries"]
.Where(x => x.SelectToken("_resourceType").ToString() == "media")
.Select(y => y.SelectToken("request"))
.DistinctBy(z => z.SelectToken("url"))
.Select(x => x.SelectToken("url").ToString()).ToList();
Console.WriteLine(urls.Count + " total requests");
return urls;
}
}
}

View File

@@ -2,7 +2,7 @@
"profiles": {
"SBT Downloader": {
"commandName": "Project",
"commandLineArgs": "-har \"C:\\Repos\\sbt-downloader\\soundbooththeater.com.har\""
"commandLineArgs": "-har \"C:\\Repos\\sbt-downloader\\episode2.soundbooththeater.com.har\""
}
}
}