forked from expectocode/telegram-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersovertime.py
executable file
·53 lines (42 loc) · 1.56 KB
/
usersovertime.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
#!/usr/bin/env python3
"""
A program to plot the users joining a chat over time. Note that leaving events are not noted.
TODO: support multiple chats.
TODO: support saving to image
"""
import argparse
import os
from json import loads
from datetime import date
from collections import defaultdict
from pprint import pprint
import matplotlib.pyplot as plt
def main():
"""
main function
"""
parser = argparse.ArgumentParser(
description="Visualise the growth a chat over time - note that leavers are not recorded, so this program can only show the join rate.")
parser.add_argument('path', help='the json file (chat log) to analyse')
args = parser.parse_args()
filepath = args.path
with open(filepath, 'r') as f:
events = (loads(line) for line in f)
counter = defaultdict(int)
for event in events:
if "action" in event and (event["action"]["type"] == "chat_add_user" or event['action']['type'] == 'chat_add_user_link'):
day = date.fromtimestamp(event["date"])
counter[day] += 1
filename = os.path.splitext(os.path.split(filepath)[-1])[0]
#frequencies = {key: l.count(True)/l.count(False) * 100 for key, l in counter.items()}
users_per_day = sorted(counter.items())
u_count = 0
for idx, (day, users) in enumerate(users_per_day):
u_count += users
users_per_day[idx] = (day, u_count)
print(users_per_day)
plt.plot(*zip(*users_per_day))
plt.title('members in "{}"'.format(filename))
plt.show()
if __name__ == "__main__":
main()