~rescuetime-developers/rescuetime-linux-uploader/trunk

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
"""
Created by Angus Helm

This is a moniter/uploader for rescuetime.com on Linux.  Once started, it periodically checks the focused window to see what program you are using.  It then uploads that data to rescuetime.com every half-hour, so that you can view how you spend your time.

"""

import os
import sys
import subprocess
import datetime
import signal
import time
import pwd
import getpass
import ctypes
from getopt import gnu_getopt
import re
import traceback
from pkg_resources import resource_filename
import gnomekeyring as gkey
import socket
socket.setdefaulttimeout(15)
import urllib
import urllib2

import RescueTimeUploader.options
import RescueTimeUploader.dirstructure

RESCUETIME_API_URL = "https://api.rescuetime.com/api/"

if os.getenv('http_proxy') != None or os.getenv('https_proxy') != None:
	proxy_support = urllib2.ProxyHandler()
	opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
	urllib2.install_opener(opener)

USER_AGENT = 'RescueTimeLinuxUploader/0.91 +https://launchpad.net/rescuetime-linux-uploader'

xlib = None
try:
	xlib = ctypes.cdll.LoadLibrary( 'libX11.so.6')
except OSError:
	print "Could not load libX11.so.6"
	print "Idle time tracking will not be functional"
	##out.flush()

xss = None
try:
	if xlib != None:
		xss = ctypes.cdll.LoadLibrary( 'libXss.so.1')
except OSError:
	print "Could not load libXss.so.1"
	print "Idle time tracking will not be functional"
	##out.flush()

if xlib != None and xss != None:
        xlib.XOpenDisplay.restype = ctypes.c_void_p
        xlib_dpy = xlib.XOpenDisplay( os.environ['DISPLAY'])
        if xlib_dpy is not None:
            xlib.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
            xlib_root = xlib.XDefaultRootWindow( xlib_dpy)
        else:
            print( "Could not connect to DISPLAY." )
            print( "Idle time tracking will not be functional" )


# This is messing with rather private data, so don't let anyone read it
os.umask(0066)

os_name = os.uname()[1] + "\\" + pwd.getpwuid(os.geteuid())[0]

# Used for getting idle time
class XScreenSaverInfo( ctypes.Structure):
  """ typedef struct { ... } XScreenSaverInfo; """
  _fields_ = [('window',      ctypes.c_ulong), # screen saver window
              ('state',       ctypes.c_int),   # off,on,disabled
              ('kind',        ctypes.c_int),   # blanked,internal,external
              ('since',       ctypes.c_ulong), # milliseconds
              ('idle',        ctypes.c_ulong), # milliseconds
              ('event_mask',  ctypes.c_ulong)] # events

def escape_less(data):
	return data.replace("\000", "").replace("'", "`")

class time_obj(object):
	"""
	A structure that holds information about a slice of time, and the 
	window/app that was focused at that time.
	"""

	def __init__(self, options, dir_structure):
		"""
		Creates a new object, initializing its start_time, app_name, 
		and window_title properties based on the current time and 
		focused application.
		"""
		self.options = options
		self.dir_structure = dir_structure
		self.start_time = datetime.datetime.today()
		self.app_name = self.get_active_application()
		self.window_title = self.get_active_window_title()

		self.end_time = None
		extended_info = self.get_extended_info()
		print self.app_name, "(" + str(self.window_title) + ")", "now focused", "(Extended info: " + str(extended_info) + ")"
		##out.flush()
		if extended_info != None:
			self.extended_info = extended_info
	
	def __str__(self):
		if hasattr(self, "extended_info"):
			return "(%s: %s %is)" % (self.app_name, self.extended_info, self.get_duration())
		return "(%s: %is)" % (self.app_name, self.get_duration())
	
	def __repr__(self):
		return self.__str__()
	
	def get_duration(self):
		if self.end_time == None:
			return (datetime.datetime.today() - self.start_time).seconds
		else:
			return (self.end_time - self.start_time).seconds
	
	def get_extended_info(self):
		"""
		Tries to get any extended info available for the application
		stored in self.app_name.  Returns None if there is none 
		available for that application.
		"""
		if self.app_name == None:
			return None

		try:
			extension_name = resource_filename(__name__,  
			     "extensions/%s/extended_info" % self.app_name)
		except KeyError:
			return None

		try:
			os.stat(extension_name)
		except OSError:
			return None

		send_to = '%s\n%s\n' % (self.app_name, 
		    self.get_active_window_title())

		if self.options.debug:
			print "Getting extended info from", extension_name
			print "send-to:", send_to
			##out.flush()
		try:
			runner = subprocess.Popen(extension_name, shell=True, 
				 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
			get_back = runner.communicate(send_to)[0][:-1]
			if self.options.debug:
				print "get_back:", get_back
				##out.flush()
			return get_back
		except:
			return None
	
	def _get_idle_time(self):
		if xlib == None or xss == None or xlib_dpy == None or xlib_root == None:
			return 0
		xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
		xss_info = xss.XScreenSaverAllocInfo()
		xss.XScreenSaverQueryInfo.argtypes = [ctypes.c_void_p,
			ctypes.c_ulong, ctypes.POINTER(XScreenSaverInfo)]
		xss.XScreenSaverQueryInfo( xlib_dpy, xlib_root, xss_info)
		return xss_info.contents.idle / 1000
	
	def _get_raw_active_application(self):
		idle_time = self._get_idle_time()
		if self.options.debug:
			print "idle-time:", idle_time 
			print "max-idle-minutes:", self.options.max_idle_minutes
			##out.flush()
		if idle_time > (self.options.max_idle_minutes * 60):
			return None
		try:
			exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/WM_CLASS/ {print $2; exit;}'| awk '{print $2}' | sed 's/^"//g' | sed 's/"$//g'"""
			runner = subprocess.Popen(exec_cmd, shell=True, 
				 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
			runner_res = runner.communicate()[0][:-1]
			if not runner_res.isalnum():
				exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/^WM_NAME/ {print $2; exit;}' | sed -e's/^ *"//g' | sed -e's/\"$//g'"""
				runner = subprocess.Popen(exec_cmd, shell=True,
	                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
				runner_res = runner.communicate()[0][:-1]
			return runner_res

		except:
			if self.options.debug:
				raise
			return None
	
	def get_active_application(self):
		"""
		Returns the application name of the focused application, such as
		Firefox or Gnome-terminal.  Sometimes returns None.
		"""
		if not self.options.tracking:
			return None

		if self.options.force_active_application != None:
			return self.options.force_active_application
		try:
			app_name = self._get_raw_active_application() 
			# Extensions can override the application name
			try:
				extension_name = resource_filename(__name__,  
				     "extensions/%s/app_name" % app_name)
			except KeyError:
				return unicode(app_name).encode('utf8')
			try:
				os.stat(extension_name)
			except OSError:
				return unicode(app_name).encode('utf8')
			send_to = '%s\n%s\n' % (app_name, self.get_active_window_title())
			if self.options.debug:
				print "Getting application name from",
				print extension_name
				print "send_to:", send_to
				##out.flush()
			runner = subprocess.Popen(extension_name, 
				 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
			app_name = runner.communicate(send_to)[0][:-1]
			if self.options.debug:
				print "app_name:", app_name
				##out.flush()

			return unicode(app_name).encode('utf8')
		except:
			if self.options.debug:
				traceback.print_exc()
			return None

	def still_active(self):
		"""
		Returns True if the application that is active is the same as
		the one that was active when this object was first created.
		Checks the application name and the window title.
		"""
		if self.app_name != self.get_active_application():
			return False
		if self.window_title != self.get_active_window_title():
			if self.window_title != "":
				return False
		if hasattr(self, 'extended_info'):
			if self.extended_info != self.get_extended_info():
				return False
		return True
	
	def close(self):
		"""
		Assigns this object's end_time parameter to the current time.
		"""
		self.end_time = datetime.datetime.today()
	
	encoded_title_re = re.compile("(0x[0-9abcdefABCDEF]*,* *)+$")
	def _get_raw_window_title(self):
		exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/^WM_NAME/ {print $2; exit;}' | sed -e's/^ *"//g' | sed -e's/\\"$//g'"""
		try:
			runner = subprocess.Popen(exec_cmd, shell=True, 
				 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
			app_name = runner.communicate()[0][:-1].strip()
			if time_obj.encoded_title_re.match(app_name) != None:
				app_name = self._decode_window_title(app_name)
			return app_name
		except:
			if self.options.debug:
				raise
			return None


	def get_active_window_title(self):
		"""
		Returns the window title of the focused application, or None sometimes.
		"""
		return self._get_raw_window_title()

	def _decode_window_title(self, title):
		wordz = title.split(" ")
		words = []
		for word in wordz:
			if word.endswith(","):
				words.append(word[2:-1])
			else:
				words.append(word[2:])
		total_str = ""
		for word in words:
			total_str += chr(int(word, 16))
		return total_str
	
	def print_out(self,id):
		"""
		Prints out a yaml version of this object, suitable for 
		uploading to rescuetime.com  The id should be an integer that
		is different for each object printed out.
		"""
		try:
			if self.app_name == None or self.app_name.isspace() or len(self.app_name) == 0 or self.app_name.lower().strip() == "none":
				return ""
			out =  "- os_username: '%s'\n" % os_name
			out += "  app_name: '%s'\n" % escape_less(str(self.app_name))
			if self.window_title == None or self.window_title.isspace() or len(self.window_title) == 0:
				out += "  window_title: 'Untitled Window'\n"
			else:
				out += "  window_title: '%s'\n" % escape_less(str(self.window_title))
			if hasattr(self, "extended_info"):
				out += "  extended_info: '%s'\n" % escape_less(str(self.extended_info))
			else:
				out += "  extended_info: ''\n"
			out += '  start_time: %s\n' % re.sub("\..*","", str(self.start_time))
			out += '  end_time: %s\n' % re.sub("\..*","", str(self.end_time))
			return out
		except:
			traceback.print_exc()


def handle_signal(a , b):
	"""
	A function suitable for acting as a signal handler.

	Prepares the latest data for uploading, then quits.  Does NOT upload
	anything.
	"""
	sys.exit(0)

class Uploader(object):
	def __init__(self, headless=False):
		#print "starting init!"
		##out.flush()
		self.dir_structure = RescueTimeUploader.dirstructure.DirStructure()
		self.options = RescueTimeUploader.options.Options(self.dir_structure)

		signal.signal(signal.SIGTERM, handle_signal)
		signal.signal(signal.SIGHUP, handle_signal)

		self.active = True

		self.email_address, self.password = self.retrieve_credentials()

                # TODO: Regarding headclass, we should split out the command
                # line client from the GUI and have a pure uploader library
		if (not self.email_address or not self.password) and not headless:
			while True:
				self.email_address = raw_input("Your rescuetime username: ")
				self.password = getpass.getpass("Your rescuetime password: ")
				if not self.test_login(self.email_address, self.password):
					print "Login failed."
					##out.flush()
				else:
					break
			self.options.save_preferences(self.email_address, self.password)

		self.time_objs = []
		self.archived_time_objs = []

		f = file(self.dir_structure.next_upload_file)
		self.next_upload = int(f.readline())
		f.close()
		#print "finished init!"
		##out.flush()
	
	
	def get_latest_time_obj(self):
		f = file(os.path.join(self.dir_structure.tmp_dir, "notifier.debuglog"), "a")
		f.write("---- STARTING CHECK ----\n")
		f.write("archived_time_objs: %s\n" % self.archived_time_objs)
		min_delay = self.options.applet_notifier_minimum_seconds
		f.write("min_delay: %i\n" % min_delay)
		if len(self.archived_time_objs) == 0:
			f.write("no self.time_objs; returning none\n")
			f.close()
			return None

		latest_time_obj = self.archived_time_objs[-1]
		for timeobj in reversed(self.archived_time_objs[:-1]):
			f.write("timeobj: %s\n" % timeobj)
			if timeobj.app_name != latest_time_obj.app_name:
				f.write("app_name wrong\n")
				continue
			if hasattr(latest_time_obj, 'extended_info') and not hasattr(timeobj, 'extended_info'):
				f.write("No extended info on timeobj")
				continue
			if not hasattr(latest_time_obj, 'extended_info') and hasattr(timeobj, 'extended_info'):
				f.write("No extended info on latest_time_obj")
				continue
			if hasattr(latest_time_obj,'extended_info') and hasattr(timeobj,'extended_info')\
			    and timeobj.extended_info != latest_time_obj.extended_info:
				f.write("extended_info wrong\n")
			    	continue
			duration_delta = latest_time_obj.start_time - timeobj.end_time
			if duration_delta.seconds > min_delay:
				f.write("duration: %i too great\n" %duration_delta.seconds)
				break
			latest_time_obj = timeobj
		f.write("Returning %s\n" % latest_time_obj)
		f.close()
		return latest_time_obj
	
	def retrieve_credentials(self):
		f = file(self.dir_structure.preference_file)
		pref_data = f.readlines()
		f.close()

		password = False
		email_address = False

		for line in pref_data:
			LL = line.split(":")
			if LL[0].strip() == "email":
				email_address = LL[1].strip()
		try:
			gkey.get_default_keyring_sync()
			attrs = { "server": "http://www.rescuetime.com",
				  "user": email_address}
			items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
			password =  items[0].secret
		except:
			pass
		return (email_address, password)

	def shutdown(self):
		print "shutting down..."
		##out.flush()
		if not self.active:
			return
		try:
                        # TODO: Why do we save preferences loaded just before?
			email_address, password = self.retrieve_credentials()
			self.options.save_preferences(email_address, password)
		except:
			print "Failed to save preferences on shutdown."
			##out.flush()
		try:
			self.time_objs[-1].close()
			self.prepare_data(self.time_objs)
		except:
			print "Failed to prepare data on shutdown."
			##out.flush()
		try:
			for filename in os.listdir(self.dir_structure.tmp_dir):
				os.unlink(os.path.join(
				    self.dir_structure.tmp_dir,
				    filename))
		except OSError:
			print "Failed to clean out temp dir:",
			print self.dir_structure.tmp_dir
			##out.flush()
		self.active = False
		sys.exit(0)
	
	def main(self):
		"""
		Goes into a loop, monitering the status of the focused window,
		and periodically uploading all the data it gains.
		"""
		print "Main() started"
		print "Python version:", sys.version
		##out.flush()
		try:
			os.stat(self.dir_structure.lock_file)
			f = file(self.dir_structure.lock_file)
			pid = int(f.read())
			f.close()

			exec_str = "ps -A | grep %i" % pid
			runner = subprocess.Popen(exec_str, shell=True, 
			                          stdin=subprocess.PIPE,
						  stdout=subprocess.PIPE)
			anything = runner.communicate()[0]
			if len(anything) != 0 and not anything.isspace():
				print "Uploader already running: pid", pid
				print "Remove", self.dir_structure.lock_file,
				print "to override."
				##out.flush()
				sys.exit(1)
		except OSError:
			pass
		f = file(self.dir_structure.lock_file, "w")
		f.write(str(os.getpid()))
		f.close()
		try:
			self.archived_time_objs = []
			self.time_objs = []
			self.time_objs.append(time_obj(self.options, 
			                               self.dir_structure))
			self.archived_time_objs.append(self.time_objs[-1])


			next_upload_time = datetime.datetime.today() + self.options.upload_interval

			try:
				while True:
					# If the focused window has changed
					if not self.time_objs[len(self.time_objs) - 1].still_active():
						self.time_objs[len(self.time_objs) - 1].close()
						self.time_objs.append(time_obj(self.options, self.dir_structure))
						self.archived_time_objs.append(self.time_objs[-1])
					# Check if it has been 30 minutes yet
					if datetime.datetime.today() > next_upload_time:
						self.prepare_data(self.time_objs[:-1])
						self.upload_data()
						self.time_objs = [self.time_objs[-1]]
						next_upload_time = datetime.datetime.today() + self.options.upload_interval
					# Sleep for a few seconds
					time.sleep(self.options.scan_interval_seconds)
			# Aborted by keyboard
			except KeyboardInterrupt:
				self.time_objs[-1].close()
				self.prepare_data(self.time_objs)
				#if "y" == raw_input("Upload the latest data? y/n: ").lower()[0]:
				#	self.upload_data()
		finally:
			self.shutdown()
	
	def test_connection(self):
		try:
			request = urllib2.Request(RESCUETIME_API_URL + "handshake")
			request.add_header('User-agent', USER_AGENT)
			urllib2.urlopen(request)
		except urllib2.HTTPError:
			traceback.print_exc(0)
			return False
		except urllib2.URLError:
			traceback.print_exc(0)
			return False
		return True
	
	def test_login(self, email_address, password):
		print "Logging in..."
		if not self.test_connection():
			print "Connection is down.  Pretending we succeeded in logging in."
			return True
		##out.flush()
		login_data = urllib.urlencode([("email", email_address),("password", password)])
		login_url = RESCUETIME_API_URL + "handshake"
		try:
			request = urllib2.Request(login_url)
			request.add_header('User-agent', USER_AGENT)
			ret = urllib2.urlopen(request, login_data)
		except urllib2.HTTPError:
			traceback.print_exc(0)
			return False
		except urllib2.URLError:
			traceback.print_exc(0)
			return False
		login_status = "".join(ret.readlines())
		print login_status
		##out.flush()
                self.email_address = email_address
                self.password = password
		return "login success!" in login_status.lower()


	def upload_data(self):
		"""
		Attempts to upload all the data files stored in PREPARED_DIR.  Moves
		any that are successful to FINISHED_DIR, and any that fail to
		FAILED_DIR.
		"""
		print "Submitting data..."
		##out.flush()

		#email_address, password = self.retrieve_credentials()

		if not self.test_login(self.email_address, self.password):
			print "It looks like the login failed."
			print "Please edit your password and username in", self.dir_structure.preference_file
			##out.flush()
			return

		print "Uploading data..."
		##out.flush()
		for prepared_file in os.listdir(self.dir_structure.prepared_dir):
			finished_name = os.path.join(self.dir_structure.finished_dir, prepared_file)
			old_name = os.path.join(self.dir_structure.prepared_dir, prepared_file)
			failed_name = os.path.join(self.dir_structure.failed_dir, prepared_file)
			print "Uploading", prepared_file, "..."
			##out.flush()
			submit_url = RESCUETIME_API_URL + "userlogs"
			f = file(old_name)
			submit_data = urllib.urlencode([("email",
                            self.email_address), ("password", self.password), ("yamldata", "".join(f.readlines()))])
			f.close()
			try:
				request = urllib2.Request(submit_url)
				request.add_header('User-agent', USER_AGENT)
				ret = urllib2.urlopen(request, submit_data)
			except urllib2.HTTPError:
				print "Error uploading."
				##out.flush()
				continue
			except urllib2.URLError:
				print "Error uploading."
				##out.flush()
				continue
			returned_data = "".join(ret.readlines())
			print returned_data
			##out.flush()
			f = file(os.path.join(self.dir_structure.reply_dir, prepared_file), "w")
			f.write(returned_data)
			f.close()
			if not "error" in returned_data:
				os.rename(old_name, finished_name)
			else:
				os.rename(old_name, failed_name)

		print "Done submitting data."
		##out.flush()

	def prepare_data(self, time_objs):
		"""
		Prepares the given data to be uploaded, and stores the prepared
		data in PREPARED_DIR.  Does not upload the data.
		"""
		i = 0
		#data = '<NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n'
		data = '---\n'
		for to in time_objs:
			data += to.print_out(i)
			i += 1
		#data += "</NewDataSet>"

		# Save the uploaded data
		if self.options.upload:
			f = file(os.path.join(self.dir_structure.prepared_dir, str(self.next_upload) + ".upload.encoded.data"), "w")
			f.write(data)
			f.close()
		
			self.next_upload += 1
			f = file(self.dir_structure.next_upload_file, "w")
			f.write(str(self.next_upload))
			f.close()
#print "finished initing module!"
#out.flush()