-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus_bg.pyw
164 lines (141 loc) · 5.61 KB
/
focus_bg.pyw
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
import datetime as dt
import gzip
import itertools
import logging
import os
import sys
import time
from collections import namedtuple
from subprocess import PIPE, Popen
import psutil as ps
import ujson
if 'win32' in sys.platform:
import win32process
import win32gui
import win32api
UPDATE_TIME = 0.1 # seconds
AUTOSAVE_EVERY = 1000 # 1000*UPDATE_TIME
AFK_TIME = 300 # seconds
if 'linux' in sys.platform:
AFK_EXE = 'compiz' # which process to use as idle process
else:
AFK_EXE = 'C:\\Windows\\explorer.exe'
AFK_TITLE = 'AFK'
# windows containing any of these keywords will not set idle timer
AFK_IGNORE = list(
map(str.lower, [
'youtube', 'twitch', 'Media Player Classic', '.mp4', '.mov', '.mpg', '.avi', '.mkv', 'VLC',
'stdin', '127.0.0.1'
]))
Window = namedtuple('Window', 'pid name start_time last_update focus_time exe cmd')
def dump(data, file):
try:
if not os.path.exists(r'./logs/'):
os.mkdir('logs')
# replace datetime with string and save *.json.gz
write = [v._replace(focus_time=str(v.focus_time)) for v in data.values()]
with gzip.open(file, mode='wb') as f:
f.write(ujson.dumps(write).encode('utf-8'))
except PermissionError:
pass
def load(file):
if file.split('.')[-1] == 'gz':
with gzip.open(file) as f:
data = ujson.loads(f.read().decode('utf-8'))
else:
with open(file, encoding='utf-8') as f:
data = ujson.load(f)
return [Window(*v) for v in data]
def exception_hook(exc_type, exc_value, exc_traceback):
logger.error('\nUncaught exception hooked:', exc_info=(exc_type, exc_value, exc_traceback))
def logging_setup():
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('errlog.log', mode='a', encoding='utf-8')
fh.setLevel(logging.ERROR)
fh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(fh)
sys.excepthook = exception_hook
return logger
def get_process_data(process):
return (process.create_time(), process.exe(), process.cmdline())
if __name__ == '__main__':
logger = logging_setup()
filename = str(dt.datetime.now().strftime("%Y-%m-%d %H-%M-%S"))
windows = dict() # window : Window()
processes = dict() # pid : process
cur_time = dt.datetime.now()
afk = None # pid of afk process (when found)
segment = 0
prev_focus_title = None
win = None
pid = None
focus_title = None
for i in itertools.count():
try:
if 'linux' in sys.platform:
win = Popen(['xdotool', 'getactivewindow'], stdout=PIPE).communicate()[0]
if not win: continue
pid = int(Popen(['xdotool', 'getwindowpid', win], stdout=PIPE).communicate()[0])
focus_title = Popen(['xdotool', 'getwindowname', win], stdout=PIPE).communicate()[0]
focus_title = str(focus_title[:-1].decode('utf-8'))
else:
win = win32gui.GetForegroundWindow()
focus_title = win32gui.GetWindowText(win)
if not win: continue
pid = win32process.GetWindowThreadProcessId(win)[1]
if pid not in processes:
processes[pid] = ps.Process(pid)
proc_data = get_process_data(processes[pid])
except ps._exceptions.NoSuchProcess as e:
logger.exception(
'\nps.NoSuchProcess, pid=%r, win=%r, title=%r',
pid,
win,
focus_title,
exc_info=True)
time.sleep(1)
continue
except PermissionError:
#logger.exception('\nPermissionError, pid=%r, win=%r, title=%r', pid, win, focus_title, exc_info=True)
time.sleep(1)
continue
except ps._exceptions.AccessDenied:
time.sleep(1)
continue
except Exception as e:
logger.exception(
'\nException, pid=%r, win=%r, title=%r', pid, win, focus_title, exc_info=True)
time.sleep(1)
continue
if 'linux' in sys.platform:
last_input_time = int(Popen(['xprintidle'],
stdout=PIPE).communicate()[0]) # milliseconds
else:
last_input_time = win32api.GetTickCount() - win32api.GetLastInputInfo()
if last_input_time/1000 > AFK_TIME and afk:
if not any(keyword in focus_title.lower() for keyword in AFK_IGNORE):
# AFK mode started
pid = afk
focus_title = AFK_TITLE
proc_data = get_process_data(processes[pid])
if focus_title != prev_focus_title:
segment += 1
prev_focus_title = focus_title
if not afk and AFK_EXE in proc_data[1]:
afk = pid
window = (str(pid), str(focus_title), str(proc_data[0]), str(segment))
now = dt.datetime.now()
if window in windows:
new_focus_time = windows[window].focus_time + now - cur_time
windows[window] = windows[window]._replace(
focus_time=new_focus_time, last_update=str(now))
else:
windows[window] = Window(pid, focus_title, str(dt.datetime.fromtimestamp(proc_data[0])),
str(now), now - cur_time, proc_data[1], ' '.join(proc_data[2]))
cur_time = now
time.sleep(UPDATE_TIME)
if i % AUTOSAVE_EVERY == 0:
logger.debug('saving...')
dump(windows, './logs/{}.json.gz'.format(filename))