#!/usr/bin/python # Poll XBMC and write a file containing how long it's been idle for # # 2 clause BSDL (C) Daniel O'Connor # import fcntl import os import sys import time import urllib2 debug = False idlefilepath = '/home/myth/.xbmcidletime' xbmcurl = "http://127.0.0.1:8080/xbmcCmds/xbmcHttp?command=getcurrentlyplaying" looptime = 5 xbmcIdletime = 0 try: idlefile = open(idlefilepath, 'w') except IOEerror, e: print "Unable to open idle file: " + str(e) sys.exit(1) try: fcntl.flock(idlefile, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError, e: print "Unable to lock file: " + str(e) sys.exit(1) os.ftruncate(idlefile.fileno(), 0) while True: time.sleep(looptime) # check XBMC for activity: xbmcRequest = urllib2.Request(xbmcurl) try: handle = urllib2.urlopen(xbmcRequest) except urllib2.URLError, e: if debug: print "Error talking to XBMC assuming idle: " + str(e) xbmcIdletime += looptime else: page = handle.read() if page.find('Filename:[Nothing Playing]') >= 0: if debug: print "Nothing Playing" xbmcIdletime += looptime elif page.find('PlayStatus:Playing') >= 0: if debug: print "XBMC is playing" xbmcIdletime = 0 elif page.find('PlayStatus:Paused') >= 0: if debug: print "XBMC is paused" xbmcIdletime += looptime if debug: print "idle time now " + str(xbmcIdletime) os.ftruncate(idlefile.fileno(), 0) idlefile.seek(0, 0) idlefile.write(str(xbmcIdletime) + '\n') idlefile.flush()