-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
67 lines (60 loc) · 1.92 KB
/
monitor.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
import sys
import os
import subprocess
import re
import json
def get_active_window():
root = subprocess.Popen(
['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=subprocess.PIPE)
stdout, stderr = root.communicate()
m = re.search(b'^_NET_ACTIVE_WINDOW.* ([\w]+)$', stdout)
if m != None:
window_id = m.group(1)
window = subprocess.Popen(
['xprop', '-id', window_id, 'WM_NAME'], stdout=subprocess.PIPE)
stdout, stderr = window.communicate()
else:
return None
match = re.match(b"WM_NAME\(\w+\) = (?P<name>.+)$", stdout)
if match != None:
ret = match.group("name")
# print(type(ret))
ret = str(ret)
return ret.split("b'")[1].replace('"','').replace("'",'')
return None
def run():
act = data['activity']
new_window = None
current_window = get_active_window()
while(True):
if new_window != current_window:
print(str(current_window).split(" - "))
task = str(current_window).split(" - ")
if len(task)>2:
if task[-1] not in act:
act[task[-1]]={}
if task[-2] not in act[task[-1]]:
act[task[-1]][task[-2]]=[]
act[task[-1]][task[-2]].append(task[-3])
current_window = new_window
new_window = get_active_window()
def count_down():
count = 0
act = data['activity']
if 'Google Chrome' in act:
chrome = act['Google Chrome']
if 'Mozilla Firefox' in act:
fox = act['Mozilla Firefox']
if 'Wikipedia' in chrome:
count+=len(chrome['Wikipedia'])
if 'Wikipedia' in fox:
count+=len(fox['Wikipedia'])
return count
try:
data={}
data['activity']={}
run()
except KeyboardInterrupt:
with open('activity.json','w') as outfile:
json.dump(data,outfile)
print("\nTotal links hopped:"+str(count_down()))