From 0987c7498b9448b3419ac10ce205c06a71c2623e Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Sat, 11 Oct 2025 16:19:01 +0200 Subject: [PATCH] Added update all UserInfo mode --- OF DL/Entities/Config.cs | 1 + OF DL/Helpers/DBHelper.cs | 19 ++++++++++++ OF DL/Program.cs | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/OF DL/Entities/Config.cs b/OF DL/Entities/Config.cs index 1be7d72..0418043 100644 --- a/OF DL/Entities/Config.cs +++ b/OF DL/Entities/Config.cs @@ -112,6 +112,7 @@ namespace OF_DL.Entities public string[] NonInteractiveSpecificLists { get; set; } = []; public bool OutputBlockedUsers { get; set; } + public bool UpdateAllUserInfo { get; set; } } public class CreatorConfig : IFileNameFormatConfig diff --git a/OF DL/Helpers/DBHelper.cs b/OF DL/Helpers/DBHelper.cs index 171c409..7516c2d 100644 --- a/OF DL/Helpers/DBHelper.cs +++ b/OF DL/Helpers/DBHelper.cs @@ -235,6 +235,25 @@ namespace OF_DL.Helpers } } + public async Task> GetUsers() + { + SqliteConnection connection = await GetAndOpenConnectionAsync($"Data Source={Directory.GetCurrentDirectory()}/users.db"); + using SqliteCommand cmd = new("SELECT user_id, username FROM users", connection); + using SqliteDataReader reader = cmd.ExecuteReader(); + + Dictionary result = new(StringComparer.OrdinalIgnoreCase); + + while (reader.Read()) + { + long userId = reader.GetInt64(0); + string username = reader.GetString(1); + + result[username] = userId; + } + + return result; + } + public async Task UpdateUserInfo(User? user) { if (user is null) diff --git a/OF DL/Program.cs b/OF DL/Program.cs index bcbab05..bcc0207 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -12,6 +12,7 @@ using OF_DL.Enumerations; using OF_DL.Enumurations; using OF_DL.Helpers; using Serilog; +using Serilog.Context; using Serilog.Core; using Serilog.Events; using Spectre.Console; @@ -531,6 +532,14 @@ public class Program config.OutputBlockedUsers = true; } + const string UPDATE_ALL_USER_INFO_ARG = "--update-userinfo"; + + if (args.Any(a => UPDATE_ALL_USER_INFO_ARG.Equals(a, StringComparison.OrdinalIgnoreCase))) + { + config.NonInteractiveMode = true; + config.UpdateAllUserInfo = true; + } + Log.Debug("Additional arguments:"); foreach (string argument in args) { @@ -872,6 +881,11 @@ public class Program await DownloadBlockedOrExpiredUsers(apiHelper, config); return; } + else if (config.UpdateAllUserInfo) + { + await UpdateAlluserInfo(apiHelper, config); + return; + } await DownloadAllData(apiHelper, auth, config); } @@ -938,6 +952,56 @@ public class Program } } + private static async Task UpdateAlluserInfo(APIHelper m_ApiHelper, Entities.Config Config) + { + DBHelper dbHelper = new(Config); + + await dbHelper.CreateUsersDB([]); + + Dictionary users = await dbHelper.GetUsers(); + + Console.WriteLine(); + Log.Information("Updating User Info for '{UserCount}' users", users.Count); + AnsiConsole.Markup($"[green]Updating User Info for '{users.Count}' users\n[/]"); + + Console.WriteLine(); + await AnsiConsole.Progress() + .Columns(new ProgressBarColumn(), new PercentageColumn(), new TaskDescriptionColumn()) + .StartAsync(RunUpdateAsync); + + async Task RunUpdateAsync(ProgressContext context) + { + ProgressTask updateTask = context.AddTask($"Updating User Info for '{users.Count}' users", true, users.Count); + + foreach ((string username, long userId) in users) + { + updateTask.Description = $"Updating '{username}'"; + + using (LogContext.PushProperty("Username", username)) + using (LogContext.PushProperty("UserId", userId)) + { + try + { + Log.Information("Updating User Info for for: {Username:l}"); + User? user_info = await m_ApiHelper.GetUserInfo($"/users/{username}"); + await dbHelper.UpdateUserInfo(user_info); + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to update User Info for: {Username:l}"); + AnsiConsole.Markup($"[red]Failed to update User Info for '{username}'\n[/]"); + } + finally + { + updateTask.Increment(1); + } + } + } + + updateTask.StopTask(); + } + } + private static async Task DownloadAllData(APIHelper m_ApiHelper, Auth Auth, Entities.Config Config) { DBHelper dBHelper = new DBHelper(Config);