Skip to content

Commit

Permalink
Fixed sorting on inaccessible or deleted items with missing data
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Apr 6, 2024
1 parent 03f980b commit b44d516
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,38 +345,36 @@ private static async Task CalculateSize()
if (response.TryGetProperty("publishedfiledetails", out var fileInfo))
{
var totalSize = 0L;
var arrayItems = fileInfo.EnumerateArray();
var currentItem = 1;
IEnumerable<JsonElement>? sortedItems = null;
IEnumerable<JsonElement> arrayItems = fileInfo.EnumerateArray();

arrayItems = arrayItems.Where(itemInfo =>
{
// Remove all unreachable or deleted items.
return itemInfo.TryGetProperty("result", out var result) && result.ToString() == "1";
});

if (order == "ASC")
{
// Ascending sorting.
sortedItems = arrayItems.OrderBy(itemInfo => long.Parse(itemInfo.GetProperty("file_size").ToString()));
arrayItems = arrayItems.OrderBy(itemInfo => long.Parse(itemInfo.GetProperty("file_size").ToString()));
}
else if (order == "DESC")
{
// Descending sorting.
sortedItems = arrayItems.OrderByDescending(itemInfo => long.Parse(itemInfo.GetProperty("file_size").ToString()));
arrayItems = arrayItems.OrderByDescending(itemInfo => long.Parse(itemInfo.GetProperty("file_size").ToString()));
}
foreach (var itemInfo in (sortedItems ?? arrayItems))

foreach (var itemInfo in arrayItems)
{
// Show details of each item.
var itemSize = long.Parse(itemInfo.GetProperty("file_size").ToString());
var itemTitle = itemInfo.GetProperty("title").ToString();
var consoleOutput = $"({currentItem}/{index}) {itemInfo.GetProperty("publishedfileid"),-10} :";

if (itemInfo.TryGetProperty("title", out var title))
{
var itemSize = long.Parse(itemInfo.GetProperty("file_size").ToString());

ConsoleLog($"{consoleOutput} {title} [{BytesToString(itemSize)}]");

totalSize += itemSize;
}
else
{
ConsoleLog($"{consoleOutput} ERROR -> OBJECT IS HIDDEN OR UNAVAILABLE");
}
ConsoleLog($"{consoleOutput} {itemTitle} [{BytesToString(itemSize)}]");

totalSize += itemSize;
currentItem++;
}

Expand Down

0 comments on commit b44d516

Please sign in to comment.