Major Plex Companion overhaul, part 5

This commit is contained in:
tomkat83 2017-12-28 21:31:05 +01:00
parent e4ea7692b2
commit 2f90674f51
4 changed files with 43 additions and 8 deletions

View file

@ -93,7 +93,7 @@ class PlexCompanion(Thread):
def _process_playlist(self, data): def _process_playlist(self, data):
# Get the playqueue ID # Get the playqueue ID
try: try:
_, plex_id, query = ParseContainerKey(data['containerKey']) _, container_key, query = ParseContainerKey(data['containerKey'])
except: except:
LOG.error('Exception while processing') LOG.error('Exception while processing')
import traceback import traceback
@ -114,9 +114,13 @@ class PlexCompanion(Thread):
api = API(xml[0]) api = API(xml[0])
playqueue = self.mgr.playqueue.get_playqueue_from_type( playqueue = self.mgr.playqueue.get_playqueue_from_type(
v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()]) v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()])
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( self.mgr.playqueue.update_playqueue_from_PMS(
playqueue, playqueue,
playqueue_id=plex_id, playqueue_id=container_key,
repeat=query.get('repeat'), repeat=query.get('repeat'),
offset=data.get('offset'), offset=data.get('offset'),
transient_token=data.get('key')) transient_token=data.get('key'))

View file

@ -199,6 +199,10 @@ class KodiMonitor(Monitor):
Will NOT be called if playback initiated by Kodi widgets Will NOT be called if playback initiated by Kodi widgets
""" """
playqueue = self.playqueue.playqueues[data['playlistid']] 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 # Check whether we even need to update our known playqueue
kodi_playqueue = js.playlist_get_items(data['playlistid']) kodi_playqueue = js.playlist_get_items(data['playlistid'])
if playqueue.old_kodi_pl == kodi_playqueue: if playqueue.old_kodi_pl == kodi_playqueue:

View file

@ -42,6 +42,9 @@ class PlaylistObjectBaseclase(object):
self.shuffled = 0 self.shuffled = 0
self.repeat = 0 self.repeat = 0
self.plex_transient_token = None 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): def __repr__(self):
""" """
@ -61,6 +64,25 @@ class PlaylistObjectBaseclase(object):
answ += '\'%s\': %s, ' % (key, str(getattr(self, key))) answ += '\'%s\': %s, ' % (key, str(getattr(self, key)))
return answ + '\'items\': %s}}' % self.items 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): def clear(self):
""" """
Resets the playlist object to an empty playlist 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)} params['item'] = {'%sid' % item.kodi_type: int(item.kodi_id)}
else: else:
params['item'] = {'file': item.file} params['item'] = {'file': item.file}
playlist.kodi_onadd()
reply = js.playlist_insert(params) reply = js.playlist_insert(params)
if reply.get('error') is not None: if reply.get('error') is not None:
LOG.error('Could not add item to playlist. Kodi reply. %s', reply) LOG.error('Could not add item to playlist. Kodi reply. %s', reply)
playlist.is_kodi_onadd()
return False return False
return True 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)} params['item'] = {'%sid' % kodi_type: int(kodi_id)}
else: else:
params['item'] = {'file': file} params['item'] = {'file': file}
playlist.kodi_onadd()
reply = js.playlist_insert(params) reply = js.playlist_insert(params)
if reply.get('error') is not None: if reply.get('error') is not None:
LOG.error('Could not add item to playlist. Kodi reply. %s', reply) LOG.error('Could not add item to playlist. Kodi reply. %s', reply)
playlist.is_kodi_onadd()
return False return False
item = playlist_item_from_kodi( item = playlist_item_from_kodi(
{'id': kodi_id, 'type': kodi_type, 'file': file}) {'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', LOG.debug('Insert listitem at position %s for Kodi only for %s',
pos, playlist) pos, playlist)
# Add the item into Kodi playlist # Add the item into Kodi playlist
playlist.kodi_onadd()
playlist.kodi_pl.add(file, listitem, index=pos) playlist.kodi_pl.add(file, listitem, index=pos)
# We need to add this to our internal queue as well # We need to add this to our internal queue as well
if xml_video_element is not None: if xml_video_element is not None:

View file

@ -122,9 +122,9 @@ class Playqueue(Thread):
except KeyError: except KeyError:
LOG.error('Could not get playqueue ID %s', playqueue_id) LOG.error('Could not get playqueue ID %s', playqueue_id)
return return
PlaybackUtils(xml, playqueue).play_all()
playqueue.repeat = 0 if not repeat else int(repeat) playqueue.repeat = 0 if not repeat else int(repeat)
playqueue.token = transient_token playqueue.token = transient_token
PlaybackUtils(xml, playqueue).play_all()
window('plex_customplaylist', value="true") window('plex_customplaylist', value="true")
if offset not in (None, "0"): if offset not in (None, "0"):
window('plex_customplaylist.seektime', window('plex_customplaylist.seektime',