Download dev CRF2 with error handling
It frequently returns an empty 500
This commit is contained in:
parent
88d3ede9ff
commit
8d2a431a2a
2 changed files with 39 additions and 24 deletions
|
@ -7,7 +7,7 @@ public static class DownloadImages
|
|||
{
|
||||
public static async Task DoIt(HttpClient client)
|
||||
{
|
||||
foreach (var dir in Directory.EnumerateDirectories("my_robots"))
|
||||
foreach (var dir in Directory.EnumerateDirectories("robots"))
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(Path.Combine(dir, "metadata.json"));
|
||||
var doc = JsonDocument.Parse(json);
|
||||
|
|
|
@ -10,9 +10,7 @@ using RC2BotArchiver;
|
|||
|
||||
Console.WriteLine("Hello, World!");
|
||||
|
||||
FixCRF2Archive.FixIt();
|
||||
|
||||
/*var client = new HttpClient();
|
||||
var client = new HttpClient();
|
||||
|
||||
async Task<JsonElement?> GetResponse(HttpResponseMessage result, string url)
|
||||
{
|
||||
|
@ -24,6 +22,8 @@ async Task<JsonElement?> GetResponse(HttpResponseMessage result, string url)
|
|||
}
|
||||
|
||||
Console.WriteLine("Status: " + result.StatusCode);
|
||||
if (result.StatusCode != HttpStatusCode.NoContent)
|
||||
Console.WriteLine("Error: " + await result.Content.ReadAsStringAsync());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -66,33 +66,48 @@ var jwt = responseJson.Value.GetProperty("Token");
|
|||
payload = new ExpandoObject();
|
||||
payload.Token = responseJson.Value.GetProperty("Token");
|
||||
payload.ClientVersion = "100.0";
|
||||
responseJson = await Post("https://progression.production.robocraft2.com/login/fj", payload);
|
||||
responseJson = await Post("https://progression.development.robocraft.org/login/fj", payload);
|
||||
var progressToken = responseJson.Value.GetProperty("token").GetString();
|
||||
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", progressToken);
|
||||
|
||||
const string factoryUrl = "https://factory.production.robocraft2.com";
|
||||
;
|
||||
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())
|
||||
const string factoryUrl = "https://factory.development.robocraft.org";
|
||||
|
||||
int page = 52;
|
||||
while (true)
|
||||
{
|
||||
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());
|
||||
var data = responseJson.Value.GetProperty("data").GetString()!;
|
||||
File.WriteAllBytes(Path.Combine(dirPath, "robot.rc2"), Convert.FromBase64String(data));
|
||||
Console.WriteLine($"Saved {robot.GetProperty("name")} by {robot.GetProperty("creatorName")}");
|
||||
Thread.Sleep(100);
|
||||
try
|
||||
{
|
||||
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);
|
||||
page++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
if (e.Message != "Failed to get robots!")
|
||||
break;
|
||||
Thread.Sleep(10000);
|
||||
}
|
||||
}
|
||||
|
||||
await DownloadImages.DoIt(client);
|
||||
|
||||
// TODO: Chart of amount of bots on CRF by date & amount of starter bots
|
||||
*/
|
Loading…
Reference in a new issue