RC2BotArchiver/RC2BotArchiver/DownloadImages.cs
NorbiPeti 8d2a431a2a Download dev CRF2 with error handling
It frequently returns an empty 500
2025-01-09 01:28:38 +01:00

23 lines
No EOL
883 B
C#

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.jpeg"));
await stream.CopyToAsync(fs);
Console.WriteLine($"Saved image for {doc.RootElement.GetProperty("name")}");
}
}
}