feat: added export to csv file

This commit is contained in:
Pavel-Savely Savianok 2025-02-11 22:37:33 +03:00
parent e9db40dee9
commit d0ad97f08b
8 changed files with 130 additions and 74 deletions

View File

@ -1,4 +1,6 @@
namespace TelegramMessageCounter; using System.Reflection;
namespace TelegramMessageCounter;
public static class ArgumentReader public static class ArgumentReader
{ {
@ -8,10 +10,9 @@ public static class ArgumentReader
string? username = null; string? username = null;
if (args.Length > 0 && args.Any(x => x == "--help")) if (args.Length > 0 && args.Any(x => x == "--help"))
{ {
Console.WriteLine("Usage: TelegramMessageCounter --login (phone number) --user (username) <path>" + var helpFile = Assembly.GetExecutingAssembly()
"\n login - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt" + .GetManifestResourceStream("TelegramMessageCounter.help.txt");
"\nuser - Username of telegram for individual stats. If argument is empty, analyzing all user history" + Console.WriteLine(new StreamReader(helpFile ?? new MemoryStream()).ReadToEnd());
"\n path - Path to save results file. If argument is empty, path will be \"results.txt\"\n");
Environment.Exit(0); Environment.Exit(0);
} }

View File

@ -1,20 +1,23 @@
using System; using CsvHelper.Configuration.Attributes;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TelegramMessageCounter namespace TelegramMessageCounter;
{
internal record CounterInfo public record CounterInfo(
( [property: Name("Message multiplier")]
float MessageMultiplier, float MessageMultiplier,
[property: Name("Symbols multiplier")]
float SymbolsMultiplier, float SymbolsMultiplier,
[property: Name("Your messages")]
int UserMessageCount, int UserMessageCount,
[property: Name("Target messages")]
int TargetMessageCount, int TargetMessageCount,
[property: Name("Your symbols")]
int UserSymbolsCount, int UserSymbolsCount,
[property: Name("Target messages")]
int TargetSymbolsCount, int TargetSymbolsCount,
[property: Name("All messages")]
int FullMessageCount, int FullMessageCount,
[Index(0)]
[property: Name("Target username")]
string TargetUsername string TargetUsername
); );
}

View File

@ -5,10 +5,12 @@ namespace TelegramMessageCounter
{ {
internal static class Program internal static class Program
{ {
private static readonly Client Client = new(23711185, "9e23f24bbb2f3dc3e561c0a5c9d3e622"); //Please, create your client, but don't shy use it if you are lazy :) 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) static void Main(string[] args)
{ {
var dataRequest = ArgumentReader.ReadArguments(args); var dataRequest = ArgumentReader.ReadArguments(args);
var username = dataRequest.Login; var username = dataRequest.Login;
Splash.MakeSplash(); Splash.MakeSplash();
@ -38,9 +40,9 @@ namespace TelegramMessageCounter
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Welcome, {Client.User}!"); Console.WriteLine($"Welcome, {Client.User}!");
List<CounterInfo> infos = []; List<CounterInfo> infos = [];
foreach(var user in Client.Messages_GetAllDialogs().Result.users.Values) foreach (var user in Client.Messages_GetAllDialogs().Result.users.Values)
{ {
if(dataRequest.Username is not null && user.username != dataRequest.Username) if (dataRequest.Username is not null && user.username != dataRequest.Username)
continue; continue;
var res = GetInfo(user.id); var res = GetInfo(user.id);
@ -50,10 +52,15 @@ namespace TelegramMessageCounter
continue; continue;
} }
Console.WriteLine($"MessageMultiplier {Client.User}/{res.TargetUsername} is: {res.MessageMultiplier} of {res.UserMessageCount}/{res.TargetMessageCount} and total {res.FullMessageCount} messages"); Console.WriteLine(
$"MessageMultiplier {Client.User}/{res.TargetUsername} is: {res.MessageMultiplier} of {res.UserMessageCount}/{res.TargetMessageCount} and total {res.FullMessageCount} messages");
infos.Add(res); infos.Add(res);
} }
SaveAndAddStats(infos, dataRequest.Path);
if (dataRequest.Path.Contains(".csv"))
ResultWriter.SaveStatsToCsv(infos, dataRequest.Path);
else
ResultWriter.SaveStatsToTxt(infos, dataRequest.Path);
} }
static CounterInfo? GetInfo(long targetId) static CounterInfo? GetInfo(long targetId)
@ -104,6 +111,7 @@ namespace TelegramMessageCounter
// ignored // ignored
} }
} }
fullMsgCount = res.Count; fullMsgCount = res.Count;
} }
catch (Exception) catch (Exception)
@ -132,6 +140,7 @@ namespace TelegramMessageCounter
} }
} }
} }
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[DONE]"); Console.WriteLine("[DONE]");
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
@ -147,39 +156,20 @@ namespace TelegramMessageCounter
string.IsNullOrEmpty(peer?.username ?? "") ? peer?.id.ToString() ?? "unknown" : $"@{peer?.username}"); string.IsNullOrEmpty(peer?.username ?? "") ? peer?.id.ToString() ?? "unknown" : $"@{peer?.username}");
} }
static void SaveAndAddStats(List<CounterInfo> infos, string path = "results.txt")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Saving results...");
string fileData = "";
float multiplier = 0;
foreach (CounterInfo info in infos)
{
fileData += "username: " + info.TargetUsername + "\n" +
"message multiplier (You/Target): " + info.MessageMultiplier + "\n" +
"symbol multiplier (You/Target): " + info.SymbolsMultiplier + "\n" +
"total messages: " + info.FullMessageCount + "\n" +
"your messages: " + info.UserMessageCount + "\n" +
"target messages: " + info.TargetMessageCount + "\n" +
"your symbols in messages: " + info.UserSymbolsCount + "\n" +
"target symbols in messages: " + info.TargetSymbolsCount + "\n-----\n";
multiplier += info.MessageMultiplier;
}
multiplier /= infos.Count;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[DONE]");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Middle result is: {multiplier}");
File.WriteAllText(path, fileData);
Console.WriteLine("Goodbye!");
}
static async Task DoLogin(string loginInfo) static async Task DoLogin(string loginInfo)
{ {
while (Client.User == null) while (Client.User == null)
switch (await Client.Login(loginInfo)) // returns which config is needed to continue login switch (await Client.Login(loginInfo)) // returns which config is needed to continue login
{ {
case "verification_code": Console.Write("Code: "); loginInfo = Console.ReadLine()!; break; case "verification_code":
case "password": Console.Write("Password: "); loginInfo = Console.ReadLine()!; break; // if user has enabled 2FA Console.Write("Code: ");
loginInfo = Console.ReadLine()!;
break;
case "password":
Console.Write("Password: ");
loginInfo = Console.ReadLine()!;
break; // if user has enabled 2FA
default: loginInfo = null!; break; default: loginInfo = null!; break;
} }
} }

View File

@ -6,11 +6,12 @@ Program for calculate statistics of messages sended in telegram with your friend
## Usage ## Usage
.\TelegramMessageCounter.exe --login (phone number) --user (username) <path> .\TelegramMessageCounter.exe --login <phone_number> --user <username> <path>
where where
- `--login (-L)` - 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 - `--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` - `path` - Path to save results file. If argument is empty, path will be `results.txt`, if path contain '.csv' program
will output stats in CSV format.
To invoke this text use To invoke this text use

50
ResultWriter.cs Normal file
View File

@ -0,0 +1,50 @@
using System.Globalization;
using CsvHelper;
namespace TelegramMessageCounter;
public static class ResultWriter
{
public static void SaveStatsToTxt(List<CounterInfo> infos, string path = "results.txt")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"Saving results to {path}...");
string fileData = "";
float multiplier = 0;
foreach (CounterInfo info in infos)
{
fileData += "username: " + info.TargetUsername + "\n" +
"message multiplier (You/Target): " + info.MessageMultiplier + "\n" +
"symbol multiplier (You/Target): " + info.SymbolsMultiplier + "\n" +
"total messages: " + info.FullMessageCount + "\n" +
"your messages: " + info.UserMessageCount + "\n" +
"target messages: " + info.TargetMessageCount + "\n" +
"your symbols in messages: " + info.UserSymbolsCount + "\n" +
"target symbols in messages: " + info.TargetSymbolsCount + "\n-----\n";
multiplier += info.MessageMultiplier;
}
multiplier /= infos.Count;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[DONE]");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Middle result is: {multiplier}");
File.WriteAllText(path, fileData);
Console.WriteLine("Goodbye!");
}
public static void SaveStatsToCsv(List<CounterInfo> infos, string path = "results.csv")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"Saving results to {path}...");
using (var writer = new StreamWriter(path))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(infos);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[DONE]");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Goodbye!");
}
}

View File

@ -9,7 +9,7 @@
Console.WriteLine("/__ \\/\\/\\ / __\\"); Console.WriteLine("/__ \\/\\/\\ / __\\");
Console.WriteLine(" / /\\/ \\ / / "); Console.WriteLine(" / /\\/ \\ / / ");
Console.WriteLine(" / / / /\\/\\ \\/ /___ "); Console.WriteLine(" / / / /\\/\\ \\/ /___ ");
Console.WriteLine(" \\/ \\/ \\/\\____/ v1.4 ꞵeta"); Console.WriteLine($" \\/ \\/ \\/\\____/ v{System.Reflection.Assembly.GetEntryAssembly()?.GetName().Version} ꞵeta");
Console.WriteLine("Telegram Message Counter by Lisoveliy"); Console.WriteLine("Telegram Message Counter by Lisoveliy");
} }
} }

View File

@ -7,13 +7,19 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Company>Lisoveliy</Company> <Company>Lisoveliy</Company>
<Authors>Lisoveliy</Authors> <Authors>Lisoveliy</Authors>
<AssemblyVersion>0.1.3</AssemblyVersion> <AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.1.3</FileVersion> <FileVersion>0.2.0.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="WTelegramClient" Version="3.5.3" /> <PackageReference Include="WTelegramClient" Version="3.5.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="help.txt" />
<EmbeddedResource Include="help.txt" />
</ItemGroup>
</Project> </Project>

5
help.txt Normal file
View File

@ -0,0 +1,5 @@
Usage: TelegramMessageCounter --login <phone_number> --user <username> <path>
--login <phone_number> - Phone number of telegram. If argument is empty, waiting for Keyboard interrupt
--user <user> - Username of telegram for individual stats. If argument is empty, analyzing all users history
<path> - Path to save results file. If argument is empty, path will be `results.txt`, if path contain '.csv' program will output stats in CSV format.