Skip to content

Instantly share code, notes, and snippets.

@derega

derega/whatyoudo.py

Created Feb 16, 2011
Embed
What would you like to do?
from subprocess import PIPE, Popen
import time
import codecs
import re
CLASS_RE = re.compile(r'WM_CLASS\(STRING\) = "([^"]+)", "([^"]+)"')
TITLE_RE = re.compile(r'WM_NAME\([A-Z_]+\) = "([^"]+)"')
def get_window_id():
root_check = ''
root = Popen(['xprop', '-root'], stdout=PIPE)
if root.stdout != root_check:
root_check = root.stdout
for i in root.stdout:
if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
return i.split()[4]
return None
def get_window_data(wid):
data = {}
if not wid:
return data
id_w = Popen(['xprop', '-id', str(wid)], stdout=PIPE)
for j in id_w.stdout:
m = TITLE_RE.search(j)
if m:
data['title'] = unicode(m.group(1).decode('utf-8'))
m = CLASS_RE.search(j)
if m:
data['class'] = m.group(1)
return data
if __name__ == '__main__':
previous_data = {}
previous_timestamp = int(time.time())
f = codecs.open('usage.log', 'a', encoding='utf-8', errors='ignore')
while True:
s = u''
data = get_window_data(get_window_id())
if data != previous_data:
timestamp = int(time.time())
if 'class' in previous_data:
s = u'%s' % int(timestamp-previous_timestamp)
s += u' %s' % previous_data['class'].replace(' ', '_')
if 'title' in previous_data:
s += u' %s' % previous_data['title']
# print s
f.write(s)
f.write('\n')
f.flush()
previous_data = data
previous_timestamp = timestamp
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment