From 4208bb9b7335c2fbc275946b0c3e09fa2a612135 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Wed, 28 Dec 2016 14:48:23 +0100 Subject: [PATCH] playQueue fixes --- resources/lib/kodimonitor.py | 5 ++++ resources/lib/playlist_func.py | 13 +++++++--- resources/lib/playqueue.py | 46 ++++++++++++++++------------------ 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index 09d797fd..1fcad0bc 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -173,6 +173,11 @@ class KodiMonitor(xbmc.Monitor): # u'position': 0} self.playqueue.kodi_onadd(data) + elif method == "Playlist.OnRemove": + # User (or PKC) deleted a playlist item + # Data: {u'position': 2, u'playlistid': 1} + self.playqueue.kodi_onremove(data) + def PlayBackStart(self, data): """ Called whenever a playback is started diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index 2b243899..6a82e876 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -30,8 +30,12 @@ class Playlist_Object_Baseclase(object): def __repr__(self): answ = "<%s: " % (self.__class__.__name__) + # For some reason, can't use dir directly + answ += "ID: %s, " % self.ID + answ += "items: %s" % self.items for key in self.__dict__: - answ += '%s: %s, ' % (key, getattr(self, key)) + if key not in ("ID", 'items'): + answ += '%s: %s, ' % (key, getattr(self, key)) return answ[:-2] + ">" def clear(self): @@ -88,8 +92,8 @@ def playlist_item_from_kodi_item(kodi_item): kodi_item dict contains keys 'id', 'type', 'file' (if applicable) """ item = Playlist_Item() - if kodi_item.get('id'): - item.kodi_id = kodi_item['id'] + item.kodi_id = kodi_item.get('id') + if item.kodi_id: with embydb.GetEmbyDB() as emby_db: emby_dbitem = emby_db.getItem_byKodiId(kodi_item['id'], kodi_item['type']) @@ -227,6 +231,7 @@ def move_playlist_item(playlist, before_pos, after_pos): """ Moves playlist item from before_pos [int] to after_pos [int] """ + log.debug('Moving item from %s to %s' % (before_pos, after_pos)) if after_pos == 0: url = "{server}/%ss/%s/items/%s/move?after=0" % \ (playlist.kind, @@ -256,7 +261,7 @@ def delete_playlist_item(playlist, pos): playlist.repeat), action_type="DELETE") _get_playListVersion_from_xml(playlist, xml) - del playlist.items[pos] + del playlist.items[pos], playlist.old_kodi_pl[pos] def get_kodi_playlist_items(playlist): diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index 507fc6b9..e4f0f4f5 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -90,7 +90,7 @@ class Playqueue(Thread): repeat = 0, 1, 2 """ - log.info('New playqueue received, updating!') + log.info('New playqueue received from the PMS, updating!') PL.update_playlist_from_PMS(playqueue, playqueue_id, repeat) log.debug('Updated playqueue: %s' % playqueue) @@ -108,17 +108,8 @@ class Playqueue(Thread): self.player.play(playqueue.kodi_pl, startpos=startpos) else: self.player.play(playqueue.kodi_pl) - log.debug('Playqueue at the end: %s' % playqueue) playqueue.log_Kodi_playlist() - @lockmethod.lockthis - def update_playqueue_with_companion(self, data): - """ - Feed with Plex companion data - """ - - # Get the correct queue - @lockmethod.lockthis def kodi_onadd(self, data): """ @@ -130,15 +121,13 @@ class Playqueue(Thread): u'position': 0 } """ - for playqueue in self.playqueues: - if playqueue.playlistid == data['playlistid']: - break + playqueue = self.playqueues[data['playlistid']] if playqueue.PKC_playlist_edits: old = (data['item'].get('id') if data['item'].get('id') else data['item'].get('file')) for i, item in enumerate(playqueue.PKC_playlist_edits): if old == item: - log.debug('kodimonitor told us of a PKC edit - ignore.') + log.debug('kodimonitor told us of a PKC edit - ignore') del playqueue.PKC_playlist_edits[i] return if playqueue.ID is None: @@ -148,15 +137,30 @@ class Playqueue(Thread): PL.add_playlist_item(playqueue, data['item'], data['position']) log.debug('Added a new item to the playqueue: %s' % playqueue) + @lockmethod.lockthis + def kodi_onremove(self, data): + """ + Called if an item is removed from a Kodi playqueue. Data is Kodi JSON- + RPC output, e.g. + {u'position': 2, u'playlistid': 1} + """ + playqueue = self.playqueues[data['playlistid']] + PL.delete_playlist_item(playqueue, data['position']) + log.debug('Deleted item at position %s. New playqueue: %s' + % (data['position'], playqueue)) + @lockmethod.lockthis def _compare_playqueues(self, playqueue, new): """ Used to poll the Kodi playqueue and update the Plex playqueue if needed """ + if self.threadStopped(): + # Chances are that we got an empty Kodi playlist due to Kodi exit + return old = playqueue.old_kodi_pl + index = list(range(0, len(old))) log.debug('Comparing new Kodi playqueue %s with our play queue %s' % (new, playqueue)) - index = list(range(0, len(old))) for i, new_item in enumerate(new): for j, old_item in enumerate(old): if old_item.get('id') is None: @@ -171,16 +175,10 @@ class Playqueue(Thread): # item now at pos i has been moved from original pos i+j PL.move_playlist_item(playqueue, i + j, i) # Delete the item we just found - del old[i + j], index[i + j] + del old[j], index[j] break - else: - # Did not find element i in the old list - Kodi monitor should - # pick this up! - # PL.add_playlist_item(playqueue, new_item, i-1) - pass - for i in index: - # Still got some old items left that need deleting - PL.delete_playlist_item(playqueue, i) + # New elements and left-over elements will be taken care of by the kodi + # monitor! log.debug('New playqueue: %s' % playqueue) def run(self):