diff --git a/OF DL/Helpers/APIHelper.cs b/OF DL/Helpers/APIHelper.cs index ceeb7b6..80e67bd 100644 --- a/OF DL/Helpers/APIHelper.cs +++ b/OF DL/Helpers/APIHelper.cs @@ -553,6 +553,66 @@ public class APIHelper : IAPIHelper } + public async Task?> GetUsersFromList(string endpoint, bool includeRestricted, IDownloadConfig config) + { + var model = new { list = new[] { new { id = int.MaxValue, username = string.Empty, isRestricted = false, isBlocked = false } }, nextOffset = 0, hasMore = false }; + + Log.Debug($"Calling GetUsersFromList - {endpoint}"); + + int limit = 50; + int offset = 0; + + Dictionary getParams = new() + { + { "offset", offset.ToString() }, + { "limit", limit.ToString() }, + { "format", "infinite" } + }; + + try + { + Dictionary users = []; + + while (true) + { + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config)); + + if (string.IsNullOrWhiteSpace(body)) + break; + + var data = JsonConvert.DeserializeAnonymousType(body, model, m_JsonSerializerSettings); + + if (data is null) + break; + + foreach (var item in data.list) + { + if (users.ContainsKey(item.username)) + continue; + + bool isRestricted = item.isRestricted; + bool isRestrictedButAllowed = isRestricted && includeRestricted; + + if (!isRestricted || isRestrictedButAllowed) + users.Add(item.username, item.id); + } + + if (!data.hasMore) + break; + + + offset += data.nextOffset; + getParams["offset"] = offset.ToString(); + } + + return users; + } + catch (Exception ex) + { + throw; + } + } + public async Task> GetMedia(MediaType mediatype, string endpoint, string? username, diff --git a/OF DL/Helpers/Interfaces/IAPIHelper.cs b/OF DL/Helpers/Interfaces/IAPIHelper.cs index 02c32a8..c76201c 100644 --- a/OF DL/Helpers/Interfaces/IAPIHelper.cs +++ b/OF DL/Helpers/Interfaces/IAPIHelper.cs @@ -18,6 +18,7 @@ namespace OF_DL.Helpers Task GetDRMMPDPSSH(string mpdUrl, string policy, string signature, string kvp); Task> GetLists(string endpoint, IDownloadConfig config); Task> GetListUsers(string endpoint, IDownloadConfig config); + Task?> GetUsersFromList(string endpoint, bool includeRestricted, IDownloadConfig config); Task> GetMedia(MediaType mediatype, string endpoint, string? username, string folder, IDownloadConfig config, List paid_post_ids); Task GetPaidPosts(string endpoint, string folder, string username, IDownloadConfig config, List paid_post_ids, StatusContext ctx); Task GetPosts(string endpoint, string folder, IDownloadConfig config, List paid_post_ids, StatusContext ctx); diff --git a/OF DL/Program.cs b/OF DL/Program.cs index 4d05d87..c88fedc 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -1,7 +1,9 @@ +using Akka.Configuration; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OF_DL.Entities; using OF_DL.Entities.Archived; +using OF_DL.Entities.Chats; using OF_DL.Entities.Messages; using OF_DL.Entities.Post; using OF_DL.Entities.Purchased; @@ -13,16 +15,12 @@ using Serilog; using Serilog.Core; using Serilog.Events; using Spectre.Console; -using System.IO; +using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; +using System.Text; using System.Text.RegularExpressions; using static OF_DL.Entities.Messages.Messages; -using Akka.Configuration; -using System.Text; -using static Akka.Actor.ProviderSelection; -using System.Diagnostics; -using OF_DL.Entities.Chats; namespace OF_DL; @@ -965,28 +963,28 @@ public class Program } } - if (users.Count <= 0) - throw new InvalidOperationException("No users found!"); - - await dBHelper.CreateUsersDB(users); - KeyValuePair> hasSelectedUsersKVP; - if(Config.NonInteractiveMode && Config.NonInteractiveModePurchasedTab) - { - hasSelectedUsersKVP = new KeyValuePair>(true, new Dictionary { { "PurchasedTab", 0 } }); - } + KeyValuePair> hasSelectedUsersKVP = new(false, []); + if (Config.NonInteractiveMode && Config.NonInteractiveModePurchasedTab) + { + hasSelectedUsersKVP = new KeyValuePair>(true, new Dictionary { { "PurchasedTab", 0 } }); + } else if (Config.NonInteractiveMode && Config.NonInteractiveSpecificLists is not null && Config.NonInteractiveSpecificLists.Length > 0) { - HashSet listUsernames = []; + Dictionary usersFromLists = new(StringComparer.OrdinalIgnoreCase); + foreach (string listName in Config.NonInteractiveSpecificLists) { if (!lists.TryGetValue(listName, out int listId)) continue; - List usernames = await m_ApiHelper.GetListUsers($"/lists/{listId}/users", Config); - foreach (string user in usernames) - listUsernames.Add(user); + AnsiConsole.Markup($"[green]Getting Users from list '{listName}' (Include Restricted: {Config.IncludeRestrictedSubscriptions})\n[/]"); + Dictionary list = await m_ApiHelper.GetUsersFromList($"/lists/{listId}/users", config.IncludeRestrictedSubscriptions, Config); + + foreach ((string username, int id) in list) + usersFromLists.TryAdd(username, id); } - users = users.Where(x => listUsernames.Contains(x.Key)).Distinct().ToDictionary(x => x.Key, x => x.Value); + + users = usersFromLists; hasSelectedUsersKVP = new KeyValuePair>(true, users); } else if (Config.NonInteractiveMode && Config.NonInteractiveSpecificUsers is not null && Config.NonInteractiveSpecificUsers.Length > 0) @@ -996,17 +994,23 @@ public class Program hasSelectedUsersKVP = new KeyValuePair>(true, users); } else if (Config.NonInteractiveMode && string.IsNullOrEmpty(Config.NonInteractiveModeListName)) - { - hasSelectedUsersKVP = new KeyValuePair>(true, users); - } - else if (Config.NonInteractiveMode && !string.IsNullOrEmpty(Config.NonInteractiveModeListName)) - { - var listId = lists[Config.NonInteractiveModeListName]; - var listUsernames = await m_ApiHelper.GetListUsers($"/lists/{listId}/users", Config) ?? []; - var selectedUsers = users.Where(x => listUsernames.Contains(x.Key)).Distinct().ToDictionary(x => x.Key, x => x.Value); - hasSelectedUsersKVP = new KeyValuePair>(true, selectedUsers); - } - else + { + hasSelectedUsersKVP = new KeyValuePair>(true, users); + } + else if (Config.NonInteractiveMode && !string.IsNullOrEmpty(Config.NonInteractiveModeListName)) + { + var listId = lists[Config.NonInteractiveModeListName]; + AnsiConsole.Markup($"[green]Getting Users from list '{Config.NonInteractiveModeListName}' (Include Restricted: {Config.IncludeRestrictedSubscriptions})\n[/]"); + users = await m_ApiHelper.GetUsersFromList($"/lists/{listId}/users", config.IncludeRestrictedSubscriptions, Config); + hasSelectedUsersKVP = new KeyValuePair>(true, users); + } + + if (users.Count <= 0) + throw new InvalidOperationException("No users found!"); + + await dBHelper.CreateUsersDB(users); + + if (hasSelectedUsersKVP.Key == false) { var userSelectionResult = await HandleUserSelection(m_ApiHelper, Config, users, lists);