forked from sim0n00ps/OF-DL
Added update all UserInfo mode
This commit is contained in:
parent
1565f668ae
commit
31921115ec
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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;
|
||||
@ -539,6 +540,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)
|
||||
{
|
||||
@ -949,6 +958,11 @@ public class Program
|
||||
await DownloadBlockedOrExpiredUsers(apiHelper, config);
|
||||
return;
|
||||
}
|
||||
else if (config.UpdateAllUserInfo)
|
||||
{
|
||||
await UpdateAlluserInfo(apiHelper, config);
|
||||
return;
|
||||
}
|
||||
|
||||
await DownloadAllData(apiHelper, auth, config);
|
||||
}
|
||||
@ -1015,6 +1029,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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user