Skip to content

Commit

Permalink
Make the code more readable + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Jun 22, 2021
1 parent f958d43 commit 732de5b
Showing 1 changed file with 43 additions and 55 deletions.
98 changes: 43 additions & 55 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
Expand All @@ -19,7 +19,7 @@ class Program
/// <summary>
/// A simple method to display messages in the console and save them in a text file.
/// </summary>
private static void ConsoleLog(string text = "", bool noNewline = false)
static void ConsoleLog(string text = "", bool noNewline = false)
{
text = text.Replace("\n", "");

Expand All @@ -35,7 +35,7 @@ private static void ConsoleLog(string text = "", bool noNewline = false)
/// <summary>
/// Main function of the program which retrieves the identifier and launches the functions to calculate the size.
/// </summary>
static async Task Main(string[] args)
static async Task Main()
{
// We ask to enter one or more identifiers.
ConsoleLog("-----------------------------------------");
Expand Down Expand Up @@ -142,32 +142,27 @@ static async Task RequestSteamAPI(string requestedID)

if (request.IsSuccessStatusCode && request.Content != null)
{
var jsonText = await request.Content.ReadAsStringAsync();
// Then we iterate through the whole JSON file to retrieve the identifiers.
var document = await JsonDocument.ParseAsync(await request.Content.ReadAsStreamAsync());
var details = document.RootElement.GetProperty("response").GetProperty("collectiondetails")[0];

using (var document = JsonDocument.Parse(jsonText))
if (details.TryGetProperty("children", out var items))
{
// Then we iterate through the whole JSON file to retrieve the identifiers.
var root = document.RootElement;
var details = root.GetProperty("response").GetProperty("collectiondetails")[0];
ConsoleLog($"The Steam API reports that the object is a Workshop collection containing {items.GetArrayLength()} items.");
ConsoleLog("Beginning of calculation...");

if (details.TryGetProperty("children", out var items))
foreach (var item in items.EnumerateArray())
{
ConsoleLog($"The Steam API reports that the object is a Workshop collection containing {items.GetArrayLength()} items.");
ConsoleLog("Beginning of calculation...");

foreach (var item in items.EnumerateArray())
if (item.TryGetProperty("publishedfileid", out var identifier))
{
if (item.TryGetProperty("publishedfileid", out var identifier))
{
retrievedIDs.Add(identifier.ToString());
}
retrievedIDs.Add(identifier.ToString());
}
}
else
{
ConsoleLog("The Steam API reports that the object is a simple addon (in some cases, the identifier you entered may be invalid).");
retrievedIDs.Add(requestedID);
}
}
else
{
ConsoleLog("The Steam API reports that the object is a simple addon (in some cases, the identifier you entered may be invalid).");
retrievedIDs.Add(requestedID);
}
}
else
Expand All @@ -185,7 +180,7 @@ static async Task RequestSteamAPI(string requestedID)
/// Transforms bytes into a human readable string.
/// https://github.com/Facepunch/garrysmod/blob/87e75a6803905bbd1189f7b6f48680dc5b3beb48/garrysmod/lua/includes/extensions/string.lua#L257-L268 (Garry's Code ™)
/// </summary>
private static string BytesToString(ulong size)
static string BytesToString(ulong size)
{
if (size <= 0)
return "0 Byte";
Expand Down Expand Up @@ -228,49 +223,42 @@ static async Task CalculateSize()

if (request.IsSuccessStatusCode && request.Content != null)
{
var jsonText = await request.Content.ReadAsStringAsync();
var document = await JsonDocument.ParseAsync(await request.Content.ReadAsStreamAsync());
var details = document.RootElement.GetProperty("response");

using (var document = JsonDocument.Parse(jsonText))
// Some of the objects previously filled in may not exist so we check that.
if (details.TryGetProperty("publishedfiledetails", out var items))
{
var root = document.RootElement;
var details = root.GetProperty("response");
var count = 1;
var total = 0UL;

// Some of the objects previously filled in may not exist so we check that.
if (details.TryGetProperty("publishedfiledetails", out var items))
foreach (var item in items.EnumerateArray())
{
var count = 1;
var total = 0UL;
var identifier = item.GetProperty("publishedfileid");
var message = $"({count}/{index}) {identifier,-10} :";

foreach (var item in items.EnumerateArray())
if (item.TryGetProperty("title", out var title))
{
var identifier = item.GetProperty("publishedfileid");
var message = $"({count}/{index}) {identifier, -10} :";

if (item.TryGetProperty("title", out var title))
{
var size = 0UL;

UInt64.TryParse(item.GetProperty("file_size").ToString(), out size);
var size = ulong.Parse(item.GetProperty("file_size").ToString());

ConsoleLog($"{message} {title} [{BytesToString(size)}]");
ConsoleLog($"{message} {title} [{BytesToString(size)}]");

total += size;
}
else
{
ConsoleLog($"{message} ERROR -> OBJECT IS HIDDEN OR UNAVAILABLE");
}

count++;
total += size;
}
else
{
ConsoleLog($"{message} ERROR -> OBJECT IS HIDDEN OR UNAVAILABLE");
}

ConsoleLog($"Total size: {BytesToString(total)}.");
ConsoleLog();
}
else
{
ConsoleLog("It seems that the object is invalid or simply temporarily unavailable.");
count++;
}

ConsoleLog($"Total size: {BytesToString(total)}.");
ConsoleLog();
}
else
{
ConsoleLog("It seems that the object is invalid or simply temporarily unavailable.");
}
}
else
Expand Down

0 comments on commit 732de5b

Please sign in to comment.