Added update all UserInfo mode

This commit is contained in:
Casper Sparre 2025-10-11 16:19:01 +02:00
parent 811cafe5ca
commit 0987c7498b
3 changed files with 84 additions and 0 deletions

View File

@ -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

View File

@ -235,6 +235,25 @@ namespace OF_DL.Helpers
}
}
public async Task<Dictionary<string, long>> 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<string, long> 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)

View File

@ -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<string, long> 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);