Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- import signal
- import subprocess
- import re
- import xbmc
- import xbmcaddon
- import xbmcgui
- import threading
- import time, datetime
- import commands
- import urllib2
- from xml.dom import minidom
- #get actioncodes from keymap.xml
- ACTION_PREVIOUS_MENU = 10
- TVHEADEND_URL = 'http://localhost:9981/status.xml'
- LOG_FILE = '/home/xbmc/.xbmc/temp/pvruptime.log'
- WAITBEFORESHUTDOWN = 120
- WAKEBEFORERECORDING = 0 #300
- class MyClass(xbmcgui.Window):
- def __init__(self):
- self.strActionInfo = xbmcgui.ControlLabel(100, 120, 1600, 400, '')
- self.addControl(self.strActionInfo)
- self.strActionInfo.setLabel('Determining TV recording status before shutting down...')
- self.button0 = xbmcgui.ControlButton(350, 500, 300, 50, "Return to XBMC")
- self.addControl(self.button0)
- self.button1 = xbmcgui.ControlButton(750, 500, 300, 50, "Shutdown XBMC")
- self.addControl(self.button1)
- self.setFocus(self.button0)
- self.button0.controlRight(self.button1)
- self.button0.controlLeft(self.button1)
- self.button1.controlRight(self.button0)
- self.button1.controlLeft(self.button0)
- self.timerMsg = threading.Timer(2, self.checkRecording)
- self.timerMsg.start()
- def onAction(self, action):
- if action == ACTION_PREVIOUS_MENU:
- self.timerMsg.cancel()
- self.close()
- def onControl(self, control):
- if control == self.button0:
- self.timerMsg.cancel()
- self.close()
- if control == self.button1:
- self.message('Are you sure you want to force a shutdown? You may have active recordings.')
- self.timerMsg.cancel()
- def message(self, message):
- dialog = xbmcgui.Dialog()
- if dialog.yesno("PVRUptime", message):
- self.close()
- def findThisProcess(self, process_name ):
- ps = subprocess.Popen("ps -eaf | grep "+process_name, shell=True, stdout=subprocess.PIPE)
- output = ps.stdout.read()
- ps.stdout.close()
- ps.wait()
- return output
- def writeToLogFile(self, message):
- if os.path.isfile(LOG_FILE) == False:
- logFile = open(LOG_FILE, 'w')
- else:
- logFile = open(LOG_FILE, 'r+')
- logFile.seek(0,2)
- logFile.write(message +'\n\n')
- logFile.close()
- def checkProcess(self, processname):
- processExists = "False"
- processList = os.popen("ps xa")
- if processList:
- for line in processList:
- fields = line.split()
- pid = fields[0]
- process = fields[4]
- if process.find(processname) > 0:
- processExists = "True"
- break
- return processExists
- def checkRecording(self):
- closestRecordingDateTimeStart = None
- closestRecordingDateTimeEnd = None
- closestRecordingTitle = None
- currentlyRecording = "False"
- difference = None
- UTCTimeDelta = datetime.timedelta(hours=8)
- currentTime = datetime.datetime.now()
- dialog = xbmcgui.Dialog()
- password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- password_mgr.add_password(None, TVHEADEND_URL, 'xbmc', 'xxxxx')
- handler = urllib2.HTTPBasicAuthHandler(password_mgr)
- opener = urllib2.build_opener(handler)
- opener.open(TVHEADEND_URL)
- urllib2.install_opener(opener)
- f = urllib2.urlopen(TVHEADEND_URL)
- link = f.read()
- xmldoc = minidom.parseString(link)
- f.close()
- self.writeToLogFile('starting the process')
- subscriptionnode = xmldoc.getElementsByTagName('subscriptions')[0]
- numsubscriptions = subscriptionnode.childNodes[0].data
- # DEBUGGING OUTPUT
- self.writeToLogFile('we have determined num subscriptions = ' + numsubscriptions)
- if numsubscriptions == "0":
- # DEBUGGING OUTPUT
- self.writeToLogFile('num subscriptions = 0 and we made it into the if statement')
- # New code -------------------------------------------------
- nextRecord = xmldoc.getElementsByTagName('next')
- if nextRecord:
- minutesUntilNextRecording = int(nextRecord[0].childNodes[0].data)
- closestRecordingDateTimeStart = currentTime + datetime.timedelta(minutes=minutesUntilNextRecording)
- else:
- closestRecordingDateTimeStart = None
- # End new code ---------------------------------------------
- # DEBUGGING OUTPUT
- #self.writeToLogFile('minutesUntilNextRecording = ' + str(minutesUntilNextRecording))
- #minutesUntilNextRecording = int(xmldoc.getElementsByTagName('next')[0].childNodes[0].data)
- #closestRecordingDateTimeStart = currentTime + datetime.timedelta(minutes=minutesUntilNextRecording)
- # DEBUGGING OUTPUT
- self.writeToLogFile('closestRecordingDateTimeStart = ' + str(closestRecordingDateTimeStart))
- elif numsubscriptions != "0":
- for recordingnode in xmldoc.getElementsByTagName('recording'):
- nextRecordingDateTimeStart = datetime.datetime.fromtimestamp(float(recordingnode.getElementsByTagName('start')[0].getElementsByTagName('unixtime')[0].childNodes[0].data)) - datetime.timedelta(minutes=5)
- nextRecordingDateTimeStartUTC = nextRecordingDateTimeStart
- nextRecordingDateTimeEnd = datetime.datetime.fromtimestamp(float(recordingnode.getElementsByTagName('stop')[0].getElementsByTagName('unixtime')[0].childNodes[0].data)) + datetime.timedelta(minutes=15)
- nextRecordingDateTimeEndUTC = nextRecordingDateTimeEnd
- nextRecordingTitle = recordingnode.getElementsByTagName('title')[0].childNodes[0].data
- #self.strActionInfo.setLabel(nextRecordingTitle)
- self.strActionInfo.setLabel(str(nextRecordingDateTimeStartUTC) + ", " + str(nextRecordingDateTimeEndUTC) +", " + str(currentTime))
- if nextRecordingDateTimeStartUTC < nextRecordingDateTimeEndUTC:
- if nextRecordingDateTimeStartUTC > currentTime:
- if currentlyRecording == "False":
- if closestRecordingDateTimeStart is None:
- closestRecordingDateTimeStart = nextRecordingDateTimeStartUTC
- closestRecordingDateTimeEnd = nextRecordingDateTimeEndUTC
- closestRecordingTitle = nextRecordingTitle
- else:
- if closestRecordingDateTimeStart > nextRecordingDateTimeStartUTC:
- closestRecordingDateTimeStart = nextRecordingDateTimeStartUTC
- closestRecordingDateTimeEnd = nextRecordingDateTimeEndUTC
- closestRecordingTitle = nextRecordingTitle
- #else:
- #print "Ignoring ", nextRecordingTitle, " as there is a current recording ", closestRecordingTitle
- else:
- if nextRecordingDateTimeEndUTC > currentTime:
- if currentlyRecording == "False":
- closestRecordingDateTimeStart = nextRecordingDateTimeStartUTC
- closestRecordingDateTimeEnd = nextRecordingDateTimeEndUTC
- closestRecordingTitle = nextRecordingTitle
- currentlyRecording = "True"
- else:
- closestRecordingTitle = closestRecordingTitle + "\n" + nextRecordingTitle
- if nextRecordingDateTimeEndUTC > closestRecordingDateTimeEnd:
- closestRecordingDateTimeStart = nextRecordingDateTimeStartUTC
- closestRecordingDateTimeEnd = nextRecordingDateTimeEndUTC
- #else:
- #print "completed recording", nextRecordingTitle
- if currentlyRecording == "True":
- difference = closestRecordingDateTimeEnd - currentTime
- outputMessage = "Currently recording \n" + closestRecordingTitle + "\nfor another " + str(difference.seconds/60) + " minutes \nuntil " + str(closestRecordingDateTimeEnd)
- self.writeToLogFile(outputMessage)
- self.strActionInfo.setLabel(outputMessage)
- self.timerMsg = threading.Timer(5, self.checkRecording)
- self.timerMsg.start()
- else:
- if closestRecordingDateTimeStart is None:
- # by default, set wake up for 4 hours time if no new recordings are scheduled and we're not recording now
- closestRecordingDateTimeStart = currentTime + datetime.timedelta(hours=4)
- # DEBUGGING OUTPUT
- self.writeToLogFile('closestRecordingDateTimeStart now reset from None = ' + str(closestRecordingDateTimeStart))
- difference = closestRecordingDateTimeStart - currentTime
- # DEBUGGING OUTPUT
- self.writeToLogFile('difference = ' + str(difference))
- self.writeToLogFile('difference.seconds = ' + str(difference.seconds))
- # determine if the next recording is > 2 minutes away
- if difference.seconds > WAITBEFORESHUTDOWN:
- wakeupDateTimeUTC = closestRecordingDateTimeStart - datetime.timedelta(seconds=WAKEBEFORERECORDING)
- outputMessage = "Next recording more than " + str(WAITBEFORESHUTDOWN/60) + " minutes away \nshutting down with restart in " + str(difference.seconds/60) + " minutes, at " + str(wakeupDateTimeUTC + UTCTimeDelta)
- self.writeToLogFile(outputMessage)
- self.writeToLogFile('Wakeup time is: ' + str(time.mktime(wakeupDateTimeUTC.timetuple())))
- self.strActionInfo.setLabel(outputMessage)
- wakeupCommand = "sudo /usr/share/xbmc/addons/script.pvruptime/shutdown.sh " + str(time.mktime(wakeupDateTimeUTC.timetuple()))
- self.strActionInfo.setLabel(wakeupCommand)
- os.system(wakeupCommand)
- self.writeToLogFile('Wakeup command is: ' + wakeupCommand)
- self.strActionInfo.setLabel("Determining TV recording status...")
- currentTime = datetime.datetime.now()
- self.writeToLogFile("Current Time: " + str(currentTime) + ", Wake-Up Time: " + str(wakeupDateTimeUTC))
- difference = time.mktime(wakeupDateTimeUTC.timetuple()) - time.mktime(currentTime.timetuple())
- outputMessage = "Difference = " + str(difference)
- self.writeToLogFile(outputMessage)
- if difference > 30:
- self.timerMsg.cancel()
- self.close()
- else:
- self.timerMsg = threading.Timer(5, self.checkRecording)
- self.timerMsg.start()
- else:
- self.strActionInfo.setLabel("Next recording less than " + str(WAITBEFORESHUTDOWN/60) + " minutes away, staying awake")
- self.timerMsg = threading.Timer(5, self.checkRecording)
- self.timerMsg.start()
- mydisplay = MyClass()
- mydisplay .doModal()
- del mydisplay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement