-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyt.py
51 lines (45 loc) · 1.83 KB
/
pyt.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
import subprocess
import psutil
import time
from datetime import datetime
import csv
import os
CATEGORIES = {
"firefox": "Productivity",
"chrome": "Productivity",
"code": "Development",
"slack": "Communication",
"spotify": "Entertainment",
}
def get_active_window():
try:
window_id = subprocess.check_output(["xdotool", "getactivewindow"]).strip().decode("utf-8")
window_name = subprocess.check_output(["xdotool", "getwindowname", window_id]).strip().decode("utf-8")
pid = subprocess.check_output(["xdotool", "getwindowpid", window_id]).strip().decode("utf-8")
process_name = psutil.Process(int(pid)).name()
category = CATEGORIES.get(process_name, "Uncategorized")
return window_name, process_name, category
except Exception as e:
return None, None, None
def get_idle_time():
idle_time = subprocess.check_output("xprintidle").strip().decode("utf-8")
return int(idle_time) / 1000
def log_usage(log_file, idle_threshold=300):
with open(log_file, 'a', newline='') as file:
writer = csv.writer(file)
while True:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
idle_time = get_idle_time()
if idle_time < idle_threshold:
window_title, app_name, category = get_active_window()
writer.writerow([timestamp, window_title, app_name, category, "Active"])
else:
writer.writerow([timestamp, "Idle", "Idle", "Idle", "Idle"])
time.sleep(5)
if __name__ == "__main__":
log_file = "logs/usage_log.csv"
if not os.path.isfile(log_file):
with open(log_file, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Timestamp", "Window Title", "Application Name", "Category", "Status"])
log_usage(log_file)