From 1d587eeccb6d4eaf3ee8b2744b68084ce2e6471c Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Wed, 3 Sep 2025 20:15:17 +0200 Subject: [PATCH] Extended "output blocked" to also include expired in separate file --- OF DL/Helpers/APIHelper.cs | 20 ++++++++++------ OF DL/Program.cs | 47 +++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/OF DL/Helpers/APIHelper.cs b/OF DL/Helpers/APIHelper.cs index d9fea2a..6449549 100644 --- a/OF DL/Helpers/APIHelper.cs +++ b/OF DL/Helpers/APIHelper.cs @@ -414,7 +414,7 @@ public class APIHelper : IAPIHelper return await GetAllSubscriptions(getParams, endpoint, includeRestricted, config); } - public async Task?> GetBlockedUsers(string endpoint, IDownloadConfig config, StatusContext ctx) + public async Task?> GetUsersWithProgress(string typeDisplay, string endpoint, StatusContext ctx, string? typeParam, bool offsetByCount) { int limit = 50; int offset = 0; @@ -427,11 +427,14 @@ public class APIHelper : IAPIHelper ["offset"] = offset.ToString() }; + if (!string.IsNullOrWhiteSpace(typeParam)) + getParams["type"] = typeParam; + try { Dictionary users = []; - Log.Debug("Calling GetBlockedUsers"); + Log.Debug("Calling GetUsersWithProgress"); bool isLastLoop = false; while (true) @@ -443,10 +446,6 @@ public class APIHelper : IAPIHelper Subscriptions? subscriptions = JsonConvert.DeserializeObject(body, m_JsonSerializerSettings); - ctx.Status($"[red]Getting Blocked Users\n[/] [red]Found {users.Count}[/]"); - ctx.Spinner(Spinner.Known.Dots); - ctx.SpinnerStyle(Style.Parse("blue")); - if (subscriptions?.list is null) break; @@ -462,13 +461,20 @@ public class APIHelper : IAPIHelper users.Add(item.username, item.id); } + ctx.Status($"[red]Getting {typeDisplay} Users\n[/] [red]Found {users.Count}[/]"); + ctx.Spinner(Spinner.Known.Dots); + ctx.SpinnerStyle(Style.Parse("blue")); + if (isLastLoop) break; if (!subscriptions.hasMore || subscriptions.list.Count == 0) isLastLoop = true; - offset += subscriptions.list.Count; + offset += offsetByCount + ? subscriptions.list.Count + : limit; + getParams["offset"] = offset.ToString(); } diff --git a/OF DL/Program.cs b/OF DL/Program.cs index 50bce1c..58ddc88 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -857,7 +857,7 @@ public class Program { if (config.OutputBlockedUsers) { - await DownloadBlockedUsers(apiHelper, config); + await DownloadBlockedOrExpiredUsers(apiHelper, config); return; } @@ -887,28 +887,37 @@ public class Program } } - private static async Task DownloadBlockedUsers(APIHelper m_ApiHelper, Entities.Config Config) + private static async Task DownloadBlockedOrExpiredUsers(APIHelper m_ApiHelper, Entities.Config Config) { - const string OUTPUT_FILE = "blocked-users.json"; + const string OUTPUT_FILE_BLOCKED = "blocked-users.json"; + const string OUTPUT_FILE_EXPIRED = "expired-users.json"; - Dictionary? blockedUsers = null; + await GetUsers("Blocked", "/users/blocked", OUTPUT_FILE_BLOCKED); + await GetUsers("Expired", "/subscriptions/subscribes", OUTPUT_FILE_EXPIRED, typeParam: "expired", offsetByCount: false); - await AnsiConsole - .Status() - .StartAsync("[red]Getting Blocked Users[/]", async ctx => + async Task GetUsers(string typeDisplay, string uri, string outputFile, string? typeParam = null, bool offsetByCount = true) + { + Dictionary? users = null; + + await AnsiConsole + .Status() + .StartAsync($"[red]Getting {typeDisplay} Users[/]", async ctx => + { + users = await m_ApiHelper.GetUsersWithProgress(typeDisplay, uri, ctx, typeParam, offsetByCount); + }); + + Console.WriteLine(); + + if (users is null || users.Count == 0) { - blockedUsers = await m_ApiHelper.GetBlockedUsers("/users/blocked", Config, ctx); - }); - - if (blockedUsers is null || blockedUsers.Count == 0) - { - AnsiConsole.Markup($"[green]No Blocked Users found.\n[/]"); - } - else - { - AnsiConsole.Markup($"[green]Found {blockedUsers.Count} Blocked Users, saving to '{OUTPUT_FILE}'\n[/]"); - string json = JsonConvert.SerializeObject(blockedUsers, Formatting.Indented); - await File.WriteAllTextAsync(OUTPUT_FILE, json); + AnsiConsole.Markup($"[green]No {typeDisplay} Users found.\n[/]"); + } + else + { + AnsiConsole.Markup($"[green]Found {users.Count} {typeDisplay} Users, saving to '{outputFile}'\n[/]"); + string json = JsonConvert.SerializeObject(users, Formatting.Indented); + await File.WriteAllTextAsync(outputFile, json); + } } }