-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathMain.py
More file actions
413 lines (344 loc) · 16.8 KB
/
Main.py
File metadata and controls
413 lines (344 loc) · 16.8 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
CounterForMessenger - application for analyzing Messenger messages
"""
import json
import tkinter as tk
import glob
import importlib
from time import time
from datetime import timedelta, datetime, date
from os.path import exists
from os import listdir
from PIL import ImageTk
from gui.theme import ThemeManager, DefaultTheme, DarkTheme
# Local module imports
from utils import set_icon, set_resolution, existing_languages
from gui.config_page import ConfigurationPage
from gui.main_page import MainPage
class MasterWindow(tk.Tk):
"""Main window of the CounterForMessenger application"""
def __init__(self, *args, **kwargs):
"""
Application initialization
Args:
*args: Arguments passed to the base class
**kwargs: Named arguments passed to the base class
"""
tk.Tk.__init__(self, *args, **kwargs)
# User data
self.directory = ''
self.username = ''
self.language = 'English'
self.from_date_entry = ''
self.to_date_entry = ''
self.theme = ""
self.lang_mdl = importlib.import_module('langs.English')
self.sent_messages = 0
self.total_messages = 0
self.total_chars = 0
# Loading user data
self.load_data()
# Window configuration
self.title('Counter for Messenger')
set_icon(self)
# Frame container settings
self.container = tk.Frame(self)
self.container.pack(side='top', fill='both', expand=True)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
# Declaration of available frames and their dimensions
self.frames = {
"ConfigurationPage": [800, 600, None],
"MainPage": [1375, 700, None]
}
# Initialization of the ThemeManager and registration of Themes
self.theme_manager = ThemeManager()
self.theme_manager.register(DefaultTheme())
self.theme_manager.register(DarkTheme())
self.change_theme("default")
# Initialization and loading of frames into the container
self.refresh_frames()
# Displaying the appropriate start page
self.show_frame(
"MainPage" if exists('config.txt') else "ConfigurationPage"
)
def show_frame(self, page_name):
"""
Displays the selected frame
Args:
page_name: Name of the page to display
"""
width, height, frame = self.frames.get(page_name)
set_resolution(self, width, height)
# Show the new frame
frame.tkraise()
def get_username(self):
"""
Gets the username
Returns:
Username or a message indicating no name
"""
return self.lang_mdl.TITLE_NOT_APPLICABLE if self.username == '' or self.username.isspace() else self.username
def get_directory(self):
"""
Gets the path to the data directory
Returns:
Path to the directory or a message indicating no selection
"""
return self.lang_mdl.TITLE_NO_SELECTION if self.directory == '/' or self.directory.isspace() else self.directory
def get_from_date_entry(self):
"""
Gets the start date
Returns:
Start date or a message indicating no date
"""
return self.lang_mdl.TITLE_NOT_APPLICABLE if self.from_date_entry == '' else self.from_date_entry
def get_to_date_entry(self):
"""
Gets the end date
Returns:
End date or a message indicating no date
"""
return self.lang_mdl.TITLE_NOT_APPLICABLE if self.to_date_entry == '' else self.to_date_entry
def get_language(self):
"""
Gets the current language
Returns:
Name of the currently selected language
"""
# Check if the language variable contains a valid assignment
if self.language not in existing_languages():
# Default language is English
self.language = 'English'
self.lang_mdl = importlib.import_module('langs.English')
return self.language
def refresh_frames(self):
"""Initializes and arranges all application frames"""
# Initialize and arrange all frames on top of each other
# Switching between frames will allow navigation in the application
# without closing it
for page_class in (ConfigurationPage, MainPage):
page_name = page_class.__name__
width, height, old_frame = self.frames[page_name]
new_frame = page_class(parent=self.container, controller=self)
self.frames[page_name] = [width, height, new_frame]
new_frame.grid(row=0, column=0, sticky='nsew')
def update_data(self, username, directory, language, from_date_entry, to_date_entry, theme):
"""
Updates user data
Args:
username: Username
directory: Path to the data directory
language: Selected language
from_date_entry: Start date
to_date_entry: End date
theme: App's theme
"""
temp = self.language
self.username = username
self.directory = directory
self.language = language
self.from_date_entry = from_date_entry
self.to_date_entry = to_date_entry
self.theme = theme
self.lang_mdl = importlib.import_module(f'langs.{language}')
# Save user data in config.txt
with open('config.txt', 'w', encoding='utf-8') as f:
f.write(f'{username}\n{directory}\n{language}\n{from_date_entry}\n{to_date_entry}\n{theme}')
# Refresh the interface only if the language has changed
if temp != language:
self.refresh_frames()
def load_data(self):
"""Loads user data from config.txt"""
if exists('config.txt'):
try:
with open('config.txt', 'r', encoding='utf-8') as f:
lines = f.read().splitlines()
if len(lines) >= 5:
self.username = lines[0]
self.directory = lines[1]
self.language = lines[2]
self.from_date_entry = lines[3]
self.to_date_entry = lines[4]
self.theme = lines[5]
self.lang_mdl = importlib.import_module(f'langs.{self.language}')
except Exception as e:
print(f"Error loading configuration: {e}")
def extract_data(self, conversation):
"""
Extracts data from JSON files for a given conversation
Args:
conversation: Conversation folder to process
Returns:
Tuple containing various conversation statistics
"""
participants = {}
chat_title, chat_type = '', self.lang_mdl.TITLE_GROUP_CHAT
call_duration = total_messages = total_chars = sent_messages = start_date = 0
total_photos = total_gifs = total_videos = total_files = 0
# Processing dates
self._normalize_dates()
# Optimization: Pre-calculate timestamps for range comparison
start_ts = datetime.combine(self.from_date_entry, datetime.min.time()).timestamp() * 1000
# End timestamp: We want to include the entire end date.
# So we go to the next day at 00:00:00 and use strictly less than.
end_ts = datetime.combine(self.to_date_entry + timedelta(days=1), datetime.min.time()).timestamp() * 1000
# Check if we're processing an e2e conversation
is_e2e = conversation == 'e2e'
current_username = self.get_username()
# Processing JSON files in the conversation folder
path_to_browse = f'{self.directory}{conversation}'
# For e2e folder, we'll store conversation data separately for each person/file
e2e_conversations = {} if is_e2e else None
for file in glob.glob(f'{path_to_browse}/*.json'):
try:
with open(file, 'r', encoding='utf-8') as f:
data = json.load(f)
if is_e2e:
# E2E conversation processing - each file represents a different person
# Get thread name without number
thread_name = data.get('threadName', '')
person_name = ''
if '_' in thread_name:
person_name = thread_name.split('_')[0]
else:
person_name = thread_name
# Initialize conversation data for this person if not exists
if person_name not in e2e_conversations:
e2e_conversations[person_name] = {
'participants': {},
'chat_title': person_name,
'chat_type': self.lang_mdl.TITLE_PRIVATE_CHAT,
'call_duration': 0,
'total_messages': 0,
'total_chars': 0,
'sent_messages': 0,
'total_photos': 0,
'total_gifs': 0,
'total_videos': 0,
'total_files': 0,
'start_date': 0
}
# Add participants
for participant_name in data.get('participants', []):
e2e_conversations[person_name]['participants'][participant_name] = e2e_conversations[person_name]['participants'].get(participant_name, 0)
# Process messages for this person
for message in data.get('messages', []):
# Filter messages by timestamp
timestamp = int(message.get("timestamp", 0))
if start_ts <= timestamp < end_ts:
e2e_conversations[person_name]['total_messages'] += 1
try:
message_text = message.get('text', '')
e2e_conversations[person_name]['total_chars'] += len(message_text)
except (KeyError, TypeError) as e:
pass
sender = message.get('senderName', '')
if sender == cached_username:
e2e_conversations[person_name]['sent_messages'] += 1
# Optimization: try-except is faster than .get() for high-frequency keys
try:
e2e_conversations[person_name]['participants'][sender] += 1
except KeyError:
e2e_conversations[person_name]['participants'][sender] = 1
current_timestamp = message.get('timestamp', 0)
if e2e_conversations[person_name]['start_date'] == 0 or current_timestamp < e2e_conversations[person_name]['start_date']:
e2e_conversations[person_name]['start_date'] = current_timestamp
# Aggregate data for all e2e conversations
for conv in e2e_conversations.values():
participants.update(conv['participants'])
total_messages += conv['total_messages']
total_chars += conv['total_chars']
sent_messages += conv['sent_messages']
call_duration += conv['call_duration']
total_photos += conv['total_photos']
total_gifs += conv['total_gifs']
total_videos += conv['total_videos']
total_files += conv['total_files']
if start_date == 0 or conv['start_date'] < start_date:
start_date = conv['start_date']
else:
# Standard conversation processing
# Collecting chat participants
for participant in data.get('participants', []):
name = participant['name']
participants[name] = participants.get(name, 0)
# Updating counters
for message in data.get('messages', []):
# Filter messages by timestamp
timestamp = int(message["timestamp_ms"])
# Filtering messages within the selected period
if start_ts <= timestamp < end_ts:
total_messages += 1
# Counting characters
try:
total_chars += len(message.get('content', ''))
except (KeyError, TypeError):
pass
# Counting sender's messages
sender = message['sender_name']
if sender == cached_username:
sent_messages += 1
# Tracking participant messages
# Optimization: try-except is faster than .get() for high-frequency keys
try:
participants[sender] += 1
except KeyError:
participants[sender] = 1
# Recording call duration
call_duration += message.get('call_duration', 0)
# Save conversation creation date
current_timestamp = message['timestamp_ms']
if start_date == 0 or current_timestamp < start_date:
start_date = current_timestamp
# Counting multimedia
if 'photos' in message:
total_photos += len(message['photos'])
if 'gifs' in message:
total_gifs += len(message['gifs'])
if 'videos' in message:
total_videos += len(message['videos'])
if 'files' in message:
total_files += len(message['files'])
# Get chat name and type
chat_title = data.get('title', '')
# Check if it's a private chat
try:
# If there is no 'joinable_mode' element, the chat is private
_ = data['joinable_mode']
except KeyError:
chat_type = self.lang_mdl.TITLE_PRIVATE_CHAT
except Exception as e:
print(f"Error processing file {file}: {e}")
return (
chat_title, participants, chat_type, total_messages, total_chars,
call_duration, sent_messages, start_date, total_photos, total_gifs,
total_videos, total_files
)
def _normalize_dates(self):
"""Normalizes the format of input and output dates"""
# Convert tuples to a single value
if isinstance(self.from_date_entry, tuple) and len(self.from_date_entry) == 1:
self.from_date_entry = self.from_date_entry[0]
if isinstance(self.to_date_entry, tuple) and len(self.to_date_entry) == 1:
self.to_date_entry = self.to_date_entry[0]
# Convert strings to date objects
if not isinstance(self.from_date_entry, date):
try:
self.from_date_entry = datetime.strptime(str(self.from_date_entry), "%Y-%m-%d").date()
except (ValueError, TypeError):
self.from_date_entry = datetime(2000, 1, 1).date()
if not isinstance(self.to_date_entry, date):
try:
self.to_date_entry = datetime.strptime(str(self.to_date_entry), "%Y-%m-%d").date()
except (ValueError, TypeError):
self.to_date_entry = datetime.now().date()
def change_theme(self, name: str):
self.theme_manager.apply(name)
def get_theme_name(self):
return self.theme_manager.current_theme
def get_theme(self):
return self.theme_manager.themes[self.get_theme_name()]
if __name__ == "__main__":
app = MasterWindow()
app.mainloop()