From 80abe0b34b189607a7ba4ed18794cc3009496e89 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 10 Aug 2019 12:01:23 +0200 Subject: [PATCH] Fix playback report not working after having played a non-Plex video file --- resources/lib/kodimonitor.py | 6 +++++- resources/lib/playlist_func.py | 7 +++++-- resources/lib/playqueue.py | 14 +++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index 8df104de..a7b615ab 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -398,7 +398,11 @@ class KodiMonitor(xbmc.Monitor): LOG.debug('No Plex id obtained - aborting playback report') app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template) return - item = PL.init_plex_playqueue(playqueue, plex_id=plex_id) + try: + item = PL.init_plex_playqueue(playqueue, plex_id=plex_id) + except PL.PlaylistError: + LOG.info('Could not initialize the Plex playlist') + return item.file = path # Set the Plex container key (e.g. using the Plex playqueue) container_key = None diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index d32912f8..39783cfd 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -358,7 +358,8 @@ def verify_kodi_item(plex_id, kodi_item): set to None if unsuccessful. Will raise a PlaylistError if plex_id is None and kodi_item['file'] starts - with either 'plugin' or 'http' + with either 'plugin' or 'http'. + Will raise KeyError if neither plex_id nor kodi_id are found """ if plex_id is not None or kodi_item.get('id') is not None: # Got all the info we need @@ -388,6 +389,8 @@ def verify_kodi_item(plex_id, kodi_item): db_type='music') kodi_item['id'] = kodi_id kodi_item['type'] = None if kodi_id is None else kodi_type + if plex_id is None and kodi_id is None: + raise KeyError('Neither Plex nor Kodi id found for %s' % kodi_item) LOG.debug('Research results for kodi_item: %s', kodi_item) return kodi_item @@ -508,8 +511,8 @@ def init_plex_playqueue(playlist, plex_id=None, kodi_item=None): Returns the first PKC playlist item or raises PlaylistError """ LOG.debug('Initializing the playqueue on the Plex side: %s', playlist) - playlist.clear(kodi=False) verify_kodi_item(plex_id, kodi_item) + playlist.clear(kodi=False) try: if plex_id: item = playlist_item_from_plex(plex_id) diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index f9fed4e4..7e78bee9 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -86,7 +86,11 @@ def init_playqueue_from_plex_children(plex_id, transient_token=None): playqueue.clear() for i, child in enumerate(xml): api = API(child) - PL.add_item_to_playlist(playqueue, i, plex_id=api.plex_id) + try: + PL.add_item_to_playlist(playqueue, i, plex_id=api.plex_id) + except PL.PlaylistError: + LOG.error('Could not add Plex item to our playlist: %s, %s', + child.tag, child.attrib) playqueue.plex_transient_token = transient_token LOG.debug('Firing up Kodi player') app.APP.player.play(playqueue.kodi_pl, None, False, 0) @@ -166,6 +170,14 @@ class PlayqueueMonitor(backgroundthread.KillableThread): except PL.PlaylistError: # Could not add the element pass + except KeyError: + # Catches KeyError from PL.verify_kodi_item() + # Hack: Kodi already started playback of a new item and we + # started playback already using kodimonitors + # PlayBackStart(), but the Kodi playlist STILL only shows + # the old element. Hence ignore playlist difference here + LOG.debug('Detected an outdated Kodi playlist - ignoring') + return except IndexError: # This is really a hack - happens when using Addon Paths # and repeatedly starting the same element. Kodi will then