From 2f90674f513f328e92334b92b856e616422368e5 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Thu, 28 Dec 2017 21:31:05 +0100 Subject: [PATCH] Major Plex Companion overhaul, part 5 --- resources/lib/PlexCompanion.py | 18 +++++++++++------- resources/lib/kodimonitor.py | 4 ++++ resources/lib/playlist_func.py | 27 +++++++++++++++++++++++++++ resources/lib/playqueue.py | 2 +- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index 2183d155..40620ece 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -93,7 +93,7 @@ class PlexCompanion(Thread): def _process_playlist(self, data): # Get the playqueue ID try: - _, plex_id, query = ParseContainerKey(data['containerKey']) + _, container_key, query = ParseContainerKey(data['containerKey']) except: LOG.error('Exception while processing') import traceback @@ -114,12 +114,16 @@ class PlexCompanion(Thread): api = API(xml[0]) playqueue = self.mgr.playqueue.get_playqueue_from_type( v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()]) - self.mgr.playqueue.update_playqueue_from_PMS( - playqueue, - playqueue_id=plex_id, - repeat=query.get('repeat'), - offset=data.get('offset'), - transient_token=data.get('key')) + if playqueue.id == container_key: + # OK, really weird, this happens at least with Plex for Android + LOG.debug('Already know this Plex playQueue, ignoring this command') + else: + self.mgr.playqueue.update_playqueue_from_PMS( + playqueue, + playqueue_id=container_key, + repeat=query.get('repeat'), + offset=data.get('offset'), + transient_token=data.get('key')) @LOCKER.lockthis def _process_streams(self, data): diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index be41eaf0..1f7f5619 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -199,6 +199,10 @@ class KodiMonitor(Monitor): Will NOT be called if playback initiated by Kodi widgets """ playqueue = self.playqueue.playqueues[data['playlistid']] + # Did PKC cause this add? Then lets not do anything + if playqueue.is_kodi_onadd() is False: + LOG.debug('PKC added this item to the playqueue - ignoring') + return # Check whether we even need to update our known playqueue kodi_playqueue = js.playlist_get_items(data['playlistid']) if playqueue.old_kodi_pl == kodi_playqueue: diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index f2fe8b34..e5bab020 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -42,6 +42,9 @@ class PlaylistObjectBaseclase(object): self.shuffled = 0 self.repeat = 0 self.plex_transient_token = None + # Needed to not add an item twice (first through PKC, then the kodi + # monitor) + self._onadd_queue = [] def __repr__(self): """ @@ -61,6 +64,25 @@ class PlaylistObjectBaseclase(object): answ += '\'%s\': %s, ' % (key, str(getattr(self, key))) return answ + '\'items\': %s}}' % self.items + def kodi_onadd(self): + """ + Call this before adding an item to the Kodi playqueue + """ + self._onadd_queue.append(None) + + def is_kodi_onadd(self): + """ + Returns False if the last kodimonitor on_add was caused by PKC - so that + we are not adding a playlist item twice. + + Calling this function will remove the item from our "checklist" + """ + try: + self._onadd_queue.pop() + except IndexError: + return True + return False + def clear(self): """ Resets the playlist object to an empty playlist @@ -428,9 +450,11 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None, params['item'] = {'%sid' % item.kodi_type: int(item.kodi_id)} else: params['item'] = {'file': item.file} + playlist.kodi_onadd() reply = js.playlist_insert(params) if reply.get('error') is not None: LOG.error('Could not add item to playlist. Kodi reply. %s', reply) + playlist.is_kodi_onadd() return False return True @@ -499,9 +523,11 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None, params['item'] = {'%sid' % kodi_type: int(kodi_id)} else: params['item'] = {'file': file} + playlist.kodi_onadd() reply = js.playlist_insert(params) if reply.get('error') is not None: LOG.error('Could not add item to playlist. Kodi reply. %s', reply) + playlist.is_kodi_onadd() return False item = playlist_item_from_kodi( {'id': kodi_id, 'type': kodi_type, 'file': file}) @@ -623,6 +649,7 @@ def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file, LOG.debug('Insert listitem at position %s for Kodi only for %s', pos, playlist) # Add the item into Kodi playlist + playlist.kodi_onadd() playlist.kodi_pl.add(file, listitem, index=pos) # We need to add this to our internal queue as well if xml_video_element is not None: diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index c178b254..cdb23cf6 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -122,9 +122,9 @@ class Playqueue(Thread): except KeyError: LOG.error('Could not get playqueue ID %s', playqueue_id) return - PlaybackUtils(xml, playqueue).play_all() playqueue.repeat = 0 if not repeat else int(repeat) playqueue.token = transient_token + PlaybackUtils(xml, playqueue).play_all() window('plex_customplaylist', value="true") if offset not in (None, "0"): window('plex_customplaylist.seektime',