From 2a5d73608da9e680cf9ce277df590049199709c4 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 14 May 2024 19:00:53 +0200 Subject: [PATCH] Robot image download --- RC2BotArchiver/DownloadImages.cs | 23 +++++++++++++++++ RC2BotArchiver/Program.cs | 42 ++++++++++++++++---------------- 2 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 RC2BotArchiver/DownloadImages.cs diff --git a/RC2BotArchiver/DownloadImages.cs b/RC2BotArchiver/DownloadImages.cs new file mode 100644 index 0000000..adccb0b --- /dev/null +++ b/RC2BotArchiver/DownloadImages.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace RC2BotArchiver; + +public static class DownloadImages +{ + public static async Task DoIt(HttpClient client) + { + foreach (var dir in Directory.EnumerateDirectories("robots")) + { + var json = await File.ReadAllTextAsync(Path.Combine(dir, "metadata.json")); + var doc = JsonDocument.Parse(json); + var url = doc.RootElement.GetProperty("image").GetString(); + if (url is null) continue; + var response = await client.GetAsync(url); + await using var stream = await response.Content.ReadAsStreamAsync(); + await using var fs = File.OpenWrite(Path.Combine(dir, "thumbnail.jpg")); + await stream.CopyToAsync(fs); + Console.WriteLine($"Saved image for {doc.RootElement.GetProperty("name")}"); + } + } +} \ No newline at end of file diff --git a/RC2BotArchiver/Program.cs b/RC2BotArchiver/Program.cs index 3c21374..ed88e60 100644 --- a/RC2BotArchiver/Program.cs +++ b/RC2BotArchiver/Program.cs @@ -6,11 +6,15 @@ using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; +using RC2BotArchiver; Console.WriteLine("Hello, World!"); var client = new HttpClient(); +await DownloadImages.DoIt(client); + +/* async Task GetResponse(HttpResponseMessage result, string url) { if (result.StatusCode == HttpStatusCode.OK) @@ -69,29 +73,25 @@ var progressToken = responseJson.Value.GetProperty("token").GetString(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", progressToken); const string factoryUrl = "https://factory.production.robocraft2.com"; - -int page = 408; -while (true) +; +Console.WriteLine($"Getting owned robots"); +responseJson = await Get(factoryUrl + "/v1/foundry/garage"); +if (!responseJson.HasValue) throw new Exception("Failed to get robots!"); +var results = responseJson.Value.GetProperty("vehicles"); +foreach (var result in results.EnumerateArray()) { - Console.WriteLine($"Getting page {page}"); - responseJson = await Get(factoryUrl + $"/v1/foundry/search?page={page++}&count=10"); - if (!responseJson.HasValue) throw new Exception("Failed to get robots!"); - var results = responseJson.Value.GetProperty("results"); - if (results.GetArrayLength() == 0) break; - foreach (var result in results.EnumerateArray()) - { - var robot = result.GetProperty("robot"); - var dirPath = Path.Combine("robots", robot.GetProperty("id").GetString()!); - Directory.CreateDirectory(dirPath); - File.WriteAllText(Path.Combine(dirPath, "metadata.json"), robot.GetRawText()); - responseJson = await Get(factoryUrl + "/v1/foundry/vehicles/" + robot.GetProperty("id")); - if (!responseJson.HasValue) throw new Exception($"Could not get bot {robot.GetProperty("name")}"); - File.WriteAllText(Path.Combine(dirPath, "robotData.json"), responseJson.Value.GetRawText()); - Console.WriteLine($"Saved {robot.GetProperty("name")} by {robot.GetProperty("creatorName")}"); - Thread.Sleep(100); - } - Thread.Sleep(1000); + var robot = result; + var dirPath = Path.Combine("my_robots", robot.GetProperty("id").GetString()!); + Directory.CreateDirectory(dirPath); + File.WriteAllText(Path.Combine(dirPath, "metadata.json"), robot.GetRawText()); + responseJson = await Get(factoryUrl + "/v1/foundry/vehicles/" + robot.GetProperty("id")); + if (!responseJson.HasValue) throw new Exception($"Could not get bot {robot.GetProperty("name")}"); + File.WriteAllText(Path.Combine(dirPath, "robotData.json"), responseJson.Value.GetRawText()); + Console.WriteLine($"Saved {robot.GetProperty("name")} by {robot.GetProperty("creatorName")}"); + Thread.Sleep(100); } // TODO: Get local saves // TODO: Save robot images +// TODO: Chart of amount of bots on CRF by date & amount of starter bots +*/ \ No newline at end of file