1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use anyhow::anyhow;
use clap::*;
use std::{fs, ops::Add};
use telegram_csv_parser::*;
fn main() -> anyhow::Result<()> {
let matches = App::new("Csv-Telegram-Parser")
.version("0.1")
.author("Vladyslav Bezborodov")
.about("A simple parser of csv_telegram files")
.arg(
Arg::with_name("file")
.short("f")
.long("file")
.value_name("FILE")
.help("Sets the input file you want to parse")
.takes_value(true),
).arg(
Arg::with_name("output")
.short("o")
.long("output")
.value_name("FILE")
.help("Sets the output file")
.takes_value(true),
)
.get_matches();
let path_to_file = matches
.value_of("file")
.ok_or_else(|| anyhow!("No input file specified"))?;
let output_file = matches.value_of("output").unwrap_or("default_output.txt");
parse_csv(&path_to_file, &output_file)?;
Ok(())
}
fn parse_csv(path: &str, output_file: &str) -> anyhow::Result<()> {
let unparsed_file = fs::read_to_string(path)?;
let file = CSVParser::parse(Rule::file, &unparsed_file)?
.next()
.ok_or_else(|| anyhow!("No pairs found"))?;
//println!("{:?}", file);
let mut word_count: usize = 0;
let mut message_count: u64 = 0;
for row in file.into_inner() {
match row.as_rule() {
Rule::row => {
if row.as_str().contains("PeerUser(user_id=") {
// Containing "PeerUser(user_id=" means that it is the user collected the telegram data
message_count += 1; // and it displays the messages user sent
for value in row.into_inner() {
match value.as_rule() {
Rule::value => {
//println!("{}", value.as_str());
for quoted_string in value.into_inner() {
match quoted_string.as_rule() {
Rule::quoted_string => {
//println!("{}", quoted_string.as_str());
if quoted_string.as_str().len() != 2 {
//println!("{}", quoted_string.as_str().len());
word_count += quoted_string
.as_str()
.split_whitespace()
.count();
//println!("{}",word_count);
}
}
_ => unreachable!(),
}
}
}
_ => unreachable!(),
}
}
}
}
Rule::EOI => (),
_ => unreachable!(),
}
}
println!("Number of messages: {}", message_count);
println!("Number of words from all messages: {}", word_count);
write_to_file(output_file, (String::from("User's message count: ").add(&message_count.to_string()).add("\n")
.add("User's word count: ").add(&word_count.to_string())).as_str()).unwrap();
Ok(())
}
fn write_to_file(file_path: &str, content: &str) -> std::io::Result<()> {
fs::write(file_path, content)
}