Fix wrong item being reported using direct paths

- Fixes #428
This commit is contained in:
croneter 2018-03-15 08:24:56 +01:00
parent 2ba74bb95d
commit c48ef5012f

View file

@ -260,10 +260,6 @@ class KodiMonitor(Monitor):
kodi_id = json_item.get('id') kodi_id = json_item.get('id')
kodi_type = json_item.get('type') kodi_type = json_item.get('type')
path = json_item.get('file') path = json_item.get('file')
if not path and not kodi_id:
LOG.debug('Aborting playback report - no Kodi id or file for %s',
json_item)
raise RuntimeError
# Plex id will NOT be set with direct paths # Plex id will NOT be set with direct paths
plex_id = state.PLEX_IDS.get(path) plex_id = state.PLEX_IDS.get(path)
try: try:
@ -340,30 +336,39 @@ class KodiMonitor(Monitor):
playqueue = PQ.PLAYQUEUES[playerid] playqueue = PQ.PLAYQUEUES[playerid]
info = js.get_player_props(playerid) info = js.get_player_props(playerid)
json_item = js.get_item(playerid) json_item = js.get_item(playerid)
path = json_item.get('file')
pos = info['position'] if info['position'] != -1 else 0 pos = info['position'] if info['position'] != -1 else 0
LOG.debug('Detected position %s for %s', pos, playqueue) LOG.debug('Detected position %s for %s', pos, playqueue)
LOG.debug('Detected Kodi playing item properties: %s', json_item)
status = state.PLAYER_STATES[playerid] status = state.PLAYER_STATES[playerid]
path = json_item.get('file')
try: try:
item = playqueue.items[pos] item = playqueue.items[pos]
except IndexError: except IndexError:
try: # PKC playqueue not yet initialized
kodi_id, kodi_type, plex_id, plex_type = self._get_ids(json_item) initialize = True
except RuntimeError: else:
return if item.kodi_id:
LOG.info('Need to initialize Plex and PKC playqueue') if (item.kodi_id != json_item.get('id') or
try: item.kodi_type != json_item.get('type')):
if plex_id: initialize = True
item = PL.init_Plex_playlist(playqueue, plex_id=plex_id)
else: else:
item = PL.init_Plex_playlist(playqueue, initialize = False
kodi_item={'id': kodi_id, else:
'type': kodi_type, # E.g. clips set-up previously with no Kodi DB entry
'file': path}) if item.file != json_item.get('file'):
except PL.PlaylistError: initialize = True
LOG.info('Could not initialize our playlist') else:
# Avoid errors initialize = False
item = PL.Playlist_Item() if initialize:
LOG.debug('Need to initialize Plex and PKC playqueue')
if not json_item.get('id') or not json_item.get('type'):
LOG.debug('No Kodi id or type obtained - aborting report')
return
kodi_id, kodi_type, plex_id, plex_type = self._get_ids(json_item)
if not plex_id:
LOG.debug('No Plex id obtained - aborting playback report')
return
item = PL.init_Plex_playlist(playqueue, plex_id=plex_id)
# Set the Plex container key (e.g. using the Plex playqueue) # Set the Plex container key (e.g. using the Plex playqueue)
container_key = None container_key = None
if info['playlistid'] != -1: if info['playlistid'] != -1:
@ -375,6 +380,7 @@ class KodiMonitor(Monitor):
container_key = '/library/metadata/%s' % plex_id container_key = '/library/metadata/%s' % plex_id
LOG.debug('Set the Plex container_key to: %s', container_key) LOG.debug('Set the Plex container_key to: %s', container_key)
else: else:
LOG.debug('No need to initialize playqueues')
kodi_id = item.kodi_id kodi_id = item.kodi_id
kodi_type = item.kodi_type kodi_type = item.kodi_type
plex_id = item.plex_id plex_id = item.plex_id