playQueue fixes

This commit is contained in:
tomkat83 2016-12-28 14:48:23 +01:00
parent 14c9d10459
commit 4208bb9b73
3 changed files with 36 additions and 28 deletions

View file

@ -173,6 +173,11 @@ class KodiMonitor(xbmc.Monitor):
# u'position': 0} # u'position': 0}
self.playqueue.kodi_onadd(data) 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): def PlayBackStart(self, data):
""" """
Called whenever a playback is started Called whenever a playback is started

View file

@ -30,7 +30,11 @@ class Playlist_Object_Baseclase(object):
def __repr__(self): def __repr__(self):
answ = "<%s: " % (self.__class__.__name__) 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__: for key in self.__dict__:
if key not in ("ID", 'items'):
answ += '%s: %s, ' % (key, getattr(self, key)) answ += '%s: %s, ' % (key, getattr(self, key))
return answ[:-2] + ">" return answ[:-2] + ">"
@ -88,8 +92,8 @@ def playlist_item_from_kodi_item(kodi_item):
kodi_item dict contains keys 'id', 'type', 'file' (if applicable) kodi_item dict contains keys 'id', 'type', 'file' (if applicable)
""" """
item = Playlist_Item() item = Playlist_Item()
if kodi_item.get('id'): item.kodi_id = kodi_item.get('id')
item.kodi_id = kodi_item['id'] if item.kodi_id:
with embydb.GetEmbyDB() as emby_db: with embydb.GetEmbyDB() as emby_db:
emby_dbitem = emby_db.getItem_byKodiId(kodi_item['id'], emby_dbitem = emby_db.getItem_byKodiId(kodi_item['id'],
kodi_item['type']) 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] 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: if after_pos == 0:
url = "{server}/%ss/%s/items/%s/move?after=0" % \ url = "{server}/%ss/%s/items/%s/move?after=0" % \
(playlist.kind, (playlist.kind,
@ -256,7 +261,7 @@ def delete_playlist_item(playlist, pos):
playlist.repeat), playlist.repeat),
action_type="DELETE") action_type="DELETE")
_get_playListVersion_from_xml(playlist, xml) _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): def get_kodi_playlist_items(playlist):

View file

@ -90,7 +90,7 @@ class Playqueue(Thread):
repeat = 0, 1, 2 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) PL.update_playlist_from_PMS(playqueue, playqueue_id, repeat)
log.debug('Updated playqueue: %s' % playqueue) log.debug('Updated playqueue: %s' % playqueue)
@ -108,17 +108,8 @@ class Playqueue(Thread):
self.player.play(playqueue.kodi_pl, startpos=startpos) self.player.play(playqueue.kodi_pl, startpos=startpos)
else: else:
self.player.play(playqueue.kodi_pl) self.player.play(playqueue.kodi_pl)
log.debug('Playqueue at the end: %s' % playqueue)
playqueue.log_Kodi_playlist() playqueue.log_Kodi_playlist()
@lockmethod.lockthis
def update_playqueue_with_companion(self, data):
"""
Feed with Plex companion data
"""
# Get the correct queue
@lockmethod.lockthis @lockmethod.lockthis
def kodi_onadd(self, data): def kodi_onadd(self, data):
""" """
@ -130,15 +121,13 @@ class Playqueue(Thread):
u'position': 0 u'position': 0
} }
""" """
for playqueue in self.playqueues: playqueue = self.playqueues[data['playlistid']]
if playqueue.playlistid == data['playlistid']:
break
if playqueue.PKC_playlist_edits: if playqueue.PKC_playlist_edits:
old = (data['item'].get('id') if data['item'].get('id') old = (data['item'].get('id') if data['item'].get('id')
else data['item'].get('file')) else data['item'].get('file'))
for i, item in enumerate(playqueue.PKC_playlist_edits): for i, item in enumerate(playqueue.PKC_playlist_edits):
if old == item: 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] del playqueue.PKC_playlist_edits[i]
return return
if playqueue.ID is None: if playqueue.ID is None:
@ -148,15 +137,30 @@ class Playqueue(Thread):
PL.add_playlist_item(playqueue, data['item'], data['position']) PL.add_playlist_item(playqueue, data['item'], data['position'])
log.debug('Added a new item to the playqueue: %s' % playqueue) 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 @lockmethod.lockthis
def _compare_playqueues(self, playqueue, new): def _compare_playqueues(self, playqueue, new):
""" """
Used to poll the Kodi playqueue and update the Plex playqueue if needed 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 old = playqueue.old_kodi_pl
index = list(range(0, len(old)))
log.debug('Comparing new Kodi playqueue %s with our play queue %s' log.debug('Comparing new Kodi playqueue %s with our play queue %s'
% (new, playqueue)) % (new, playqueue))
index = list(range(0, len(old)))
for i, new_item in enumerate(new): for i, new_item in enumerate(new):
for j, old_item in enumerate(old): for j, old_item in enumerate(old):
if old_item.get('id') is None: 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 # item now at pos i has been moved from original pos i+j
PL.move_playlist_item(playqueue, i + j, i) PL.move_playlist_item(playqueue, i + j, i)
# Delete the item we just found # Delete the item we just found
del old[i + j], index[i + j] del old[j], index[j]
break break
else: # New elements and left-over elements will be taken care of by the kodi
# Did not find element i in the old list - Kodi monitor should # monitor!
# 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)
log.debug('New playqueue: %s' % playqueue) log.debug('New playqueue: %s' % playqueue)
def run(self): def run(self):