Revert "Plex Companion: correctly tell when we stop playing"

This reverts commit fc71fcf1fb.
This commit is contained in:
tomkat83 2016-08-08 21:36:05 +02:00
parent fc71fcf1fb
commit 99e1239acc
2 changed files with 43 additions and 70 deletions

View file

@ -180,7 +180,6 @@ class PlexCompanion(threading.Thread):
message_count += 1
if httpd:
if not t.isAlive():
# Use threads cause the method will stall
t = threading.Thread(target=httpd.handle_request)
t.start()

View file

@ -13,15 +13,9 @@ class SubscriptionManager:
self.serverlist = []
self.subscribers = {}
self.info = {}
self.key = ""
self.lastkey = ""
self.containerKey = ""
# We need one pass with previous rating key
# Tuple: (<type>, <ratingkey>)
self.ratingkey = self.lastratingkey = {
'video': "",
'music': "",
'photo': ""
}
self.lastratingkey = ""
self.volume = 0
self.mute = '0'
self.server = ""
@ -66,81 +60,67 @@ class SubscriptionManager:
msg += self.getTimelineXML(self.js.getVideoPlayerId(players), plex_video())
msg += "\r\n</MediaContainer>"
return msg
def getTimelineXML(self, playerid, ptype):
"""
If playerid is None => player stopped
"""
if playerid is not None:
self.info = self.getPlayerProperties(playerid)
info = self.getPlayerProperties(playerid)
# save this info off so the server update can use it too
self.playerprops[playerid] = self.info
state = self.info['state']
time = self.info['time']
self.playerprops[playerid] = info;
state = info['state']
time = info['time']
else:
state = "stopped"
time = 0
ret = "\r\n"+' <Timeline state="%s" time="%s" type="%s"' % (state, time, ptype)
if playerid is None and not self.lastratingkey[ptype]:
# We already told everyone that we stopped
if playerid is None:
ret += ' seekRange="0-0"'
ret += ' />'
return ret
# pbmc_server = str(WINDOW.getProperty('plexbmc.nowplaying.server'))
# userId = str(WINDOW.getProperty('currUserId'))
pbmc_server = window('pms_server')
if pbmc_server:
(self.protocol, self.server, self.port) = \
pbmc_server.split(':')
self.server = self.server.replace('/', '')
keyid = None
if playerid is not None:
count = 0
while not keyid:
if count > 300:
break
keyid = window('Plex_currently_playing_itemid')
xbmc.sleep(10)
count += 1
count = 0
while not keyid:
if count > 300:
break
keyid = window('Plex_currently_playing_itemid')
xbmc.sleep(100)
count += 1
if keyid:
self.key = "/library/metadata/%s" % keyid
self.lastratingkey[ptype] = self.ratingkey[ptype] = keyid
ret += ' location="%s"' % self.mainlocation
ret += ' key="%s"' % self.key
ret += ' ratingKey="%s"' % self.ratingkey[ptype]
elif self.lastratingkey[ptype]:
# Currently not playing a Plex item (maybe stopped)
# We need to tell everyone exactly once
self.key = "/library/metadata/%s" % self.lastratingkey[ptype]
ret += ' location="%s"' % self.mainlocation
ret += ' key="%s"' % self.key
ret += ' ratingKey="%s"' % self.lastratingkey[ptype]
# Done our job of telling we've stopped. Let's do that only once
self.lastratingkey[ptype] = ""
self.logMsg('Done telling everyone')
self.lastkey = "/library/metadata/%s"%keyid
self.lastratingkey = keyid
ret += ' location="%s"' % (self.mainlocation)
ret += ' key="%s"' % (self.lastkey)
ret += ' ratingKey="%s"' % (self.lastratingkey)
serv = self.getServerByHost(self.server)
if self.info.get('playQueueID'):
self.containerKey = "/playQueues/%s" % self.info.get('playQueueID')
ret += ' playQueueID="%s"' % self.info.get('playQueueID')
ret += ' playQueueVersion="%s"' % self.info.get('playQueueVersion')
ret += ' playQueueItemID="%s"' % self.info.get('playQueueItemID')
if info.get('playQueueID'):
self.containerKey = "/playQueues/%s" % info.get('playQueueID')
ret += ' playQueueID="%s"' % info.get('playQueueID')
ret += ' playQueueVersion="%s"' % info.get('playQueueVersion')
ret += ' playQueueItemID="%s"' % (info.get('playQueueItemID'))
ret += ' containerKey="%s"' % self.containerKey
elif keyid or self.lastratingkey[ptype]:
self.containerKey = self.key
elif keyid:
self.containerKey = self.lastkey
ret += ' containerKey="%s"' % (self.containerKey)
ret += ' duration="%s"' % self.info['duration']
ret += ' seekRange="0-%s"' % self.info['duration']
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"' % self.info['guid']
ret += ' volume="%s"' % self.info['volume']
ret += ' shuffle="%s"' % self.info['shuffle']
ret += ' guid="%s"' % info['guid']
ret += ' volume="%s"' % info['volume']
ret += ' shuffle="%s"' % info['shuffle']
ret += ' mute="%s"' % self.mute
ret += ' repeat="%s"' % self.info['repeat']
ret += ' repeat="%s"' % info['repeat']
# Might need an update in the future
ret += ' subtitleStreamID="-1"'
ret += ' audioStreamID="-1"'
@ -150,9 +130,9 @@ class SubscriptionManager:
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.subscribers[uuid].commandID = int(commandID)
def notify(self, event = False):
self.cleanup()
# Don't tell anyone if we don't know a Plex ID and are still playing
# (e.g. no stop called). Used for e.g. PVR/TV without PKC usage
@ -165,18 +145,12 @@ class SubscriptionManager:
if self.subscribers:
with threading.RLock():
for sub in self.subscribers.values():
sub.send_update(msg, len(players) == 0)
sub.send_update(msg, len(players)==0)
self.notifyServer(players)
return True
def notifyServer(self, players):
conv = {
"video": "video",
"audio": "music",
"picture": "photo"
}
for typus, p in players.iteritems():
ptype = conv[typus]
for p in players.values():
info = self.playerprops[p.get('playerid')]
params = {'state': 'stopped'}
params['containerKey'] = (self.containerKey or "/library/metadata/900000")
@ -184,8 +158,8 @@ class SubscriptionManager:
params['containerKey'] = '/playQueues/' + info['playQueueID']
params['playQueueVersion'] = info['playQueueVersion']
params['playQueueItemID'] = info['playQueueItemID']
params['key'] = (self.key or "/library/metadata/900000")
params['ratingKey'] = (self.ratingkey[ptype] or "900000")
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']