2017-03-17 15:41:46 +11:00
|
|
|
# Gnu General Public License - see LICENSE.TXT
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
import xbmc
|
|
|
|
|
import xbmcgui
|
|
|
|
|
import time
|
|
|
|
|
from datetime import datetime
|
2017-03-10 10:45:38 +11:00
|
|
|
|
2017-03-17 15:41:46 +11:00
|
|
|
from resources.lib.websocketclient import WebSocketThread
|
|
|
|
|
from resources.lib.downloadutils import DownloadUtils
|
|
|
|
|
from resources.lib.simple_logging import SimpleLogging
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-03-17 15:41:46 +11:00
|
|
|
log = SimpleLogging("EmbyCon.service")
|
2014-10-03 19:46:33 +10:00
|
|
|
downloadUtils = DownloadUtils()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
# auth the service
|
|
|
|
|
try:
|
|
|
|
|
downloadUtils.authenticate()
|
|
|
|
|
except Exception, e:
|
|
|
|
|
pass
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
newWebSocketThread = WebSocketThread()
|
2017-03-13 17:40:39 +11:00
|
|
|
newWebSocketThread.setDaemon(True)
|
2014-10-03 19:46:33 +10:00
|
|
|
newWebSocketThread.start()
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
def hasData(data):
|
|
|
|
|
if(data == None or len(data) == 0 or data == "None"):
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def stopAll(played_information):
|
|
|
|
|
|
|
|
|
|
if(len(played_information) == 0):
|
|
|
|
|
return
|
|
|
|
|
|
2017-04-08 12:02:12 +10:00
|
|
|
log.info("played_information : " + str(played_information))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
for item_url in played_information:
|
|
|
|
|
data = played_information.get(item_url)
|
|
|
|
|
if(data != None):
|
2017-04-08 12:02:12 +10:00
|
|
|
log.info("item_url : " + item_url)
|
|
|
|
|
log.info("item_data : " + str(data))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
currentPossition = data.get("currentPossition")
|
|
|
|
|
item_id = data.get("item_id")
|
|
|
|
|
|
2014-10-11 14:23:35 +11:00
|
|
|
if(hasData(item_id)):
|
2017-04-08 12:02:12 +10:00
|
|
|
log.info("Playback Stopped at: " + str(int(currentPossition * 10000000)))
|
2014-10-04 12:34:53 +10:00
|
|
|
newWebSocketThread.playbackStopped(item_id, str(int(currentPossition * 10000000)))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
played_information.clear()
|
2014-10-03 19:46:33 +10:00
|
|
|
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
class Service( xbmc.Player ):
|
|
|
|
|
|
|
|
|
|
played_information = {}
|
|
|
|
|
|
|
|
|
|
def __init__( self, *args ):
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> starting monitor service")
|
2014-09-28 10:30:07 +10:00
|
|
|
self.played_information = {}
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def onPlayBackStarted( self ):
|
|
|
|
|
# Will be called when xbmc starts playing a file
|
|
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
|
|
|
|
currentFile = xbmc.Player().getPlayingFile()
|
2017-04-09 10:25:58 +10:00
|
|
|
log.info("onPlayBackStarted" + currentFile)
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
2017-04-09 10:25:58 +10:00
|
|
|
item_id = WINDOW.getProperty("playback_url_" + currentFile)
|
|
|
|
|
log.info("item_id: " + item_id)
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
# reset all these so they dont get used is xbmc plays a none
|
2017-04-09 10:25:58 +10:00
|
|
|
#WINDOW.setProperty("item_id", "")
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
if(item_id == None or len(item_id) == 0):
|
|
|
|
|
return
|
|
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
newWebSocketThread.playbackStarted(item_id)
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
data = {}
|
|
|
|
|
data["item_id"] = item_id
|
|
|
|
|
self.played_information[currentFile] = data
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> ADDING_FILE : " + currentFile)
|
|
|
|
|
log.info("EmbyCon Service -> ADDING_FILE : " + str(self.played_information))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
def onPlayBackEnded( self ):
|
|
|
|
|
# Will be called when xbmc stops playing a file
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> onPlayBackEnded")
|
2014-09-28 10:30:07 +10:00
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
|
|
|
|
def onPlayBackStopped( self ):
|
|
|
|
|
# Will be called when user stops xbmc playing a file
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> onPlayBackStopped")
|
2014-09-28 10:30:07 +10:00
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
|
|
|
|
monitor = Service()
|
|
|
|
|
lastProgressUpdate = datetime.today()
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
while not xbmc.abortRequested:
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
if xbmc.Player().isPlaying():
|
|
|
|
|
try:
|
|
|
|
|
# send update
|
|
|
|
|
td = datetime.today() - lastProgressUpdate
|
|
|
|
|
secDiff = td.seconds
|
2014-10-04 12:34:53 +10:00
|
|
|
if(secDiff > 5):
|
2017-03-13 17:40:39 +11:00
|
|
|
|
|
|
|
|
playTime = xbmc.Player().getTime()
|
|
|
|
|
currentFile = xbmc.Player().getPlayingFile()
|
|
|
|
|
|
|
|
|
|
if(monitor.played_information.get(currentFile) != None):
|
|
|
|
|
monitor.played_information[currentFile]["currentPossition"] = playTime
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
if(monitor.played_information.get(currentFile) != None and monitor.played_information.get(currentFile).get("item_id") != None):
|
|
|
|
|
item_id = monitor.played_information.get(currentFile).get("item_id")
|
2014-10-03 19:46:33 +10:00
|
|
|
newWebSocketThread.sendProgressUpdate(item_id, str(int(playTime * 10000000)))
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
lastProgressUpdate = datetime.today()
|
|
|
|
|
|
|
|
|
|
except Exception, e:
|
2017-03-10 10:45:38 +11:00
|
|
|
log.error("EmbyCon Service -> Exception in Playback Monitor : " + str(e))
|
2014-09-28 10:30:07 +10:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
xbmc.sleep(1000)
|
2017-03-08 21:02:10 +11:00
|
|
|
xbmcgui.Window(10000).setProperty("EmbyCon_Service_Timestamp", str(int(time.time())))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
# stop the WebSocket client
|
2014-10-03 19:46:33 +10:00
|
|
|
newWebSocketThread.stopClient()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> Service shutting down")
|