-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinactiveusers.py
More file actions
executable file
·58 lines (48 loc) · 1.87 KB
/
inactiveusers.py
File metadata and controls
executable file
·58 lines (48 loc) · 1.87 KB
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
#!/usr/bin/env python3
"""
A quick hack of a program to find a rough percentage of users in a chat who have sent less than 3 messages.
Warning: written at 1AM
"""
import argparse
from json import loads
from os import path
from collections import defaultdict
def main():
"""
main function
"""
#cutoff for a 'non active' user
minimum = 3
non_active_users = 0
active_users = 0
parser = argparse.ArgumentParser(description="Find the number of inactive users (users who have sent less than 3 messages) in a Telegram chat")
parser.add_argument('filepath', help='the jsonl chatlog file to analyse')
args = parser.parse_args()
filepath = args.filepath
_, filename = path.split(filepath)
filename, _ = path.splitext(filename)
#make filename just the name of the file, with no leading directories and no extension
counter = defaultdict(int) #store events from each user
#names = {} #dict
total_datapoints = 0
with open(filepath, 'r') as jsonfile:
events = (loads(line) for line in jsonfile)
for event in events:
if "from" in event:
if "peer_id" in event["from"] and "print_name" in event["from"]:
total_datapoints += 1
user = event['from']['peer_id']
counter[user] += 1
for person, frequency in counter.items():
if frequency < minimum:
non_active_users += 1
else:
active_users += 1
print('For this chat, there were {} users who sent less than'
' {} messages, out of a total of {}.'.format(
non_active_users,minimum,non_active_users+active_users))
print("That's", round(100* non_active_users/(non_active_users + active_users),1), "%!")
# print(type(*sorted(counter.items())))
# plt.pie(*zip(*sorted(counter.items())))
if __name__ == "__main__":
main()