From 6e73d73e3eca9e065453517e683d8f7c71ae44eb Mon Sep 17 00:00:00 2001 From: dinnerhe <61226161+dinnerhe@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:23:08 -0500 Subject: [PATCH] Add C# implementation --- README.md | 9 +++ mkbsdSharp/.gitignore | 3 + mkbsdSharp/Program.cs | 104 +++++++++++++++++++++++++++++++++++ mkbsdSharp/mkbsdSharp.csproj | 10 ++++ mkbsdSharp/mkbsdSharp.sln | 16 ++++++ 5 files changed, 142 insertions(+) create mode 100644 mkbsdSharp/.gitignore create mode 100644 mkbsdSharp/Program.cs create mode 100644 mkbsdSharp/mkbsdSharp.csproj create mode 100644 mkbsdSharp/mkbsdSharp.sln diff --git a/README.md b/README.md index 2d34182..9efc6ba 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ MKBSD comes in two variants! Node.js and Python. 4. Wait a little. 5. All wallpapers are now in a newly created `downloads` subfolder. +### Running in C# + +1. Ensure you have dotnet installed. +2. Navigate to `mkbsdSharp` folder. +3. Run `dotnet run` +4. Wait a little. +4. All wallpapers are now in a newly created `downloads` subfolder. + + ## FAQ ### Q: What's the story behind this? diff --git a/mkbsdSharp/.gitignore b/mkbsdSharp/.gitignore new file mode 100644 index 0000000..d8715dc --- /dev/null +++ b/mkbsdSharp/.gitignore @@ -0,0 +1,3 @@ +.idea/ +bin/ +obj/ \ No newline at end of file diff --git a/mkbsdSharp/Program.cs b/mkbsdSharp/Program.cs new file mode 100644 index 0000000..fccdc43 --- /dev/null +++ b/mkbsdSharp/Program.cs @@ -0,0 +1,104 @@ +// Licensed under the WTFPL License + +using System.Text.Json; + + +class Program +{ + private static readonly HttpClient Client = new(); + + static async Task Main(string[] args) + { + AsciiArt(); + await Task.Delay(1000); + string url = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s"; + + try + { + HttpResponseMessage response = await Client.GetAsync(url); + if (!response.IsSuccessStatusCode) + { + throw new Exception($"⛔ Failed to fetch JSON file: {response.ReasonPhrase}"); + } + + DataType? jsonData = + await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync()); + var data = jsonData?.data; + + + if (data == null) + { + throw new Exception("⛔ JSON does not have a 'data' property at its root."); + } + + string downloadDir = Path.Combine(Directory.GetCurrentDirectory(), "downloads"); + if (!Directory.Exists(downloadDir)) + { + Directory.CreateDirectory(downloadDir); + Console.WriteLine($"📁 Created directory: {downloadDir}"); + } + + var keyvaluepair = data.Values.ToList(); + for(int i=0; i< keyvaluepair.Count; i++ ) + { + var subproperty = keyvaluepair[i]; + if (subproperty.TryGetValue("dhd", out var imageUrl)) + { + Console.WriteLine("🔍 Found image URL!"); + string ext = Path.GetExtension(new Uri(imageUrl).AbsolutePath) ?? ".jpg"; + string filename = $"{i}{ext}"; + string filePath = Path.Combine(downloadDir, filename); + await DownloadImage(imageUrl, filePath); + } + } + + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + + } + } + + + static async Task DownloadImage(string url, string filePath) + { + + await Task.Delay(100); + HttpResponseMessage response = await Client.GetAsync(url); + if (!response.IsSuccessStatusCode) + { + throw new Exception($"Failed to download image: {response.ReasonPhrase}"); + } + + byte[] imageBytes = await response.Content.ReadAsByteArrayAsync(); + await File.WriteAllBytesAsync(filePath, imageBytes); + Console.WriteLine($"🖼️ Saved image to {filePath}"); + await Task.Delay(250); + } + + static void AsciiArt() + { + Console.WriteLine(@" + /$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ +| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$ +| $$$$ /$$$$| $$ /$$/ | $$ \ $$| $$ \__/| $$ \ $$ +| $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$ +| $$ $$$| $$| $$ $$ | $$__ $$ \____ $$| $$ | $$ +| $$\ $ | $$| $$\ $$ | $$ \ $$ /$$ \ $$| $$ | $$ +| $$ \/ | $$| $$ \ $$| $$$$$$$/| $$$$$$/| $$$$$$$/ +|__/ |__/|__/ \__/|_______/ \______/ |_______/"); + Console.WriteLine(); + Console.WriteLine("🤑 Starting downloads from your favorite sellout grifter's wallpaper app..."); + } + + private class DataType + { + public int version { get; set; } + public Dictionary>? data { get; set; } + } + + +} + + diff --git a/mkbsdSharp/mkbsdSharp.csproj b/mkbsdSharp/mkbsdSharp.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/mkbsdSharp/mkbsdSharp.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/mkbsdSharp/mkbsdSharp.sln b/mkbsdSharp/mkbsdSharp.sln new file mode 100644 index 0000000..3cfef8a --- /dev/null +++ b/mkbsdSharp/mkbsdSharp.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mkbsdSharp", "mkbsdSharp.csproj", "{F6B460ED-5903-4500-8C1E-93EAD8971B12}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6B460ED-5903-4500-8C1E-93EAD8971B12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6B460ED-5903-4500-8C1E-93EAD8971B12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6B460ED-5903-4500-8C1E-93EAD8971B12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6B460ED-5903-4500-8C1E-93EAD8971B12}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal