Skip to content

Instantly share code, notes, and snippets.

@freckletonj
Created October 8, 2016 00:58
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save freckletonj/ff14386365e21cc7c3e19fd9bcb060f1 to your computer and use it in GitHub Desktop.
MyTime - Records a Log of My Active Window And Mouse Coordinates
"""
The MIT License (MIT)
Copyright (c) 2016 Josh Freckleton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------
MyTime - Active Window Logger
Purpose: Records my active window to a log continually forever
so I can big-data myself, and track how I spend my
time. Also records mouse X,Y so I can tell if I've
stepped away for a bit.
Note: There are a lot of security/privacy implications of this
code.
Also existential quandries if you find you're a crappy time
manager
Note: I use on: Python 2.7
Ubuntu
Probably will need some customization to get it to work on
other machines (ex: the `os` and `Xlib` libraries)
Note: I added the following alias to my `.bashrc` for convenience
alias mytime="cd ~/mytime; python2 mytime.py"
"""
import os
import datetime
from time import sleep
import sys
from Xlib import display
print("This will record your active window to a destination file")
print("and will stop if you enter:")
print("Ctrl + c")
dest = "mytime.csv" # destination file
freq = 10 # in seconds
def cleanUpStr(s):
"""removes superfluous starting text and carriage return,
may only apply to my computer's set up"""
return s[29:-2]
d = display.Display()
def whachaUpTo():
"runs an os command, probably linux-only, to find what window is focused"
return cleanUpStr(os.popen("xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) _NET_WM_NAME").read())
# I couldn't quickly get the following to work reliably,
# may be a better solution, but I already had a good'n.
#
# window = d.get_input_focus().focus
# wmname = window.get_wm_name()
# wmclass = window.get_wm_class()
# if wmclass is None and wmname is None:
# window = window.query_tree().parent
# wmname = window.get_wm_name()
# return wmname
mouse = d.screen().root
def wheresDaMouse():
"gets the mouse's coordinates"
a = mouse.query_pointer()._data
return [a["root_x"], a["root_y"]]
def now():
"return the datetime now, as string"
return datetime.datetime.now().isoformat()
while True:
try:
x = now()
y = whachaUpTo()
z = wheresDaMouse()
with open(dest, "a") as f:
f.write('"{}", "{}", "{}", "{}"\n'.format(x, y, z[0], z[1]))
print(x, y, z)
sleep(freq)
except KeyboardInterrupt:
print("\nGood Bye, your progress was saved to:\n")
print(dest)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment