Robot image download
This commit is contained in:
parent
189d784c57
commit
2a5d73608d
2 changed files with 44 additions and 21 deletions
23
RC2BotArchiver/DownloadImages.cs
Normal file
23
RC2BotArchiver/DownloadImages.cs
Normal 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")}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,11 +6,15 @@ using System.Net;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using RC2BotArchiver;
|
||||||
|
|
||||||
Console.WriteLine("Hello, World!");
|
Console.WriteLine("Hello, World!");
|
||||||
|
|
||||||
var client = new HttpClient();
|
var client = new HttpClient();
|
||||||
|
|
||||||
|
await DownloadImages.DoIt(client);
|
||||||
|
|
||||||
|
/*
|
||||||
async Task<JsonElement?> GetResponse(HttpResponseMessage result, string url)
|
async Task<JsonElement?> GetResponse(HttpResponseMessage result, string url)
|
||||||
{
|
{
|
||||||
if (result.StatusCode == HttpStatusCode.OK)
|
if (result.StatusCode == HttpStatusCode.OK)
|
||||||
|
@ -69,29 +73,25 @@ var progressToken = responseJson.Value.GetProperty("token").GetString();
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", progressToken);
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", progressToken);
|
||||||
|
|
||||||
const string factoryUrl = "https://factory.production.robocraft2.com";
|
const string factoryUrl = "https://factory.production.robocraft2.com";
|
||||||
|
;
|
||||||
int page = 408;
|
Console.WriteLine($"Getting owned robots");
|
||||||
while (true)
|
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}");
|
var robot = result;
|
||||||
responseJson = await Get(factoryUrl + $"/v1/foundry/search?page={page++}&count=10");
|
var dirPath = Path.Combine("my_robots", robot.GetProperty("id").GetString()!);
|
||||||
if (!responseJson.HasValue) throw new Exception("Failed to get robots!");
|
Directory.CreateDirectory(dirPath);
|
||||||
var results = responseJson.Value.GetProperty("results");
|
File.WriteAllText(Path.Combine(dirPath, "metadata.json"), robot.GetRawText());
|
||||||
if (results.GetArrayLength() == 0) break;
|
responseJson = await Get(factoryUrl + "/v1/foundry/vehicles/" + robot.GetProperty("id"));
|
||||||
foreach (var result in results.EnumerateArray())
|
if (!responseJson.HasValue) throw new Exception($"Could not get bot {robot.GetProperty("name")}");
|
||||||
{
|
File.WriteAllText(Path.Combine(dirPath, "robotData.json"), responseJson.Value.GetRawText());
|
||||||
var robot = result.GetProperty("robot");
|
Console.WriteLine($"Saved {robot.GetProperty("name")} by {robot.GetProperty("creatorName")}");
|
||||||
var dirPath = Path.Combine("robots", robot.GetProperty("id").GetString()!);
|
Thread.Sleep(100);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Get local saves
|
// TODO: Get local saves
|
||||||
// TODO: Save robot images
|
// TODO: Save robot images
|
||||||
|
// TODO: Chart of amount of bots on CRF by date & amount of starter bots
|
||||||
|
*/
|
Loading…
Reference in a new issue