From cee2bfcc6c3933f1b0dcec383df9b3c3b025c0fc Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Thu, 25 May 2017 14:21:27 +0200 Subject: [PATCH] Better way to sync progress to another account - Partially fixes #297 --- resources/lib/PlexCompanion.py | 8 ++++++-- resources/lib/player.py | 2 +- resources/lib/playlist_func.py | 3 +++ resources/lib/playqueue.py | 3 +++ resources/lib/plexbmchelper/subscribers.py | 13 ++++++++++--- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index c172ca38..a16bf50e 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -78,7 +78,7 @@ class PlexCompanion(Thread): data = task['data'] # Get the token of the user flinging media (might be different one) - state.PLEX_TRANSIENT_TOKEN = data.get('token') + token = data.get('token') if task['action'] == 'alexa': # e.g. Alexa xml = GetPlexMetadata(data['key']) @@ -90,9 +90,11 @@ class PlexCompanion(Thread): api = API(xml[0]) if api.getType() == v.PLEX_TYPE_ALBUM: log.debug('Plex music album detected') - self.mgr.playqueue.init_playqueue_from_plex_children( + queue = self.mgr.playqueue.init_playqueue_from_plex_children( api.getRatingKey()) + queue.plex_transient_token = token else: + state.PLEX_TRANSIENT_TOKEN = token params = { 'mode': 'plex_node', 'key': '{server}%s' % data.get('key'), @@ -106,6 +108,7 @@ class PlexCompanion(Thread): elif (task['action'] == 'playlist' and data.get('address') == 'node.plexapp.com'): # E.g. watch later initiated by Companion + state.PLEX_TRANSIENT_TOKEN = token params = { 'mode': 'plex_node', 'key': '{server}%s' % data.get('key'), @@ -144,6 +147,7 @@ class PlexCompanion(Thread): ID, repeat=query.get('repeat'), offset=data.get('offset')) + playqueue.plex_transient_token = token def run(self): # Ensure that sockets will be closed no matter what diff --git a/resources/lib/player.py b/resources/lib/player.py index e596c854..1dafbe68 100644 --- a/resources/lib/player.py +++ b/resources/lib/player.py @@ -310,7 +310,7 @@ class Player(xbmc.Player): 'plex_forcetranscode'): window(item, clear=True) # We might have saved a transient token from a user flinging media via - # Companion + # Companion (if we could not use the playqueue to store the token) state.PLEX_TRANSIENT_TOKEN = None log.debug("Cleared playlist properties.") diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index fa439cde..f1371acd 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -29,6 +29,8 @@ class Playlist_Object_Baseclase(object): selectedItemOffset = None shuffled = 0 # [int], 0: not shuffled, 1: ??? 2: ??? repeat = 0 # [int], 0: not repeated, 1: ??? 2: ??? + # If Companion playback is initiated by another user + plex_transient_token = None def __repr__(self): answ = "<%s: " % (self.__class__.__name__) @@ -58,6 +60,7 @@ class Playlist_Object_Baseclase(object): self.selectedItemOffset = None self.shuffled = 0 self.repeat = 0 + self.plex_transient_token = None log.debug('Playlist cleared: %s' % self) def log_Kodi_playlist(self): diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index 6df21b0d..2420ca0a 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -78,6 +78,8 @@ class Playqueue(Thread): def init_playqueue_from_plex_children(self, plex_id): """ Init a new playqueue e.g. from an album. Alexa does this + + Returns the Playlist_Object """ xml = GetAllPlexChildren(plex_id) try: @@ -93,6 +95,7 @@ class Playqueue(Thread): PL.add_item_to_playlist(playqueue, i, plex_id=api.getRatingKey()) log.debug('Firing up Kodi player') Player().play(playqueue.kodi_pl, None, False, 0) + return playqueue def update_playqueue_from_PMS(self, playqueue, diff --git a/resources/lib/plexbmchelper/subscribers.py b/resources/lib/plexbmchelper/subscribers.py index 4ac31483..dcee3e57 100644 --- a/resources/lib/plexbmchelper/subscribers.py +++ b/resources/lib/plexbmchelper/subscribers.py @@ -123,6 +123,8 @@ class SubscriptionManager: ret += ' itemType="%s"' % info['itemType'] if state.PLEX_TRANSIENT_TOKEN: ret += ' token="%s"' % state.PLEX_TRANSIENT_TOKEN + elif info['plex_transient_token']: + ret += ' token="%s"' % info['plex_transient_token'] # Might need an update in the future if ptype == 'video': ret += ' subtitleStreamID="-1"' @@ -157,7 +159,7 @@ class SubscriptionManager: def notifyServer(self, players): for typus, p in players.iteritems(): info = self.playerprops[p.get('playerid')] - self._sendNotification(info) + self._sendNotification(info, int(p['playerid'])) self.lastinfo[typus] = info # Cross the one of the list try: @@ -167,9 +169,10 @@ class SubscriptionManager: # Process the players we have left (to signal a stop) for typus, p in self.lastplayers.iteritems(): self.lastinfo[typus]['state'] = 'stopped' - self._sendNotification(self.lastinfo[typus]) + self._sendNotification(self.lastinfo[typus], int(p['playerid'])) - def _sendNotification(self, info): + def _sendNotification(self, info, playerid): + playqueue = self.playqueue.playqueues[playerid] xargs = getXArgsDeviceInfo() params = { 'containerKey': self.containerKey or "/library/metadata/900000", @@ -181,6 +184,8 @@ class SubscriptionManager: } if state.PLEX_TRANSIENT_TOKEN: xargs['X-Plex-Token'] = state.PLEX_TRANSIENT_TOKEN + elif playqueue.plex_transient_token: + xargs['X-Plex-Token'] = playqueue.plex_transient_token if info.get('playQueueID'): params['containerKey'] = '/playQueues/%s' % info['playQueueID'] params['playQueueVersion'] = info['playQueueVersion'] @@ -272,6 +277,8 @@ class SubscriptionManager: info['volume'] = self.volume info['mute'] = self.mute + info['plex_transient_token'] = playqueue.plex_transient_token + return info