-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_time_data.py
85 lines (77 loc) · 3.33 KB
/
extract_time_data.py
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
from telethon import TelegramClient, events, sync
from datetime import timezone, datetime, timedelta
import numpy as np
from draw import draw
with open('api.config', 'r') as config_file:
lines = config_file.readlines()
api_id = int(lines[0])
api_hash = lines[1][:-1]
client = TelegramClient('session_name', api_id, api_hash)
client.start()
print("Client started!")
my_id = client.get_me().id
username = input("Enter username (or telegram id as a number) e.g.: mohammad or 81273618\n")
if username.isnumeric():
username = int(username)
message_times = list()
total_msgs = client.get_messages(username).total
time_diffrence = timedelta(hours = 6)
receive_response_count = 0
receive_total_response_time = 0
sent_response_count = 0
sent_total_response_time = 0
num_sent_start_conversation = 0
num_receive_start_conversation = 0
num_total_sent = 0
num_total_receive = 0
count_conversation = 0
last_msg_date = 0
last_msg_id = 0
i = 0
for message in client.iter_messages(username):
# if i == 10:
# break
message_time = message.date.replace(tzinfo=timezone.utc).astimezone(tz=None)
message_times.append([message_time, message.sender.id == my_id])
i += 1
print(" Message " + str(i) + " of " + str(total_msgs), end="\r")
if message.sender.id == my_id:
num_total_sent += 1
else:
num_total_receive += 1
new_msg_time = message_time
new_msg_id = message.sender.id == my_id
if i != 1:
if (new_msg_time < last_msg_date-time_diffrence):
if message.sender.id == my_id:
num_sent_start_conversation += 1
else:
num_receive_start_conversation += 1
count_conversation += 1
else:
if last_msg_id != new_msg_id:
if message.sender.id == my_id:
receive_response_count += 1
receive_response_time = (last_msg_date - new_msg_time).total_seconds()
receive_total_response_time += receive_response_time
else:
sent_response_count += 1
sent_response_time = (last_msg_date - new_msg_time).total_seconds()
sent_total_response_time += sent_response_time
last_msg_date = new_msg_time
last_msg_id = new_msg_id
avg_sent_response_time = sent_total_response_time / sent_response_count
avg_receive_response_time = receive_total_response_time / receive_response_count
print('')
with open(str(username)+"_analysis.txt", "w") as text_file:
print("Number of total messages : "+str(num_total_receive+num_total_sent), file=text_file)
print("Number of total messages sent : "+str(num_total_sent), file=text_file)
print("Number of total messages received : "+str(num_total_receive), file=text_file)
print("Number of total conversations : "+str(count_conversation), file=text_file)
print("Number of total conversations sender started : "+str(num_sent_start_conversation), file=text_file)
print("Number of total conversations receiver started : "+str(num_receive_start_conversation), file=text_file)
print("Average response time of sender : "+str(avg_sent_response_time), file=text_file)
print("Average response time of reciver : "+str(avg_receive_response_time), file=text_file)
file_path = str(username) + "_times.npy"
np.save(file_path, np.array(message_times))
draw(file_path, True)