Compare commits

...

2 Commits

2 changed files with 101 additions and 17 deletions

View File

@ -414,17 +414,85 @@ public class APIHelper : IAPIHelper
return await GetAllSubscriptions(getParams, endpoint, includeRestricted, config);
}
public async Task<Dictionary<string, int>?> GetBlockedUsers(string endpoint, IDownloadConfig config)
public async Task<Dictionary<string, int>?> GetUsersWithProgress(string typeDisplay, string endpoint, StatusContext ctx, string? typeParam, bool offsetByCount)
{
int limit = 50;
int offset = 0;
bool includeRestricted = true;
Dictionary<string, string> getParams = new()
{
{ "type", "expired" },
{ "format", "infinite"}
["format"] = "infinite",
["limit"] = limit.ToString(),
["offset"] = offset.ToString()
};
Log.Debug("Calling GetBlockedUsers");
if (!string.IsNullOrWhiteSpace(typeParam))
getParams["type"] = typeParam;
return await GetAllSubscriptions(getParams, endpoint, true, config);
try
{
Dictionary<string, int> users = [];
Log.Debug("Calling GetUsersWithProgress");
bool isLastLoop = false;
while (true)
{
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
if (string.IsNullOrWhiteSpace(body))
break;
Subscriptions? subscriptions = JsonConvert.DeserializeObject<Subscriptions>(body, m_JsonSerializerSettings);
if (subscriptions?.list is null)
break;
foreach (Subscriptions.List item in subscriptions.list)
{
if (users.ContainsKey(item.username))
continue;
bool isRestricted = item.isRestricted ?? false;
bool isRestrictedButAllowed = isRestricted && includeRestricted;
if (!isRestricted || isRestrictedButAllowed)
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 += offsetByCount
? subscriptions.list.Count
: limit;
getParams["offset"] = offset.ToString();
}
return users;
}
catch (Exception ex)
{
Console.WriteLine("Exception caught: {0}\n\nStackTrace: {1}", ex.Message, ex.StackTrace);
Log.Error("Exception caught: {0}\n\nStackTrace: {1}", ex.Message, ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine("\nInner Exception:");
Console.WriteLine("Exception caught: {0}\n\nStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace);
Log.Error("Inner Exception: {0}\n\nStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace);
}
}
return null;
}
public async Task<Dictionary<string, int>> GetLists(string endpoint, IDownloadConfig config)

View File

@ -857,7 +857,7 @@ public class Program
{
if (config.OutputBlockedUsers)
{
await DownloadBlockedUsers(apiHelper, config);
await DownloadBlockedOrExpiredUsers(apiHelper, config);
return;
}
@ -887,21 +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<string, int>? blockedUsers = await m_ApiHelper.GetBlockedUsers("/users/blocked", Config);
await GetUsers("Blocked", "/users/blocked", OUTPUT_FILE_BLOCKED);
await GetUsers("Expired", "/subscriptions/subscribes", OUTPUT_FILE_EXPIRED, typeParam: "expired", offsetByCount: false);
if (blockedUsers is null || blockedUsers.Count == 0)
async Task GetUsers(string typeDisplay, string uri, string outputFile, string? typeParam = null, bool offsetByCount = true)
{
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);
Dictionary<string, int>? 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)
{
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);
}
}
}