-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
1557 lines (1276 loc) · 62 KB
/
Copy pathgui.py
File metadata and controls
1557 lines (1276 loc) · 62 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Author: Phaticusthiccy
This script is a graphical user interface (GUI) application built with Tkinter. It allows users to monitor their gaming activity and update their Telegram status accordingly.
Key Features:
1. Add and manage a list of games to monitor.
2. Display a searchable list of available games.
3. Set a default biography message for Telegram.
4. Automatically update Telegram status with the current game and elapsed time.
5. Notify specified Telegram users when a game starts.
6. Toggle between light and dark themes.
7. View game statistics and usage reports.
Core Components:
- **Game Management**: Add, remove, and view games in a monitored list.
- **Telegram Integration**: Update status and send notifications using the Telethon library.
- **Process Monitoring**: Detect running games using the psutil library.
- **Customizable Settings**: Configure default biography, notification messages, and themes.
- **Statistics**: Generate reports on game activity and resource usage.
Setup Instructions:
1. Configure your Telegram API credentials and other settings in the `.env` file.
2. Run the script to launch the application.
3. Add games to monitor, set a biography, and specify notification recipients.
4. Start monitoring to update your Telegram status in real-time.
Note: A Telegram account and API credentials are required for this application to function.
"""
import asyncio
from turtle import color
import psutil
import sys
import time
import tkinter as tk
from tkinter import messagebox
import tkinter.font as tkfont
from telethon import TelegramClient
from telethon.tl.functions.account import UpdateProfileRequest
import json
from dotenv import load_dotenv
import os
import platform
from PIL import Image, ImageTk
import requests
import sv_ttk
import logging
from datetime import datetime
import GPUtil
"""
Configures the logging system for the application.
Adds a console handler and a file handler to the root logger, both set to log at the DEBUG level. The console handler and file handler use a common formatter that includes the timestamp, log level, and log message.
The root logger is also set to log at the DEBUG level, ensuring that all log messages at or above the DEBUG level will be captured by the configured handlers.
"""
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('./debug/debug.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
load_dotenv()
added_games = []
default_bio = os.getenv("DEFAULT_BIO")
start = False
default_start = False
me_welcome = None
api_id = os.getenv("API_ID")
api_hash = os.getenv("API_HASH")
app_icon = os.getenv("APP_ICON")
ACTION_EMOJI_LESS_10_MIN = os.getenv("ACTION_EMOJI_LESS_10_MIN")
ACTION_EMOJI_10_TO_60_MIN = os.getenv("ACTION_EMOJI_10_TO_60_MIN")
ACTION_EMOJI_60_TO_120_MIN = os.getenv("ACTION_EMOJI_60_TO_120_MIN")
ACTION_EMOJI_MORE_120_MIN = os.getenv("ACTION_EMOJI_MORE_120_MIN")
global latest_version
global local_version
STATS_FILE = os.getenv("STATS_FILE")
game_stats = json.load(open('./' + STATS_FILE))
notification_usernames = []
global playing_game
playing_game = None
notification_message_text_global = None
if "notification_usernames" in game_stats:
notification_usernames = game_stats["notification_usernames"]
else:
game_stats["notification_usernames"] = []
if "notification_message" in game_stats:
notification_message_text_global_str = game_stats["notification_message"]
else:
game_stats["notification_message"] = os.getenv("NOTIFICATION_MESSAGE")
notification_message_text_global_str = os.getenv("NOTIFICATION_MESSAGE")
if "default_bio" in game_stats:
default_bio = game_stats["default_bio"]
else:
game_stats["default_bio"] = os.getenv("DEFAULT_BIO")
default_bio = os.getenv("DEFAULT_BIO")
if "debugmode" in game_stats:
debugmode = True if game_stats["debugmode"] == True else False
os.environ["DEBUG"] = "true" if debugmode == True else "false"
else:
game_stats["debugmode"] = False
debugmode = False
def get_cpu_usage():
"""
Gets the current CPU usage as a percentage.
Args:
process_name (str): The name of the process to get the CPU usage for.
Returns:
float: The current CPU usage as a percentage.
"""
return psutil.cpu_percent()
def get_gpu_usage():
"""
Gets the current GPU usage as a percentage.
Returns:
float: The current GPU usage as a percentage.
"""
return (max(GPUtil.getGPUs(), key=lambda gpu: gpu.load).load) * 100
def log_game_start(game_name):
"""
Logs the start of a game session in the game_stats dictionary.
Args:
game_name (str): The name of the game being played.
Notes:
The start time is recorded in ISO format using datetime.now().isoformat().
If the game is not already in the daily stats dictionary, a new entry is created with a start time and total duration of 0.
The start_time is updated every time the game starts.
"""
current_time = datetime.now().isoformat()
if game_name not in game_stats["daily"]:
game_stats["daily"][game_name] = {"start_time": current_time, "total_duration": 0}
else:
game_stats["daily"][game_name]["start_time"] = current_time
def log_game_end(game_name):
"""
Logs the end of a game session in the game_stats dictionary.
Args:
game_name (str): The name of the game being played.
Notes:
The end time is recorded in ISO format using datetime.now().isoformat().
The duration of the session is calculated by taking the difference between the start and end times.
The total duration of all sessions for the game is incremented by the duration of this session.
The updated game_stats dictionary is written to the file specified by STATS_FILE by calling _save_stats_to_file.
"""
if game_name in game_stats["daily"]:
start_time = datetime.fromisoformat(game_stats["daily"][game_name]["start_time"])
end_time = datetime.now()
duration = (end_time - start_time).total_seconds() / 60
game_stats["daily"][game_name]["total_duration"] += duration
_save_stats_to_file()
def _save_stats_to_file():
"""Writes the game_stats dictionary to the file specified by STATS_FILE in JSON format."""
with open(STATS_FILE, 'w') as f:
json.dump(game_stats, f, indent=4)
async def get_first_name(username):
"""
Asynchronously retrieves the first name of a Telegram user given their username.
Args:
username (str): The Telegram username of the user.
Returns:
str: The first name of the user, or None if the user is not found or an error occurs.
"""
try:
user = await client.get_entity(username)
return user.first_name
except Exception as e:
logger.warning(f"Could not get first name for {username}: {e}")
return False
def get_latest_version(language="en"):
"""
Retrieves the latest version information from a remote source.
Returns:
dict: A dictionary containing the latest version and update message, or None if an error occurred.
"""
try:
changeLogURL = "https://raw.githubusercontent.com/phaticusthiccy/Telegram-Activity/master/sample.tr.env" if language == "tr" else "https://raw.githubusercontent.com/phaticusthiccy/Telegram-Activity/master/sample.env"
response = requests.get(changeLogURL)
response.raise_for_status()
message_payload = {
"version": "",
"message": ""
}
for line in response.text.split("\n"):
if line.startswith("VERSION="):
message_payload["version"] = line.split("=")[1].strip('"')
if line.startswith("UPDATE_MESSAGE="):
message_payload["message"] = (line.split("=")[1].strip('"')).replace("&", "\n")
return message_payload
except requests.exceptions.RequestException as e:
logger.error(f"Error retrieving latest version: {e}")
return {
"version": "",
"message": ""
}
async def print_me():
"""
Asynchronously retrieves the user's own Telegram account information and stores it in the `me_welcome` global variable.
"""
global me_welcome;
me_welcome = await client.get_me()
def checkAuth():
"""
Checks that the required configuration values are set before running the application.
Raises:
ValueError: If any of the required configuration values are missing.
Returns:
bool: True if all required configuration values are set, False otherwise.
"""
if not os.path.isfile(".env"):
raise ValueError("The .env file is missing!")
if api_id is None or api_id == "":
raise ValueError(os.getenv("APP_ID_MISSING"))
if api_hash is None or api_hash == "":
raise ValueError(os.getenv("APP_HASH_MISSING"))
if default_bio is None or default_bio == "":
raise ValueError(os.getenv("DEFAULT_BIO_MISSING"))
if app_icon is None or app_icon == "":
raise ValueError(os.getenv("APP_ICON_MISSING"))
return True;
checkAuth()
logger.info(os.getenv("LOADING_MESSAGE"))
logger.info(os.getenv("DEBUG_ON")) if os.getenv("DEBUG") == "true" else None
local_version = os.getenv("VERSION")
logger.info(os.getenv("DEBUG_VERSION") + local_version) if os.getenv("DEBUG") == "true" else None
def toggle_debug_mode(fromTheme):
"""
Toggles the debug mode for the application.
Args:
fromTheme (bool): Indicates whether the toggle was triggered from a theme change.
Returns:
None
"""
if not fromTheme:
if debug_mode_var.get():
os.environ["DEBUG"] = "true"
game_stats["debugmode"] = True
logger.info(os.getenv("DEBUG_MODE_ON"))
else:
os.environ["DEBUG"] = "false"
game_stats["debugmode"] = False
logger.info(os.getenv("DEBUG_MODE_OFF"))
_save_stats_to_file()
if theme == 0:
debug_mode_button.configure(selectcolor="black")
hint_mode_button.configure(selectcolor="black")
else:
debug_mode_button.configure(selectcolor="white")
hint_mode_button.configure(selectcolor="white")
return
def toggle_hint_mode(fromTheme):
"""
Toggles the hint mode for the application.
Args:
fromTheme (bool): Indicates whether the toggle was triggered from a theme change.
Returns:
None
"""
if not fromTheme:
if hint_mode_var.get():
os.environ["HINTS"] = "true"
logger.info(os.getenv("HINTS_ON"))
else:
os.environ["HINTS"] = "false"
logger.info(os.getenv("HINTS_OFF"))
if theme == 0:
return hint_mode_button.configure(selectcolor="black")
else:
return hint_mode_button.configure(selectcolor="white")
def is_supported_os():
"""
Returns the current system platform as a lowercase string.
This function checks the current system platform and returns it as a lowercase string. If the DEBUG environment variable is set to "true", it will also log the system platform to the logger.
Returns:
str: The current system platform as a lowercase string.
"""
system = platform.system() + " " + platform.release()
logger.info(os.getenv("DEBUG_SYSTEM") + system) if os.getenv("DEBUG") == "true" else None
return system
def load_process_mapping(file_path):
"""
The function `load_process_mapping` reads and loads a JSON file from the specified file path.
:param file_path: The `file_path` parameter in the `load_process_mapping` function is a string that
represents the path to the file containing the process mapping data that you want to load and
process. This file should be in a format that can be loaded using the `json.load()` function, such
as a JSON
:return: The function `load_process_mapping` reads a JSON file located at the `file_path` and
returns the contents of the file as a Python dictionary after loading it using `json.load()`.
"""
with open(file_path, 'r') as file:
return json.load(file)
def handle_exit(signum, frame):
"""
Handles the exit signal for the application.
When the application receives an exit signal (e.g. from the user closing the window),
this function is called to quit the main event loop and exit the application.
"""
try:
root.quit()
except:
pass
logger.info(os.getenv("DEBUG_LOGOUT") + local_version) if os.getenv("DEBUG") == "true" else None
sys.exit()
def capitalize_first_letters(text):
"""
Capitalizes the first letter of each word in the given text.
Args:
text (str): The input text to capitalize.
Returns:
str: The input text with the first letter of each word capitalized.
"""
words = text.split()
capitalized_words = [word[0].upper() + word[1:] if word else '' for word in words]
return ' '.join(capitalized_words)
def show_stats():
"""
Displays a window containing statistics about the user's gaming activity.
Args:
None
Returns:
None
"""
stats_window = tk.Toplevel(root)
stats_window.withdraw()
stats_window.title(os.getenv("STATS_TITLE"))
root.update_idletasks()
root_x = root.winfo_x()
root_y = root.winfo_y()
root_width = root.winfo_width()
root_height = root.winfo_height()
stats_window_width = 350
stats_window_height = 200
x = root_x + (root_width // 2) - (stats_window_width // 2)
y = root_y + (root_height // 2) - (stats_window_height // 2)
stats_window.geometry(f"{stats_window_width}x{stats_window_height}+{x}+{y}")
stats_window.resizable(False, False)
def lock_position(event):
stats_window.geometry(f"{stats_window_width}x{stats_window_height}+{x}+{y}")
stats_window.bind('<Configure>', lock_position)
if theme == 0:
bg_color = "#2b2b2b"
fg_color = "white"
select_color = "white"
else:
bg_color = "#f0f0f0"
fg_color = "black"
select_color = "black"
stats_window.configure(bg=bg_color)
title_label = tk.Label(stats_window, text=os.getenv("STATS_TITLE"), font=(poppins_font, 16, "bold"), bg=bg_color, fg=fg_color)
title_label.pack(pady=(20, 10))
content_frame = tk.Frame(stats_window, bg=bg_color)
content_frame.pack(pady=10)
generate_button = tk.Button(content_frame, text=os.getenv("GENERATE_REPORT"), command=lambda: _generate_report(stats_window), font=(poppins_font, 12), bg="#4CAF50", fg="white", relief="raised", cursor="hand2")
generate_button.pack(pady=(10, 0))
stats_window.deiconify()
def _generate_report(oldWindow=None):
"""
Destroys the old statistics window and creates a new one with the current daily game activity data.
Args:
oldWindow (tkinter.Toplevel): The old statistics window to be destroyed.
Returns:
None
"""
oldWindow.destroy()
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import ConnectionPatch
from matplotlib.widgets import Button
data = game_stats["daily"]
labels = list(data.keys())
labelsNames = [unedited_process_name_mapping[data][0] for data in labels]
total_durations = [v['total_duration'] for v in data.values()]
sum_total = sum(total_durations)
overall_ratios = [dur / sum_total for dur in total_durations]
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_axes([0.3, 0.1, 0.35, 0.8])
ax2 = fig.add_axes([0.7, 0.1, 0.25, 0.8])
wedges = None
current_hovered = None
def draw_plots(selected_label, hovered=None):
nonlocal wedges, current_hovered
idx = labels.index(selected_label)
explode = [0] * len(labels)
explode[idx] = 0.1
if hovered is not None:
explode[hovered] = 0.2
ax1.clear()
ax2.clear()
ratios = overall_ratios
wedges, texts, autotexts = ax1.pie(ratios, autopct='%1.1f%%',
startangle=0, labels=labelsNames,
explode=explode, textprops={'fontsize': 8})
for wedge in wedges:
wedge.set_picker(True)
if hovered is not None:
wedges[hovered].set_linewidth(3)
wedges[hovered].set_edgecolor('black')
game_data = data[selected_label]
played_time = game_data['total_duration']
ax1.text(0, -1.5, f'{os.getenv("PLAYED_TIME")} {played_time:.2f} {os.getenv("DURATION")}',
ha='center', fontsize=10, bbox=dict(facecolor='white', alpha=0.5))
wedge = wedges[idx]
theta1, theta2 = wedge.theta1, wedge.theta2
center, r = wedge.center, wedge.r
game_data = data[selected_label]
cpu = game_data.get('avgCPUusage', 0) / 100
gpu = game_data.get('avgGPUusage', 0) / 100
age_ratios = [cpu, gpu]
age_labels = ['CPU', 'GPU']
bottom = 1
width = 0.2
colors = ['#1f77b4', '#ff7f0e']
for j, (height, label) in enumerate(reversed(list(zip(age_ratios, age_labels)))):
bottom -= height
bars = ax2.bar(0, height, width, bottom=bottom, color=colors[j],
label=label, alpha=0.7)
ax2.bar_label(bars, labels=[f"{height*100:.0f}%"],
label_type='center', color='white')
ax2.set_title(f'{os.getenv("COMPUTE_USAGE")} ({selected_label})', pad=20)
ax2.legend(loc='upper right')
ax2.axis('off')
ax2.set_xlim(-2.5 * width, 2.5 * width)
bar_top = 1.0
bar_bottom = 1.0 - sum(age_ratios)
fig.canvas.draw_idle()
def on_pick(event):
if event.mouseevent.button == 1:
artist = event.artist
if wedges and artist in wedges:
idx = wedges.index(artist)
selected_label = labels[idx]
global current_selection
current_selection = selected_label
print(selected_label)
hoveredLabel = labels.index(selected_label)
draw_plots(selected_label, hovered=hoveredLabel)
def on_motion(event):
nonlocal current_hovered
if wedges:
for i, wedge in enumerate(wedges):
if wedge.contains(event)[0]:
if current_hovered != i:
current_hovered = i
draw_plots(labels[i], hovered=i)
return
fig.canvas.mpl_connect('pick_event', on_pick)
fig.canvas.mpl_connect('motion_notify_event', on_motion)
try:
current_selection = labels[0]
except:
messagebox.showinfo("Error", os.getenv("NO_GAME_DATA"))
return
draw_plots(current_selection)
ax_button = fig.add_axes([0.01, 0.9, 0.1, 0.05])
menu_button = Button(ax_button, os.getenv("MENU"))
def toggle_menu(event):
show_game_selection_window()
menu_button.on_clicked(toggle_menu)
def show_game_selection_window():
import tkinter as tk
from tkinter import ttk
selection_window = tk.Toplevel()
selection_window.title(os.getenv("SELECT_GAME"))
selection_window.geometry("600x500")
selection_window.resizable(False, False)
fig_window = fig.canvas.manager.window
selection_window.update_idletasks()
fig_x = fig_window.winfo_x()
fig_y = fig_window.winfo_y()
fig_width = fig_window.winfo_width()
fig_height = fig_window.winfo_height()
window_width = selection_window.winfo_reqwidth() + 100
window_height = selection_window.winfo_reqheight()
x = fig_x + (fig_width - window_width) // 2
y = fig_y + (fig_height - window_height) // 2
selection_window.geometry(f"+{x}+{y}")
def lock_position(event):
selection_window.geometry(f"{window_width}x{window_height}+{x}+{y}")
selection_window.bind('<Configure>', lock_position)
search_var = tk.StringVar()
search_entry = ttk.Entry(selection_window, textvariable=search_var, width=200)
search_entry.pack(pady=10, padx=10, fill=tk.X)
frame = ttk.Frame(selection_window)
frame.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL)
listbox = tk.Listbox(frame, yscrollcommand=scrollbar.set, cursor="hand2", selectmode=tk.SINGLE, width=200, height=25)
scrollbar.config(command=listbox.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
for lab in labelsNames:
listbox.insert(tk.END, lab)
def filter_list(*args):
search_term = search_var.get().lower()
listbox.delete(0, tk.END)
for lab in labelsNames:
if search_term in lab.lower():
listbox.insert(tk.END, lab)
search_var.trace_add("write", filter_list)
def on_select(event):
selection = listbox.curselection()
if selection:
selected_game = listbox.get(selection[0])
try:
idx = labelsNames.index(selected_game)
selected_key = labels[idx]
global current_selection
current_selection = selected_key
draw_plots(selected_key)
except ValueError:
pass
selection_window.destroy()
listbox.bind('<Double-1>', on_select)
listbox.bind('<Return>', on_select)
ok_button = ttk.Button(selection_window, text="Select", command=lambda: on_select(None))
ok_button.pack(pady=10)
try:
fig_width = 1200
fig_height = 600
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width - fig_width) // 2
y = (screen_height - fig_height) // 2
fig.canvas.manager.window.geometry(f"{fig_width}x{fig_height}+{x}+{y}")
except:
pass
plt.show()
def find_process_name(name):
"""
Finds the process name that matches the given name.
Args:
name (str): The name to search for.
Returns:
str or False: The matching process name, or False if no match is found.
"""
name_lower = name.lower()
for key, value in process_name_mapping.items():
if name_lower in [item.lower() for item in value]:
return key
return False
def get_process_name(friendly_name):
"""
Returns the process name for the given friendly name. If no mapping is found, the original friendly name is returned.
Args:
friendly_name (str): The friendly name to look up.
Returns:
str: The process name for the given friendly name, or the original friendly name if no mapping is found.
"""
return friendly_name_mapping.get(friendly_name, friendly_name)
def get_friendly_name(process_name):
"""
Returns a friendly name for the given process name. If no friendly name mapping is
available, the original process name is returned.
Args:
process_name (str): The name of the process to get a friendly name for.
Returns:
str: The friendly name for the given process name, or the original process name
if no friendly name mapping is available.
"""
return friendly_name_mapping.get(process_name, process_name)
def is_any_game_running(game_names):
"""
Checks if any of the specified games are currently running.
Args:
game_names (list[str]): A list of game names to check.
Returns:
str or None: The name of the first game found to be running, or None if no games are running.
"""
running_processes = [proc.info['name'].lower() for proc in psutil.process_iter(['name'])]
# Looping through games to see if any are running. (It's like playing hide and seek with processes, but less fun)
for game_name in game_names:
if any(name.lower() in running_processes for name in game_name):
return game_name[0]
return None
async def update_status(game_name, elapsed_time, games):
"""
Updates the Telegram profile status with the current game name and elapsed time.
Args:
game_name (str): The name of the game to update the status with.
elapsed_time (int): The elapsed time in seconds to update the status with.
games (list[tuple[str, str]]): A list of tuples containing the process name and friendly name of the games.
Returns:
None
"""
global start
global notification_usernames
global playing_game
global notification_message_text_global
global notification_message_text_global_str
if game_name is False and elapsed_time is False:
try:
await client(UpdateProfileRequest(about=default_bio))
playing_game = None
except Exception as e:
logger.warning(os.getenv("ERROR_UPDATE_DEFAULT_BIO"))
logger.critical(e) if os.getenv("DEBUG") == "true" else None
if not start:
start = True
text_start = ""
# Building a list of games for the start message. (Because why send a boring message when you can list all your games?)
for item in games:
game_name2 = item[0]
friendly_game_name2 = get_friendly_name(game_name2)
process_name2 = find_process_name(friendly_game_name2)
if any(process_name2 is not False and key.lower() == process_name2.lower() for key in process_name_mapping):
friendly_game_name2 = capitalize_first_letters(process_name_mapping[process_name2][0])
text_start += friendly_game_name2.replace("`", "").replace("_", "").replace("*", "") + "\n"
if len(text_start) > 3800:
text_start = text_start[:3800] + "..."
try:
await client.send_message("me", (os.getenv("START_MESSAGE").replace("#local_version", local_version)) + text_start, parse_mode="Markdown")
logger.info(os.getenv("DEBUG_START")) if os.getenv("DEBUG") == "true" else None
except Exception as e:
await client.log_out()
messagebox.showerror(os.getenv("ERROR"), os.getenv("CANT_CONNECT"))
logger.warning(os.getenv("ERROR_START_MESSAGE")) if os.getenv("DEBUG") == "true" else None
logger.critical(e) if os.getenv("DEBUG") == "true" else None
return handle_exit(None, None)
else:
friendly_game_name = get_friendly_name(game_name)
friendly_game_name_cap = capitalize_first_letters(process_name_mapping[find_process_name(friendly_game_name)][0])
if not start:
start = True
text_start = ""
for item in games:
game_name2 = item[0]
friendly_game_name2 = get_friendly_name(game_name2)
process_name2 = find_process_name(friendly_game_name2)
if any(process_name2 is not False and key.lower() == process_name2.lower() for key in process_name_mapping):
friendly_game_name2 = capitalize_first_letters(process_name_mapping[process_name2][0])
text_start += friendly_game_name2.replace("`", "").replace("_", "").replace("*", "") + "\n"
if len(text_start) > 3800:
text_start = text_start[:3800] + "..."
try:
await client.send_message("me", (os.getenv("START_MESSAGE").replace("#local_version", local_version)) + text_start, parse_mode="Markdown")
logger.info(os.getenv("DEBUG_START")) if os.getenv("DEBUG") == "true" else None
except Exception as e:
await client.log_out()
messagebox.showerror(os.getenv("ERROR"), os.getenv("CANT_CONNECT"))
logger.warning(os.getenv("ERROR_START_MESSAGE")) if os.getenv("DEBUG") == "true" else None
logger.critical(e) if os.getenv("DEBUG") == "true" else None
return handle_exit(None, None)
action_emoji = ""
if elapsed_time < 10:
action_emoji = ACTION_EMOJI_LESS_10_MIN
elif 9 < elapsed_time < 60:
action_emoji = ACTION_EMOJI_10_TO_60_MIN
elif 59 < elapsed_time < 120:
action_emoji = ACTION_EMOJI_60_TO_120_MIN
else:
action_emoji = ACTION_EMOJI_MORE_120_MIN
# Game names may be buggy, so we need to fix them. (I dont really want to deal with whole process names so this is the best I can do)
fixedGameName = game_name
for key, value in unedited_process_name_mapping.items():
if key == game_name:
fixedGameName = value[0]
# Stripping out annoying suffixes because who needs them anyway? (Game names are messy, but this is our band-aid fix)
new_status = (os.getenv("ACTION_STATUS").replace("#action_emoji", action_emoji).replace("#game_name", fixedGameName).replace("#elapsed_time", str(elapsed_time + 1))).replace(" (Steam)", "").replace(" (Non-Steam)", "").replace(" (x86)", "").replace(" (steam)", "").replace(" (non-steam)", "").replace(" (Retail)", "").replace(" (retail)", "").replace(" (Release)", "").replace(" (release)", "").replace(" (Dev)", "").replace(" (dev)", "").replace(" (x64)", "").replace(" (dx11)", "").replace(" (dx12)", "")
try:
await client(UpdateProfileRequest(about=new_status))
if playing_game != friendly_game_name_cap:
if notification_usernames:
try:
if notification_message_text_global is not None and notification_message_text_global.winfo_exists():
notification_message_template = notification_message_text_global.get("1.0", tk.END).strip()
else:
notification_message_template = notification_message_text_global_str
except Exception:
notification_message_template = notification_message_text_global_str
if not notification_message_template:
notification_message_template = os.getenv("NOTIFICATION_MESSAGE")
now = datetime.now()
current_time_str = now.strftime("%H:%M")
for username in notification_usernames:
first_name = await get_first_name(username)
if first_name != False:
notification_message = notification_message_template.replace("#game_name", friendly_game_name_cap).replace("#name", first_name).replace("#time", current_time_str)
try:
await client.send_message(username, notification_message)
logger.info(os.getenv("DEBUG_NOTIFICATION_SENT").replace("#name", first_name).replace("#game_name", friendly_game_name_cap)) if os.getenv("DEBUG") == "true" else None
except Exception as e:
logger.warning(os.getenv("ERROR_NOTIFICATION_FAILED").replace("#name", first_name).replace("#game_name", friendly_game_name_cap)) if os.getenv("DEBUG") == "true" else None
logger.critical(e) if os.getenv("DEBUG") == "true" else None
playing_game = friendly_game_name_cap
logger.info(os.getenv("DEBUG_PLAYING") + friendly_game_name_cap + os.getenv("DEBUG_PLAYTIME") + str(elapsed_time + 1)) if os.getenv("DEBUG") == "true" else None
except Exception as e:
messagebox.showerror(os.getenv("ERROR"), e)
logger.critical(e) if os.getenv("DEBUG") == "true" else None
root.quit()
sys.exit()
current_game = None
async def main(games):
"""
Continuously monitors a list of games and updates the status of the currently running game.
"""
global current_game
for proc in psutil.process_iter(['name', 'exe', 'username']):
if proc.info['name'] == 'python.exe':
try:
proc.nice(psutil.IDLE_PRIORITY_CLASS)
logger.debug(os.getenv("DEBUG_SET_LOW_PRIORITY")) if os.getenv("DEBUG") == "true" else None
break
except:
False
start_time = None
check_interval = int(os.getenv("INTERVAL_TIME"))
while True:
try:
await client.connect()
except:
pass
game_name = is_any_game_running(games)
if game_name:
if current_game != game_name:
if current_game:
log_game_end(current_game)
log_game_start(game_name)
current_game = game_name
start_time = time.time()
elapsed_time = int((time.time() - start_time) / check_interval)
await update_status(game_name, elapsed_time, games)
# Logging CPU and GPU usage because why not track everything? (Might as well know how much your PC hates you playing games)
cpu_usage = get_cpu_usage()
gpu_usage = get_gpu_usage()
if game_name in game_stats["daily"]:
game_stats["daily"][game_name]["avgCPUusage"] = cpu_usage if cpu_usage is not None else 1
game_stats["daily"][game_name]["avgGPUusage"] = gpu_usage if gpu_usage is not None else 1
_save_stats_to_file()
else:
if current_game:
log_game_end(current_game)
current_game = None
await update_status(False, False, games)
start_time = None
try:
await client.disconnect()
except:
pass
await asyncio.sleep(check_interval)
def start_monitoring(games):
"""
Starts the main event loop and creates a task to run the main application logic.
Args:
games (list): A list of game objects to monitor.
"""
loop = asyncio.get_event_loop()
loop.create_task(main(games))
loop.run_forever()
def remove_game(arg=None):
"""
Removes the selected game from the games_listbox and the added_games list.
If no game is selected, displays a warning message.
"""
selected_game = games_listbox.curselection()
if selected_game:
game_to_remove = games_listbox.get(selected_game)
games_listbox.delete(selected_game)
if game_to_remove in added_games:
added_games.remove(game_to_remove)
show_toast(os.getenv("GAME_DELETED"), duration=1000, is_hint=False)
logger.info(os.getenv("DEBUG_GAME_REMOVED") + " - " + game_to_remove) if os.getenv("DEBUG") == "true" else None
else:
show_toast(os.getenv("SELECT_GAME_TO_DEL"), duration=1000, is_hint=False)
logger.debug(os.getenv("DEBUG_DELETE_GAME")) if os.getenv("DEBUG") == "true" else None
def start_button_click():
"""
Starts the game monitoring process when the user clicks the start button.
This function is called when the user clicks the "Start" button in the GUI. It retrieves the list of games selected in the listbox, checks if the default biography is within the character limit, and then starts the game monitoring process. If no games are selected, it displays a warning message.
"""
games = [tuple(games_listbox.get(i).split(", ")) for i in range(games_listbox.size())]
if games:
global default_bio
global notification_usernames
global notification_message_text_global
default_bio = default_bio_text.get("1.0", tk.END).strip()
if len(default_bio) > 70:
messagebox.showerror(os.getenv("ERROR"), os.getenv("DEFAULT_BIO_MAX_LENGTH"))
logger.error(os.getenv("DEBUG_DEFAULT_BIO_IS_TOO_LONG") + " - " + default_bio) if os.getenv("DEBUG") == "true" else None
return
usernames_str = notification_usernames_entry.get()
if usernames_str != os.getenv("NOTIFICATION_USERNAMES_PLACEHOLDER"):
notification_usernames = [uname.strip() for uname in usernames_str.replace(',', ' ').split() if uname.strip()]
notification_message_text_global_str = notification_message_text.get("1.0", tk.END).strip()
game_stats["notification_usernames"] = notification_usernames
try:
game_stats["default_bio"] = default_bio
game_stats["notification_message"] = notification_message_text_global_str
except:
pass
_save_stats_to_file()
messagebox.showinfo(os.getenv("STARTED"), os.getenv("STARTED_MESSAGE"))
logger.info(os.getenv("STARTED_MESSAGE")) if os.getenv("DEBUG") == "true" else None
root.destroy()
try:
root.quit()
except:
pass
logger.info(os.getenv("CONSOLE_START_MESSAGE"))
start_monitoring(games)
else:
logger.warning(os.getenv("DEBUG_EMPTY_GAME_LIST")) if os.getenv("DEBUG") == "true" else None
messagebox.showwarning(os.getenv("WARNING"), os.getenv("ADD_AT_LEAST_ONE_GAME"))
def add_game_to_list(process_name, list_window):
"""
Adds a game to the list of added games.
Args:
process_name (str): The name of the game to be added.
list_window (tkinter.Toplevel): The window containing the list of games.
Returns:
callable: A function that can be called to add the game to the list.
"""
def add_to_list():
if process_name in added_games:
logger.debug(os.getenv("DEBUG_ALREADY_ADDED") + " - " + process_name) if os.getenv("DEBUG") == "true" else None
messagebox.showerror(os.getenv("ERROR"), os.getenv("ALREADY_ADDED"))
return
else:
logger.info(os.getenv("DEBUG_GAME_ADDED") + " - " + process_name) if os.getenv("DEBUG") == "true" else None
added_games.append(process_name)
games_listbox.insert(tk.END, process_name)
list_window.destroy()
return add_to_list
def add_placeholder(entry, placeholder):
"""
Adds a placeholder text to an entry widget and handles focus events to show/hide the placeholder.
Args: