From f34b6d5ab96bb26624c520ab6e1f726ab640e01f Mon Sep 17 00:00:00 2001 From: matt <18199813engirugger42@users.noreply.github.com> Date: Sat, 30 Mar 2024 09:24:37 +0100 Subject: [PATCH] Working first pass. Downloads all files with generic filename. Does not parse title. Does not write metadata. --- MyArgs.cs | 14 +++++++ Program.cs | 77 ++++++++++++++++++++++++++++++++++ Properties/launchSettings.json | 8 ++++ SBT Downloader.csproj | 16 +++++++ SBT Downloader.sln | 25 +++++++++++ SbtHar.cs | 12 ++++++ 6 files changed, 152 insertions(+) create mode 100644 MyArgs.cs create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 SBT Downloader.csproj create mode 100644 SBT Downloader.sln create mode 100644 SbtHar.cs diff --git a/MyArgs.cs b/MyArgs.cs new file mode 100644 index 0000000..6c643d9 --- /dev/null +++ b/MyArgs.cs @@ -0,0 +1,14 @@ +using PowerArgs; + +namespace SBT_Downloader +{ + [TabCompletion] + public class MyArgs + { + // This argument is required and if not specified the user + // will be prompted. It has the short '-h', but tab completion + // Is support + [ArgRequired(PromptIfMissing = true), ArgShortcut("-h")] + public string Har { get; set; } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1d2ca46 --- /dev/null +++ b/Program.cs @@ -0,0 +1,77 @@ +using PowerArgs; +using Newtonsoft.Json.Linq; +using System.Net; + +namespace SBT_Downloader +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Welcome to the Sound Booth Theater Personal Archiving Tool!"); + try + { + var parsed = Args.Parse(args); + Console.WriteLine("Downloading book now..."); + DownloadBook(parsed.Har); + } + catch (ArgException ex) + { + Console.WriteLine(ex.Message); + Console.WriteLine(ArgUsage.GenerateUsageFromTemplate()); + } + } + + static void DownloadBook(string fileName) + { + string harContents = LoadHar(fileName); + JObject harObject = JObject.Parse(harContents); + List mp3Urls = ParseUrls(harObject); + _ = DownloadMp3sAsync(mp3Urls); + } + + private static async Task DownloadMp3sAsync(List mp3Urls) + { + int fileNumber = 1; + foreach (string url in mp3Urls) + { + using var client = new WebClient(); + client.DownloadFile(url, "file" + fileNumber + ".mp3"); + Console.WriteLine("Downloading file #" + fileNumber); + fileNumber++; + } + } + + static string LoadHar(string harFileName) + { + string harContents; + try + { + using StreamReader streamReader = new(harFileName); + harContents = streamReader.ReadToEnd(); + return harContents; + } + catch (Exception e) + { + Console.WriteLine("The file could not be read:"); + Console.WriteLine(e.Message); + Environment.Exit(0); + return string.Empty; + } + } + + static List ParseUrls(JObject harObject) + { + List 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; + } + + } +} \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..a4d4879 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "SBT Downloader": { + "commandName": "Project", + "commandLineArgs": "-har \"C:\\Repos\\sbt-downloader\\soundbooththeater.com.har\"" + } + } +} \ No newline at end of file diff --git a/SBT Downloader.csproj b/SBT Downloader.csproj new file mode 100644 index 0000000..f9af13e --- /dev/null +++ b/SBT Downloader.csproj @@ -0,0 +1,16 @@ + + + + Exe + net6.0 + SBT_Downloader + enable + enable + + + + + + + + diff --git a/SBT Downloader.sln b/SBT Downloader.sln new file mode 100644 index 0000000..5482da7 --- /dev/null +++ b/SBT Downloader.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32014.148 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SBT Downloader", "SBT Downloader.csproj", "{F4898EE2-CBCD-4301-B5ED-9D73BB1F4425}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F4898EE2-CBCD-4301-B5ED-9D73BB1F4425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4898EE2-CBCD-4301-B5ED-9D73BB1F4425}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4898EE2-CBCD-4301-B5ED-9D73BB1F4425}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4898EE2-CBCD-4301-B5ED-9D73BB1F4425}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3EE4D311-5E9A-4BBD-804B-C8D508703F30} + EndGlobalSection +EndGlobal diff --git a/SbtHar.cs b/SbtHar.cs new file mode 100644 index 0000000..b3ea11a --- /dev/null +++ b/SbtHar.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SBT_Downloader +{ + internal class SbtHar + { + } +}