Robot image download

This commit is contained in:
Norbi Peti 2024-05-14 19:00:53 +02:00
parent 189d784c57
commit 2a5d73608d
2 changed files with 44 additions and 21 deletions

View file

@ -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")}");
}
}
}

View file

@ -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<JsonElement?> 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
*/