From 1e40bbd73597ee7a9d11e6e100b2d1858e1ed610 Mon Sep 17 00:00:00 2001 From: Savely Savianok <1986developer@gmail.com> Date: Tue, 11 Feb 2025 20:51:33 +0300 Subject: [PATCH] feat: rework of argument read. Added possibility to analyze only one user. --- ArgumentReader.cs | 38 ++++++++++++++++++++++++++++++++++++++ DataRequest.cs | 3 +++ Program.cs | 28 +++++++++++----------------- README.md | 11 ++++++----- 4 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 ArgumentReader.cs create mode 100644 DataRequest.cs diff --git a/ArgumentReader.cs b/ArgumentReader.cs new file mode 100644 index 0000000..4ac0d70 --- /dev/null +++ b/ArgumentReader.cs @@ -0,0 +1,38 @@ +namespace TelegramMessageCounter; + +public static class ArgumentReader +{ + public static DataRequest ReadArguments(string[] args) + { + string? login = null; + string? username = null; + if (args.Length > 0 && args.Any(x => x == "--help")) + { + Console.WriteLine("Usage: TelegramMessageCounter --login (phone number) --user (username) " + + "\n login - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt" + + "\nuser - Username of telegram for individual stats. If argument is empty, analyzing all user history" + + "\n path - Path to save results file. If argument is empty, path will be \"results.txt\"\n"); + Environment.Exit(0); + } + + if (args.Any(x => x == "--user")) + { + var userNameArgIndex = args.ToList().IndexOf("--user"); + username = args[userNameArgIndex+1]; + } + + if (args.Any(x => x == "--login" || x == "-L")) + { + var userNameArgIndex = args.ToList().IndexOf("--login"); + if (userNameArgIndex == -1) + { + userNameArgIndex = args.ToList().IndexOf("-L"); + } + login = args[userNameArgIndex+1]; + } + string? path = args.Length > 1 ? args.Last() : null; + + return new(login, username, (path ?? default)!); + + } +} \ No newline at end of file diff --git a/DataRequest.cs b/DataRequest.cs new file mode 100644 index 0000000..fcbebad --- /dev/null +++ b/DataRequest.cs @@ -0,0 +1,3 @@ +namespace TelegramMessageCounter; + +public record DataRequest(string? Login, string? Username, string Path = "results.txt"); \ No newline at end of file diff --git a/Program.cs b/Program.cs index 94cf9d2..8bb7d1b 100644 --- a/Program.cs +++ b/Program.cs @@ -8,25 +8,19 @@ namespace TelegramMessageCounter private static readonly Client Client = new(23711185, "9e23f24bbb2f3dc3e561c0a5c9d3e622"); //Please, create your client, but don't shy use it if you are lazy :) static void Main(string[] args) { - if (args.Length > 0 && args[0] == "--help") - { - Console.WriteLine("Usage: TelegramMessageCounter " + - "\n login - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt" + - "\n path - Path to save results file. If argument is empty, path will be \"results.txt\"\n"); - } - - string? login = args.Length > 0 ? args[0] : null; - string? path = args.Length > 1 ? args[1] : null; + + var dataRequest = ArgumentReader.ReadArguments(args); + var username = dataRequest.Login; Splash.MakeSplash(); loginStage: - if (login is null) + if (username is null) { Console.WriteLine("Enter your phone number: "); - login = Console.ReadLine(); + username = Console.ReadLine(); } - if (login is null) + if (username is null) { Console.WriteLine("Login is invalid!"); goto loginStage; @@ -37,7 +31,7 @@ namespace TelegramMessageCounter Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Waiting to login..."); - DoLogin(login).Wait(); + DoLogin(username).Wait(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("[DONE]"); //Check logged @@ -46,6 +40,9 @@ namespace TelegramMessageCounter List infos = []; foreach(var user in Client.Messages_GetAllDialogs().Result.users.Values) { + if(dataRequest.Username is not null && user.username != dataRequest.Username) + continue; + var res = GetInfo(user.id); if (res == null) { @@ -56,10 +53,7 @@ namespace TelegramMessageCounter Console.WriteLine($"Multiplier {Client.User}/{res.TargetUsername} is: {res.Multiplier} of {res.MyMsg}/{res.TargetMsg} and total {res.FullMsg} messages"); infos.Add(res); } - if(path is not null) - SaveAndAddStats(infos, path); - else - SaveAndAddStats(infos); + SaveAndAddStats(infos, dataRequest.Path); } static CounterInfo? GetInfo(long targetId) diff --git a/README.md b/README.md index 7ff2b4a..d30ec14 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ Program for calculate statistics of messages sended in telegram with your friend ## Usage - .\TelegramMessageCounter.exe + .\TelegramMessageCounter.exe --login (phone number) --user (username) where -- `login` - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt +- `--login (-L)` - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt +- `--user` - Username of telegram for individual stats. If argument is empty, analyzing all user history - `path` - Path to save results file. If argument is empty, path will be `results.txt` To invoke this text use @@ -16,9 +17,9 @@ To invoke this text use .\TelegramMessageCounter.exe --help ### Example - .\TelegramMessageCounter.exe +14928587194 - .\TelegramMessageCounter.exe +14928587194 D:\Results\results.txt - .\TelegramMessageCounter.exe + .\TelegramMessageCounter.exe --login +14928587194 + .\TelegramMessageCounter.exe -L +14928587194 D:\Results\results.txt + .\TelegramMessageCounter.exe -L +14928587194 --user lisoveliy ### Results