forked from mnemocron/TelegramChatStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-results.py
82 lines (69 loc) · 2.93 KB
/
print-results.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
#! /usr/bin/python3
#_*_ coding: utf-8 _*_
'''
@file print-results.py
@author Simon Burkhardt - github.com/mnemocron
@date 2018.10.10
This file prints the resulting numbers to the command line
Post about this code:
https://www.reddit.com/r/LongDistance/comments/9mgcol/oc_chat_statistics_from_telegram_using_python/
Inspiration:
https://www.reddit.com/r/LongDistance/comments/9jud8j/analysis_of_texts_from_a_long_distance/
'''
from __future__ import print_function
import sys
import os
import optparse
import json
from pprint import pprint
parser = optparse.OptionParser('telegram-stats')
parser.add_option('-i', '--input-file', dest='indir', type='string', help='chat history file')
(opts, args) = parser.parse_args()
def load_file_to_raw(path):
try:
with open(path, encoding='utf-8-sig') as fh:
data = json.load(fh)
return data
except IOError:
print('Error: could not open the file')
exit(-1)
### MAIN
def main():
if ( opts.indir is None):
parser.print_help()
exit(0)
metrics = load_file_to_raw(opts.indir)
print('[ name: ' + metrics['A']['name'] + ']')
print('total message count: \t' + str(metrics['A']['total_messages']))
print('total word count: \t' + str(metrics['A']['total_words']))
print('total character count: \t' + str(metrics['A']['total_chars']))
print('average word count: \t' + str(metrics['A']['avg_words']))
print('total unique words: \t' + str(metrics['A']['unique_words']))
print('average character count: \t' + str(metrics['A']['avg_chars']))
print('used markdown in: \t' + str(metrics['A']['urls']) + ' messages')
print('total urls in messages: \t' + str(metrics['A']['urls']))
print('total photos: \t' + str(metrics['A']['photo']))
for key in metrics['A']['media']:
print('total ' + str(key) + ' count: \t\t' + str(metrics['A']['media'][key]))
print('')
print('[ name: ' + metrics['B']['name'] + ']')
print('total message count: \t' + str(metrics['B']['total_messages']))
print('total word count: \t' + str(metrics['B']['total_words']))
print('total character count: \t' + str(metrics['B']['total_chars']))
print('average word count: \t' + str(metrics['B']['avg_words']))
print('total unique words: \t' + str(metrics['B']['unique_words']))
print('average character count: \t' + str(metrics['B']['avg_chars']))
print('used markdown in: \t' + str(metrics['B']['urls']) + ' messages')
print('total urls in messages: \t' + str(metrics['B']['urls']))
print('total photos: \t' + str(metrics['B']['photo']))
for key in metrics['B']['media']:
print('total ' + str(key) + ' count: \t\t' + str(metrics['B']['media'][key]))
print('')
print('[ combined stats ]')
print('total message count: \t' + str(metrics['total']))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt as e:
print('Aborted by KeyboardInterrupt')
exit(0)