Refactoring: playlist and playqueue items to use API instead of xml

This commit is contained in:
croneter 2021-09-13 13:26:04 +02:00
parent 176fa07e80
commit 2bd692e173
3 changed files with 31 additions and 31 deletions

View file

@ -344,8 +344,7 @@ class KodiMonitor(xbmc.Monitor):
container_key = '/library/metadata/%s' % plex_id container_key = '/library/metadata/%s' % plex_id
# Mechanik for Plex skip intro feature # Mechanik for Plex skip intro feature
if utils.settings('enableSkipIntro') == 'true': if utils.settings('enableSkipIntro') == 'true':
api = API(item.xml) status['intro_markers'] = item.api.intro_markers()
status['intro_markers'] = api.intro_markers()
# Remember the currently playing item # Remember the currently playing item
app.PLAYSTATE.item = item app.PLAYSTATE.item = item
# Remember that this player has been active # Remember that this player has been active
@ -593,11 +592,10 @@ def _next_episode(current_api):
current_api.grandparent_title()) current_api.grandparent_title())
return return
try: try:
next_api = API(xml[counter + 1]) return API(xml[counter + 1])
except IndexError: except IndexError:
# Was the last episode # Was the last episode
return pass
return next_api
def _complete_artwork_keys(info): def _complete_artwork_keys(info):
@ -623,7 +621,7 @@ def _notify_upnext(item):
""" """
if not item.plex_type == v.PLEX_TYPE_EPISODE: if not item.plex_type == v.PLEX_TYPE_EPISODE:
return return
this_api = API(item.xml) this_api = item.api
next_api = _next_episode(this_api) next_api = _next_episode(this_api)
if next_api is None: if next_api is None:
return return

View file

@ -445,27 +445,26 @@ def _conclude_playback(playqueue, pos):
""" """
LOG.debug('Concluding playback for playqueue position %s', pos) LOG.debug('Concluding playback for playqueue position %s', pos)
item = playqueue.items[pos] item = playqueue.items[pos]
api = API(item.xml) if item.api.mediastream_number() is None:
if api.mediastream_number() is None:
# E.g. user could choose between several media streams and cancelled # E.g. user could choose between several media streams and cancelled
LOG.debug('Did not get a mediastream_number') LOG.debug('Did not get a mediastream_number')
_ensure_resolve() _ensure_resolve()
return return
api.part = item.part or 0 item.api.part = item.part or 0
playback_decision.set_pkc_playmethod(api, item) playback_decision.set_pkc_playmethod(item.api, item)
if not playback_decision.audio_subtitle_prefs(api, item): if not playback_decision.audio_subtitle_prefs(item.api, item):
LOG.info('Did not set audio subtitle prefs, aborting silently') LOG.info('Did not set audio subtitle prefs, aborting silently')
_ensure_resolve() _ensure_resolve()
return return
playback_decision.set_playurl(api, item) playback_decision.set_playurl(item.api, item)
if not item.file: if not item.file:
LOG.info('Did not get a playurl, aborting playback silently') LOG.info('Did not get a playurl, aborting playback silently')
_ensure_resolve() _ensure_resolve()
return return
listitem = api.listitem(listitem=transfer.PKCListItem, resume=False) listitem = item.api.listitem(listitem=transfer.PKCListItem, resume=False)
listitem.setPath(item.file.encode('utf-8')) listitem.setPath(item.file.encode('utf-8'))
if item.playmethod != v.PLAYBACK_METHOD_DIRECT_PATH: if item.playmethod != v.PLAYBACK_METHOD_DIRECT_PATH:
listitem.setSubtitles(api.cache_external_subs()) listitem.setSubtitles(item.api.cache_external_subs())
transfer.send(listitem) transfer.send(listitem)
LOG.debug('Done concluding playback') LOG.debug('Done concluding playback')

View file

@ -149,13 +149,14 @@ class PlaylistItem(object):
file = None [str] Path to the item's file. STRING!! file = None [str] Path to the item's file. STRING!!
uri = None [str] PMS path to item; will be auto-set with plex_id uri = None [str] PMS path to item; will be auto-set with plex_id
guid = None [str] Weird Plex guid guid = None [str] Weird Plex guid
xml = None [etree] XML from PMS, 1 lvl below <MediaContainer> api = None [API] API of xml 1 lvl below <MediaContainer>
playmethod = None [str] either 'DirectPath', 'DirectStream', 'Transcode' playmethod = None [str] either 'DirectPath', 'DirectStream', 'Transcode'
playcount = None [int] how many times the item has already been played playcount = None [int] how many times the item has already been played
offset = None [int] the item's view offset UPON START in Plex time offset = None [int] the item's view offset UPON START in Plex time
part = 0 [int] part number if Plex video consists of mult. parts part = 0 [int] part number if Plex video consists of mult. parts
force_transcode [bool] defaults to False force_transcode [bool] defaults to False
""" """
def __init__(self): def __init__(self):
self.id = None self.id = None
self._plex_id = None self._plex_id = None
@ -165,7 +166,7 @@ class PlaylistItem(object):
self.file = None self.file = None
self._uri = None self._uri = None
self.guid = None self.guid = None
self.xml = None self.api = None
self.playmethod = None self.playmethod = None
self.playcount = None self.playcount = None
self.offset = None self.offset = None
@ -226,10 +227,10 @@ class PlaylistItem(object):
count = 0 count = 0
if kodi_stream_index == -1: if kodi_stream_index == -1:
# Kodi telling us "it's the last one" # Kodi telling us "it's the last one"
iterator = list(reversed(self.xml[0][self.part])) iterator = list(reversed(self.api.plex_media_streams()))
kodi_stream_index = 0 kodi_stream_index = 0
else: else:
iterator = self.xml[0][self.part] iterator = self.api.plex_media_streams()
# Kodi indexes differently than Plex # Kodi indexes differently than Plex
for stream in iterator: for stream in iterator:
if (stream.get('streamType') == stream_type and if (stream.get('streamType') == stream_type and
@ -270,7 +271,7 @@ class PlaylistItem(object):
Returns None if no stream has been selected Returns None if no stream has been selected
""" """
stream_type = v.PLEX_STREAM_TYPE_FROM_STREAM_TYPE[stream_type] stream_type = v.PLEX_STREAM_TYPE_FROM_STREAM_TYPE[stream_type]
for stream in self.xml[0][self.part]: for stream in self.api.plex_media_streams():
if stream.get('streamType') == stream_type \ if stream.get('streamType') == stream_type \
and stream.get('selected') == '1': and stream.get('selected') == '1':
return (utils.cast(int, stream.get('id')), return (utils.cast(int, stream.get('id')),
@ -287,9 +288,9 @@ class PlaylistItem(object):
if stream_type == '3': if stream_type == '3':
streams = accessible_plex_subtitles(self.playmethod, streams = accessible_plex_subtitles(self.playmethod,
self.file, self.file,
self.xml[0][self.part]) self.api.plex_media_streams())
else: else:
streams = [x for x in self.xml[0][self.part] streams = [x for x in self.api.plex_media_streams()
if x.get('streamType') == stream_type] if x.get('streamType') == stream_type]
return streams return streams
@ -415,14 +416,15 @@ def playlist_item_from_xml(xml_video_element, kodi_id=None, kodi_type=None):
item.guid = api.guid_html_escaped() item.guid = api.guid_html_escaped()
item.playcount = api.viewcount() item.playcount = api.viewcount()
item.offset = api.resume_point() item.offset = api.resume_point()
item.xml = xml_video_element item.api = api
LOG.debug('Created new playlist item from xml: %s', item) LOG.debug('Created new playlist item from xml: %s', item)
return item return item
def _get_playListVersion_from_xml(playlist, xml): def _update_playlist_version(playlist, xml):
""" """
Takes a PMS xml as input to overwrite the playlist version (e.g. Plex Takes a PMS xml (one level above the xml-depth where we're usually applying
API()) as input to overwrite the playlist version (e.g. Plex
playQueueVersion). playQueueVersion).
Raises PlaylistError if unsuccessful Raises PlaylistError if unsuccessful
@ -605,7 +607,7 @@ def add_item_to_plex_playqueue(playlist, pos, plex_id=None, kodi_item=None):
raise PlaylistError('Could not add item %s to playlist %s' raise PlaylistError('Could not add item %s to playlist %s'
% (kodi_item, playlist)) % (kodi_item, playlist))
api = API(xml[-1]) api = API(xml[-1])
item.xml = xml[-1] item.api = api
item.id = api.item_id() item.id = api.item_id()
item.guid = api.guid_html_escaped() item.guid = api.guid_html_escaped()
item.offset = api.resume_point() item.offset = api.resume_point()
@ -613,7 +615,7 @@ def add_item_to_plex_playqueue(playlist, pos, plex_id=None, kodi_item=None):
playlist.items.append(item) playlist.items.append(item)
if pos == len(playlist.items) - 1: if pos == len(playlist.items) - 1:
# Item was added at the end # Item was added at the end
_get_playListVersion_from_xml(playlist, xml) _update_playlist_version(playlist, xml)
else: else:
# Move the new item to the correct position # Move the new item to the correct position
move_playlist_item(playlist, move_playlist_item(playlist,
@ -657,7 +659,7 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
{'id': kodi_id, 'type': kodi_type, 'file': file}) {'id': kodi_id, 'type': kodi_type, 'file': file})
if item.plex_id is not None: if item.plex_id is not None:
xml = PF.GetPlexMetadata(item.plex_id) xml = PF.GetPlexMetadata(item.plex_id)
item.xml = xml[-1] item.api = API(xml[-1])
playlist.items.insert(pos, item) playlist.items.insert(pos, item)
return item return item
@ -681,9 +683,10 @@ def move_playlist_item(playlist, before_pos, after_pos):
playlist.id, playlist.id,
playlist.items[before_pos].id, playlist.items[before_pos].id,
playlist.items[after_pos - 1].id) playlist.items[after_pos - 1].id)
# We need to increment the playlistVersion # Tell the PMS that we're moving items around
_get_playListVersion_from_xml( xml = DU().downloadUrl(url, action_type="PUT")
playlist, DU().downloadUrl(url, action_type="PUT")) # We need to increment the playlist version for communicating with the PMS
_update_playlist_version(playlist, xml)
# Move our item's position in our internal playlist # Move our item's position in our internal playlist
playlist.items.insert(after_pos, playlist.items.pop(before_pos)) playlist.items.insert(after_pos, playlist.items.pop(before_pos))
LOG.debug('Done moving for %s', playlist) LOG.debug('Done moving for %s', playlist)
@ -731,7 +734,7 @@ def delete_playlist_item_from_PMS(playlist, pos):
playlist.repeat), playlist.repeat),
action_type="DELETE") action_type="DELETE")
del playlist.items[pos] del playlist.items[pos]
_get_playListVersion_from_xml(playlist, xml) _update_playlist_version(playlist, xml)
# Functions operating on the Kodi playlist objects ########## # Functions operating on the Kodi playlist objects ##########