2016-01-15 22:12:52 +11:00
|
|
|
import re
|
|
|
|
import threading
|
2016-02-07 22:38:50 +11:00
|
|
|
|
|
|
|
from xbmc import Player
|
2016-04-07 02:23:51 +10:00
|
|
|
|
2016-01-23 01:37:20 +11:00
|
|
|
import downloadutils
|
2016-04-03 01:46:23 +11:00
|
|
|
from utils import window, logging
|
2016-02-07 22:38:50 +11:00
|
|
|
import PlexFunctions as pf
|
2016-04-07 02:23:51 +10:00
|
|
|
from functions import *
|
2016-01-15 22:12:52 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
|
|
|
|
@logging
|
2016-01-15 22:12:52 +11:00
|
|
|
class SubscriptionManager:
|
2016-04-03 01:46:23 +11:00
|
|
|
def __init__(self, jsonClass, RequestMgr):
|
|
|
|
self.serverlist = []
|
2016-01-15 22:12:52 +11:00
|
|
|
self.subscribers = {}
|
|
|
|
self.info = {}
|
|
|
|
self.lastkey = ""
|
2016-01-30 06:07:21 +11:00
|
|
|
self.containerKey = ""
|
2016-01-15 22:12:52 +11:00
|
|
|
self.lastratingkey = ""
|
|
|
|
self.volume = 0
|
2016-02-07 22:38:50 +11:00
|
|
|
self.mute = '0'
|
2016-01-15 22:12:52 +11:00
|
|
|
self.server = ""
|
|
|
|
self.protocol = "http"
|
|
|
|
self.port = ""
|
|
|
|
self.playerprops = {}
|
2016-04-07 02:23:51 +10:00
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2016-02-07 22:38:50 +11:00
|
|
|
self.xbmcplayer = Player()
|
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
self.js = jsonClass
|
|
|
|
self.RequestMgr = RequestMgr
|
|
|
|
|
|
|
|
def getServerByHost(self, host):
|
|
|
|
if len(self.serverlist) == 1:
|
|
|
|
return self.serverlist[0]
|
|
|
|
for server in self.serverlist:
|
|
|
|
if (server.get('serverName') in host or
|
|
|
|
server.get('server') in host):
|
|
|
|
return server
|
|
|
|
return {}
|
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def getVolume(self):
|
2016-04-03 01:46:23 +11:00
|
|
|
self.volume, self.mute = self.js.getVolume()
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
def msg(self, players):
|
|
|
|
msg = getXMLHeader()
|
|
|
|
msg += '<MediaContainer commandID="INSERTCOMMANDID"'
|
|
|
|
if players:
|
|
|
|
self.getVolume()
|
|
|
|
maintype = plex_audio()
|
|
|
|
for p in players.values():
|
|
|
|
if p.get('type') == xbmc_video():
|
|
|
|
maintype = plex_video()
|
|
|
|
elif p.get('type') == xbmc_photo():
|
|
|
|
maintype = plex_photo()
|
|
|
|
self.mainlocation = "fullScreen" + maintype[0:1].upper() + maintype[1:].lower()
|
|
|
|
else:
|
|
|
|
self.mainlocation = "navigation"
|
|
|
|
msg += ' location="%s">' % self.mainlocation
|
2016-04-03 01:46:23 +11:00
|
|
|
msg += self.getTimelineXML(self.js.getAudioPlayerId(players), plex_audio())
|
|
|
|
msg += self.getTimelineXML(self.js.getPhotoPlayerId(players), plex_photo())
|
|
|
|
msg += self.getTimelineXML(self.js.getVideoPlayerId(players), plex_video())
|
2016-01-15 22:12:52 +11:00
|
|
|
msg += "\r\n</MediaContainer>"
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def getTimelineXML(self, playerid, ptype):
|
|
|
|
if playerid is not None:
|
|
|
|
info = self.getPlayerProperties(playerid)
|
|
|
|
# save this info off so the server update can use it too
|
|
|
|
self.playerprops[playerid] = info;
|
|
|
|
state = info['state']
|
|
|
|
time = info['time']
|
|
|
|
else:
|
|
|
|
state = "stopped"
|
|
|
|
time = 0
|
2016-02-07 22:38:50 +11:00
|
|
|
ret = "\r\n"+' <Timeline state="%s" time="%s" type="%s"' % (state, time, ptype)
|
|
|
|
if playerid is None:
|
|
|
|
ret += ' seekRange="0-0"'
|
|
|
|
ret += ' />'
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# pbmc_server = str(WINDOW.getProperty('plexbmc.nowplaying.server'))
|
2016-03-11 02:02:46 +11:00
|
|
|
# userId = str(WINDOW.getProperty('currUserId'))
|
2016-03-23 01:34:59 +11:00
|
|
|
pbmc_server = window('pms_server')
|
2016-03-23 00:58:51 +11:00
|
|
|
if pbmc_server:
|
|
|
|
(self.protocol, self.server, self.port) = \
|
|
|
|
pbmc_server.split(':')
|
|
|
|
self.server = self.server.replace('/', '')
|
2016-02-07 22:38:50 +11:00
|
|
|
keyid = None
|
|
|
|
count = 0
|
|
|
|
while not keyid:
|
2016-03-17 03:02:22 +11:00
|
|
|
if count > 300:
|
2016-02-07 22:38:50 +11:00
|
|
|
break
|
2016-03-23 01:34:59 +11:00
|
|
|
keyid = window('Plex_currently_playing_itemid')
|
2016-03-17 03:02:22 +11:00
|
|
|
xbmc.sleep(100)
|
2016-02-07 22:38:50 +11:00
|
|
|
count += 1
|
|
|
|
if keyid:
|
|
|
|
self.lastkey = "/library/metadata/%s"%keyid
|
|
|
|
self.lastratingkey = keyid
|
|
|
|
ret += ' location="%s"' % (self.mainlocation)
|
|
|
|
ret += ' key="%s"' % (self.lastkey)
|
|
|
|
ret += ' ratingKey="%s"' % (self.lastratingkey)
|
2016-04-03 01:46:23 +11:00
|
|
|
serv = self.getServerByHost(self.server)
|
2016-02-07 22:38:50 +11:00
|
|
|
if info.get('playQueueID'):
|
2016-03-17 04:01:07 +11:00
|
|
|
self.containerKey = "/playQueues/%s" % info.get('playQueueID')
|
2016-02-07 22:38:50 +11:00
|
|
|
ret += ' playQueueID="%s"' % info.get('playQueueID')
|
|
|
|
ret += ' playQueueVersion="%s"' % info.get('playQueueVersion')
|
|
|
|
ret += ' playQueueItemID="%s"' % (info.get('playQueueItemID'))
|
2016-03-17 04:01:07 +11:00
|
|
|
ret += ' containerKey="%s"' % self.containerKey
|
2016-02-07 22:38:50 +11:00
|
|
|
elif keyid:
|
2016-03-17 04:01:07 +11:00
|
|
|
self.containerKey = self.lastkey
|
2016-02-07 22:38:50 +11:00
|
|
|
ret += ' containerKey="%s"' % (self.containerKey)
|
|
|
|
|
|
|
|
ret += ' duration="%s"' % info['duration']
|
|
|
|
ret += ' seekRange="0-%s"' % info['duration']
|
|
|
|
ret += ' controllable="%s"' % self.controllable()
|
|
|
|
ret += ' machineIdentifier="%s"' % serv.get('uuid', "")
|
|
|
|
ret += ' protocol="%s"' % serv.get('protocol', "http")
|
|
|
|
ret += ' address="%s"' % serv.get('server', self.server)
|
|
|
|
ret += ' port="%s"' % serv.get('port', self.port)
|
|
|
|
ret += ' guid="%s"' % info['guid']
|
|
|
|
ret += ' volume="%s"' % info['volume']
|
|
|
|
ret += ' shuffle="%s"' % info['shuffle']
|
|
|
|
ret += ' mute="%s"' % self.mute
|
|
|
|
ret += ' repeat="%s"' % info['repeat']
|
|
|
|
# Might need an update in the future
|
|
|
|
ret += ' subtitleStreamID="-1"'
|
|
|
|
ret += ' audioStreamID="-1"'
|
|
|
|
|
|
|
|
ret += ' />'
|
2016-01-15 22:12:52 +11:00
|
|
|
return ret
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def updateCommandID(self, uuid, commandID):
|
|
|
|
if commandID and self.subscribers.get(uuid, False):
|
|
|
|
self.subscribers[uuid].commandID = int(commandID)
|
|
|
|
|
|
|
|
def notify(self, event = False):
|
|
|
|
self.cleanup()
|
2016-04-03 01:46:23 +11:00
|
|
|
players = self.js.getPlayers()
|
2016-01-15 22:12:52 +11:00
|
|
|
# fetch the message, subscribers or not, since the server
|
|
|
|
# will need the info anyway
|
|
|
|
msg = self.msg(players)
|
|
|
|
if self.subscribers:
|
|
|
|
with threading.RLock():
|
|
|
|
for sub in self.subscribers.values():
|
|
|
|
sub.send_update(msg, len(players)==0)
|
|
|
|
self.notifyServer(players)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def notifyServer(self, players):
|
2016-01-27 22:18:54 +11:00
|
|
|
if not players:
|
|
|
|
return True
|
2016-01-15 22:12:52 +11:00
|
|
|
params = {'state': 'stopped'}
|
|
|
|
for p in players.values():
|
|
|
|
info = self.playerprops[p.get('playerid')]
|
|
|
|
params = {}
|
2016-01-30 06:07:21 +11:00
|
|
|
params['containerKey'] = (self.containerKey or "/library/metadata/900000")
|
2016-02-07 22:38:50 +11:00
|
|
|
if info.get('playQueueID'):
|
|
|
|
params['containerKey'] = '/playQueues/' + info['playQueueID']
|
|
|
|
params['playQueueVersion'] = info['playQueueVersion']
|
|
|
|
params['playQueueItemID'] = info['playQueueItemID']
|
2016-01-15 22:12:52 +11:00
|
|
|
params['key'] = (self.lastkey or "/library/metadata/900000")
|
|
|
|
params['ratingKey'] = (self.lastratingkey or "900000")
|
|
|
|
params['state'] = info['state']
|
|
|
|
params['time'] = info['time']
|
|
|
|
params['duration'] = info['duration']
|
2016-04-03 01:46:23 +11:00
|
|
|
serv = self.getServerByHost(self.server)
|
2016-01-23 01:37:20 +11:00
|
|
|
url = serv.get('protocol', 'http') + '://' \
|
|
|
|
+ serv.get('server', 'localhost') + ':' \
|
2016-03-14 19:54:15 +11:00
|
|
|
+ serv.get('port', '32400') + "/:/timeline"
|
2016-04-07 02:23:51 +10:00
|
|
|
self.doUtils(url, type="GET", parameters=params)
|
2016-01-23 01:37:20 +11:00
|
|
|
# requests.getwithparams(serv.get('server', 'localhost'), serv.get('port', 32400), "/:/timeline", params, getPlexHeaders(), serv.get('protocol', 'http'))
|
2016-04-03 01:46:23 +11:00
|
|
|
self.logMsg("params: %s" % params, 2)
|
|
|
|
self.logMsg("players: %s" % players, 2)
|
|
|
|
self.logMsg("sent server notification with state = %s"
|
|
|
|
% params['state'], 2)
|
2016-01-27 22:18:54 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def controllable(self):
|
2016-01-30 06:07:21 +11:00
|
|
|
return "volume,shuffle,repeat,audioStream,videoStream,subtitleStream,skipPrevious,skipNext,seekTo,stepBack,stepForward,stop,playPause"
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def addSubscriber(self, protocol, host, port, uuid, commandID):
|
2016-04-03 01:46:23 +11:00
|
|
|
sub = Subscriber(protocol,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
uuid,
|
|
|
|
commandID,
|
|
|
|
self,
|
|
|
|
self.RequestMgr)
|
2016-01-15 22:12:52 +11:00
|
|
|
with threading.RLock():
|
|
|
|
self.subscribers[sub.uuid] = sub
|
|
|
|
return sub
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def removeSubscriber(self, uuid):
|
|
|
|
with threading.RLock():
|
|
|
|
for sub in self.subscribers.values():
|
|
|
|
if sub.uuid == uuid or sub.host == uuid:
|
|
|
|
sub.cleanup()
|
|
|
|
del self.subscribers[sub.uuid]
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def cleanup(self):
|
|
|
|
with threading.RLock():
|
|
|
|
for sub in self.subscribers.values():
|
|
|
|
if sub.age > 30:
|
|
|
|
sub.cleanup()
|
|
|
|
del self.subscribers[sub.uuid]
|
|
|
|
|
|
|
|
def getPlayerProperties(self, playerid):
|
|
|
|
info = {}
|
|
|
|
try:
|
|
|
|
# get info from the player
|
2016-04-03 01:46:23 +11:00
|
|
|
props = self.js.jsonrpc("Player.GetProperties", {"playerid": playerid, "properties": ["time", "totaltime", "speed", "shuffled", "repeat"]})
|
|
|
|
self.logMsg(self.js.jsonrpc("Player.GetItem", {"playerid": playerid, "properties": ["file", "showlink", "episode", "season"]}), 2)
|
2016-01-15 22:12:52 +11:00
|
|
|
info['time'] = timeToMillis(props['time'])
|
|
|
|
info['duration'] = timeToMillis(props['totaltime'])
|
|
|
|
info['state'] = ("paused", "playing")[int(props['speed'])]
|
2016-02-07 22:38:50 +11:00
|
|
|
info['shuffle'] = ("0","1")[props.get('shuffled', False)]
|
|
|
|
info['repeat'] = pf.getPlexRepeat(props.get('repeat'))
|
|
|
|
# New PMS playQueue attributes
|
|
|
|
cf = self.xbmcplayer.getPlayingFile()
|
|
|
|
info['playQueueID'] = window('playQueueID')
|
|
|
|
info['playQueueVersion'] = window('playQueueVersion')
|
|
|
|
info['playQueueItemID'] = window('plex_%s.playQueueItemID' % cf)
|
|
|
|
info['guid'] = window('plex_%s.guid' % cf)
|
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
except:
|
|
|
|
info['time'] = 0
|
|
|
|
info['duration'] = 0
|
|
|
|
info['state'] = "stopped"
|
|
|
|
info['shuffle'] = False
|
|
|
|
# get the volume from the application
|
|
|
|
info['volume'] = self.volume
|
2016-02-07 22:38:50 +11:00
|
|
|
info['mute'] = self.mute
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
return info
|
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
|
|
|
|
@logging
|
2016-01-15 22:12:52 +11:00
|
|
|
class Subscriber:
|
2016-04-03 01:46:23 +11:00
|
|
|
def __init__(self, protocol, host, port, uuid, commandID,
|
|
|
|
subMgr, RequestMgr):
|
2016-01-15 22:12:52 +11:00
|
|
|
self.protocol = protocol or "http"
|
|
|
|
self.host = host
|
|
|
|
self.port = port or 32400
|
|
|
|
self.uuid = uuid or host
|
|
|
|
self.commandID = int(commandID) or 0
|
|
|
|
self.navlocationsent = False
|
|
|
|
self.age = 0
|
2016-04-07 02:23:51 +10:00
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2016-04-03 01:46:23 +11:00
|
|
|
self.subMgr = subMgr
|
|
|
|
self.RequestMgr = RequestMgr
|
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.uuid == other.uuid
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def tostr(self):
|
|
|
|
return "uuid=%s,commandID=%i" % (self.uuid, self.commandID)
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def cleanup(self):
|
2016-04-03 01:46:23 +11:00
|
|
|
self.RequestMgr.closeConnection(self.protocol, self.host, self.port)
|
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def send_update(self, msg, is_nav):
|
|
|
|
self.age += 1
|
|
|
|
if not is_nav:
|
|
|
|
self.navlocationsent = False
|
|
|
|
elif self.navlocationsent:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.navlocationsent = True
|
|
|
|
msg = re.sub(r"INSERTCOMMANDID", str(self.commandID), msg)
|
2016-04-03 01:46:23 +11:00
|
|
|
self.logMsg("sending xml to subscriber %s: %s"
|
|
|
|
% (self.tostr(), msg), 2)
|
2016-01-23 01:37:20 +11:00
|
|
|
url = self.protocol + '://' + self.host + ':' + self.port \
|
|
|
|
+ "/:/timeline"
|
2016-02-11 20:35:16 +11:00
|
|
|
t = threading.Thread(target=self.threadedSend, args=(url, msg))
|
|
|
|
t.start()
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2016-02-11 20:35:16 +11:00
|
|
|
def threadedSend(self, url, msg):
|
|
|
|
"""
|
|
|
|
Threaded POST request, because they stall due to PMS response missing
|
|
|
|
the Content-Length header :-(
|
|
|
|
"""
|
2016-04-07 02:23:51 +10:00
|
|
|
response = self.doUtils(url,
|
|
|
|
postBody=msg,
|
|
|
|
type="POST")
|
2016-01-30 06:07:21 +11:00
|
|
|
if response in [False, None, 401]:
|
2016-04-03 01:46:23 +11:00
|
|
|
self.subMgr.removeSubscriber(self.uuid)
|