From 233de7c9e5350e71cb53cea0596c213c8a56277c Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Sat, 8 Mar 2025 14:56:01 +0100 Subject: [PATCH] Extended command line args for NonInteractive --- OF DL/Entities/Config.cs | 3 ++ OF DL/Program.cs | 66 +++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/OF DL/Entities/Config.cs b/OF DL/Entities/Config.cs index 0d05fbf..4beba86 100644 --- a/OF DL/Entities/Config.cs +++ b/OF DL/Entities/Config.cs @@ -103,6 +103,9 @@ namespace OF_DL.Entities [JsonConverter(typeof(StringEnumConverter))] public VideoResolution DownloadVideoResolution { get; set; } = VideoResolution.source; + + public string[] NonInteractiveSpecificUsers { get; set; } = []; + public string[] NonInteractiveSpecificLists { get; set; } = []; } public class CreatorConfig : IFileNameFormatConfig diff --git a/OF DL/Program.cs b/OF DL/Program.cs index b28092c..beae5a9 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -476,15 +476,44 @@ public class Program if (args is not null && args.Length > 0) { - const string NON_INTERACTIVE_ARG = "--non-interactive"; + const string NON_INTERACTIVE_ARG = "--non-interactive"; + const string SPECIFIC_LISTS_ARG = "--specific-lists"; + const string SPECIFIC_USERS_ARG = "--specific-users"; - if (args.Any(a => NON_INTERACTIVE_ARG.Equals(NON_INTERACTIVE_ARG, StringComparison.OrdinalIgnoreCase))) - { - cliNonInteractive = true; - Log.Debug("NonInteractiveMode set via command line"); - } + if (args.Any(a => NON_INTERACTIVE_ARG.Equals(NON_INTERACTIVE_ARG, StringComparison.OrdinalIgnoreCase))) + { + AnsiConsole.Markup($"[grey]Non-Interactive Mode enabled through command-line argument![/]\n"); - Log.Debug("Additional arguments:"); + config.NonInteractiveMode = true; + + int indexOfSpecificListsArg = Array.FindIndex(args, a => a.Contains(SPECIFIC_LISTS_ARG, StringComparison.OrdinalIgnoreCase)); + int indexOfSpecificUsersArg = Array.FindIndex(args, a => a.Contains(SPECIFIC_USERS_ARG, StringComparison.OrdinalIgnoreCase)); + char[] separator = [',']; + + if (indexOfSpecificListsArg >= 0) + { + int indexOfListValues = indexOfSpecificListsArg + 1; + + string[] strListValues = args.ElementAtOrDefault(indexOfListValues)?.Split(separator, StringSplitOptions.RemoveEmptyEntries) ?? []; + if (strListValues.Length > 0) + { + config.NonInteractiveSpecificLists = strListValues; + config.NonInteractiveModeListName = string.Empty; + } + } + + if (indexOfSpecificUsersArg >= 0) + { + int indexOfUserValues = indexOfSpecificUsersArg + 1; + string[] strUserValues = args.ElementAtOrDefault(indexOfUserValues)?.Split(separator, StringSplitOptions.RemoveEmptyEntries) ?? []; + if (strUserValues.Length > 0) + { + config.NonInteractiveSpecificUsers = strUserValues; + } + } + } + + Log.Debug("Additional arguments:"); foreach (string argument in args) { Log.Debug(argument); @@ -898,7 +927,28 @@ public class Program { hasSelectedUsersKVP = new KeyValuePair>(true, new Dictionary { { "PurchasedTab", 0 } }); } - else if (Config.NonInteractiveMode && string.IsNullOrEmpty(Config.NonInteractiveModeListName)) + else if (Config.NonInteractiveMode && Config.NonInteractiveSpecificLists is not null && Config.NonInteractiveSpecificLists.Length > 0) + { + HashSet listUsernames = []; + 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); + } + users = users.Where(x => listUsernames.Contains(x.Key)).Distinct().ToDictionary(x => x.Key, x => x.Value); + hasSelectedUsersKVP = new KeyValuePair>(true, users); + } + else if (Config.NonInteractiveMode && Config.NonInteractiveSpecificUsers is not null && Config.NonInteractiveSpecificUsers.Length > 0) + { + HashSet usernames = [.. Config.NonInteractiveSpecificUsers]; + users = users.Where(u => usernames.Contains(u.Key)).ToDictionary(u => u.Key, u => u.Value); + hasSelectedUsersKVP = new KeyValuePair>(true, users); + } + else if (Config.NonInteractiveMode && string.IsNullOrEmpty(Config.NonInteractiveModeListName)) { hasSelectedUsersKVP = new KeyValuePair>(true, users); }