Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extract data from selfspy and append it as a row to google docs
import re
from datetime import timedelta
regex = re.compile(r'((?P<hours>\d+?)h)? ((?P<minutes>\d+?)m)? ((?P<seconds>\d+?)s)?')
regex2 = re.compile(r'((?P<minutes>\d+?)m)? ((?P<seconds>\d+?)s)?')
def parse_time(time_str):
parts = regex.match(time_str)
if not parts:
parts = regex2.match(time_str)
parts = parts.groupdict()
time_params = {}
for (name, param) in parts.iteritems():
if param:
time_params[name] = int(param)
return timedelta(**time_params)
from fabric.operations import local
from datetime import datetime,timedelta
from time import strptime
import time
res = local('selfstats --back 24 h --pactive 25', capture=True).splitlines()
programs = res[5:]
total_active = parse_time(res[2].split(': ')[1])
apps = {'productive': ['Gvim', 'Dreampie', 'Gnome-terminal'], 'research': ['Firefox'], 'wasted': ['Google-chrome']}
times = {} # Non-disjoint.
for app in apps.items():
times[app[0]] = 0
for p in programs:
p_name = p.split(', ')[0]
p_time = p.split(', ')[1]
if p_name in app[1]:
times[app[0]] += parse_time(p_time).total_seconds()
times[app[0]] = str(times[app[0]])
res = local('selfstats --back 24 h --tactive 25', capture=True).splitlines()
top_titles = res[5:10]
data = {'totalactive': str(total_active.total_seconds())}
data.update(times)
data['date'] = time.strftime( "%Y-%m-%d" )
data['programs'] = ', '.join([x.split(',')[0] for x in programs[0:6]])
data['toptitles'] = ', '.join( filter(lambda x: len(x) > 0, [x.split(',')[0] for x in top_titles]))
res = local('selfstats --back 24 h --ratios', capture=True).splitlines()
line1 = res[0].split(' ')
data['keystrokes'] = line1[0]
data['keysequences'] = line1[3]
data['mouseactions'] = line1[6]
data['mouseclicks'] = line1[8].strip('(')
data['mousemoves'] = line1[11]
data['keyclickratio'] = res[4].split(' ')[3]
data['activesecondskeysratio'] = res[5].split(' ')[4]
data['mousemoveskeysratio'] = res[7].split(' ')[4]
data['mousemovesclicksratio'] = res[8].split(' ')[4]
######################################################
# UPLOAD THE DATA
######################################################
import gdata.spreadsheet.service
email = '****@gmail.com'
password = '******'
# Find this value in the url with 'key=XXX' and copy XXX below
spreadsheet_key = 'KEY_HERE'
# All spreadsheets have worksheets. I think worksheet #1 by default always
# has a value of 'od6'
worksheet_id = 'od6'
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
# Prepare the dictionary to write
print data
entry = spr_client.InsertRow(data, spreadsheet_key, worksheet_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
print "Insert row succeeded."
else:
print "Insert row failed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.