feat: added export to csv file
This commit is contained in:
parent
e9db40dee9
commit
d0ad97f08b
@ -1,4 +1,6 @@
|
||||
namespace TelegramMessageCounter;
|
||||
using System.Reflection;
|
||||
|
||||
namespace TelegramMessageCounter;
|
||||
|
||||
public static class ArgumentReader
|
||||
{
|
||||
@ -8,13 +10,12 @@ public static class ArgumentReader
|
||||
string? username = null;
|
||||
if (args.Length > 0 && args.Any(x => x == "--help"))
|
||||
{
|
||||
Console.WriteLine("Usage: TelegramMessageCounter --login (phone number) --user (username) <path>" +
|
||||
"\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");
|
||||
var helpFile = Assembly.GetExecutingAssembly()
|
||||
.GetManifestResourceStream("TelegramMessageCounter.help.txt");
|
||||
Console.WriteLine(new StreamReader(helpFile ?? new MemoryStream()).ReadToEnd());
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
|
||||
if (args.Any(x => x == "--user"))
|
||||
{
|
||||
var userNameArgIndex = args.ToList().IndexOf("--user");
|
||||
|
@ -1,20 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CsvHelper.Configuration.Attributes;
|
||||
|
||||
namespace TelegramMessageCounter
|
||||
{
|
||||
internal record CounterInfo
|
||||
(
|
||||
float MessageMultiplier,
|
||||
float SymbolsMultiplier,
|
||||
int UserMessageCount,
|
||||
int TargetMessageCount,
|
||||
int UserSymbolsCount,
|
||||
int TargetSymbolsCount,
|
||||
int FullMessageCount,
|
||||
string TargetUsername
|
||||
);
|
||||
}
|
||||
namespace TelegramMessageCounter;
|
||||
|
||||
public record CounterInfo(
|
||||
[property: Name("Message multiplier")]
|
||||
float MessageMultiplier,
|
||||
[property: Name("Symbols multiplier")]
|
||||
float SymbolsMultiplier,
|
||||
[property: Name("Your messages")]
|
||||
int UserMessageCount,
|
||||
[property: Name("Target messages")]
|
||||
int TargetMessageCount,
|
||||
[property: Name("Your symbols")]
|
||||
int UserSymbolsCount,
|
||||
[property: Name("Target messages")]
|
||||
int TargetSymbolsCount,
|
||||
[property: Name("All messages")]
|
||||
int FullMessageCount,
|
||||
[Index(0)]
|
||||
[property: Name("Target username")]
|
||||
string TargetUsername
|
||||
);
|
78
Program.cs
78
Program.cs
@ -5,27 +5,29 @@ namespace TelegramMessageCounter
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
var dataRequest = ArgumentReader.ReadArguments(args);
|
||||
var username = dataRequest.Login;
|
||||
Splash.MakeSplash();
|
||||
|
||||
|
||||
loginStage:
|
||||
if (username is null)
|
||||
{
|
||||
Console.WriteLine("Enter your phone number: ");
|
||||
username = Console.ReadLine();
|
||||
}
|
||||
|
||||
|
||||
if (username is null)
|
||||
{
|
||||
Console.WriteLine("Login is invalid!");
|
||||
goto loginStage;
|
||||
}
|
||||
|
||||
|
||||
Helpers.Log = (_, _) => { }; //force logger to shut up
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
@ -38,11 +40,11 @@ namespace TelegramMessageCounter
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.WriteLine($"Welcome, {Client.User}!");
|
||||
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;
|
||||
|
||||
|
||||
var res = GetInfo(user.id);
|
||||
if (res == null)
|
||||
{
|
||||
@ -50,12 +52,17 @@ namespace TelegramMessageCounter
|
||||
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);
|
||||
}
|
||||
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)
|
||||
{
|
||||
var dialogs = Client.Messages_GetAllDialogs().Result;
|
||||
@ -104,6 +111,7 @@ namespace TelegramMessageCounter
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
fullMsgCount = res.Count;
|
||||
}
|
||||
catch (Exception)
|
||||
@ -132,54 +140,36 @@ namespace TelegramMessageCounter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("[DONE]");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
return new(
|
||||
(float)userMsgCount / targetMsgCount,
|
||||
(float)userMsgCount / targetMsgCount,
|
||||
(float)userSymbolsCount / targetSymbolsCount,
|
||||
userMsgCount,
|
||||
targetMsgCount,
|
||||
userMsgCount,
|
||||
targetMsgCount,
|
||||
userSymbolsCount,
|
||||
targetSymbolsCount,
|
||||
fullMsgCount,
|
||||
fullMsgCount,
|
||||
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)
|
||||
{
|
||||
while (Client.User == null)
|
||||
switch (await Client.Login(loginInfo)) // returns which config is needed to continue login
|
||||
{
|
||||
case "verification_code": Console.Write("Code: "); loginInfo = Console.ReadLine()!; break;
|
||||
case "password": Console.Write("Password: "); loginInfo = Console.ReadLine()!; break; // if user has enabled 2FA
|
||||
case "verification_code":
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,12 @@ Program for calculate statistics of messages sended in telegram with your friend
|
||||
|
||||
## Usage
|
||||
|
||||
.\TelegramMessageCounter.exe --login (phone number) --user (username) <path>
|
||||
.\TelegramMessageCounter.exe --login <phone_number> --user <username> <path>
|
||||
where
|
||||
- `--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`
|
||||
- `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
|
||||
|
||||
|
50
ResultWriter.cs
Normal file
50
ResultWriter.cs
Normal 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!");
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
@ -7,13 +7,19 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<Company>Lisoveliy</Company>
|
||||
<Authors>Lisoveliy</Authors>
|
||||
<AssemblyVersion>0.1.3</AssemblyVersion>
|
||||
<FileVersion>0.1.3</FileVersion>
|
||||
<AssemblyVersion>0.2.0.0</AssemblyVersion>
|
||||
<FileVersion>0.2.0.0</FileVersion>
|
||||
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CsvHelper" Version="33.0.1" />
|
||||
<PackageReference Include="WTelegramClient" Version="3.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="help.txt" />
|
||||
<EmbeddedResource Include="help.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
5
help.txt
Normal file
5
help.txt
Normal 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.
|
Loading…
x
Reference in New Issue
Block a user