Attach PMS xml piece to playlist item

This commit is contained in:
croneter 2017-12-07 17:25:24 +01:00
parent 2a6d8757e6
commit 65a48ebe7b

View file

@ -128,6 +128,9 @@ class Playlist_Item(object):
guid = None guid = None
xml = None xml = None
# Yet to be implemented: handling of a movie with several parts
part = 0
def __repr__(self): def __repr__(self):
""" """
Print the playlist item, e.g. to log Print the playlist item, e.g. to log
@ -203,6 +206,8 @@ def playlist_item_from_plex(plex_id):
def playlist_item_from_xml(playlist, xml_video_element): def playlist_item_from_xml(playlist, xml_video_element):
""" """
Returns a playlist element for the playqueue using the Plex xml Returns a playlist element for the playqueue using the Plex xml
xml_video_element: etree xml piece 1 level underneath <MediaContainer>
""" """
item = Playlist_Item() item = Playlist_Item()
api = API(xml_video_element) api = API(xml_video_element)
@ -219,6 +224,7 @@ def playlist_item_from_xml(playlist, xml_video_element):
item.kodi_id, item.kodi_type = int(db_element[0]), db_element[4] item.kodi_id, item.kodi_type = int(db_element[0]), db_element[4]
except TypeError: except TypeError:
pass pass
item.xml = xml_video_element
log.debug('Created new playlist item from xml: %s' % item) log.debug('Created new playlist item from xml: %s' % item)
return item return item
@ -285,8 +291,9 @@ def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
def init_Plex_playlist(playlist, plex_id=None, kodi_item=None): def init_Plex_playlist(playlist, plex_id=None, kodi_item=None):
""" """
Initializes the Plex side without changing the Kodi playlists Initializes the Plex side without changing the Kodi playlists
WILL ALSO UPDATE OUR PLAYLISTS.
WILL ALSO UPDATE OUR PLAYLISTS Returns True if successful, False otherwise
""" """
log.debug('Initializing the playlist %s on the Plex side' % playlist) log.debug('Initializing the playlist %s on the Plex side' % playlist)
try: try:
@ -303,11 +310,14 @@ def init_Plex_playlist(playlist, plex_id=None, kodi_item=None):
action_type="POST", action_type="POST",
parameters=params) parameters=params)
get_playlist_details_from_xml(playlist, xml) get_playlist_details_from_xml(playlist, xml)
except KeyError: item.xml = xml[0]
log.error('Could not init Plex playlist') except (KeyError, IndexError, TypeError):
return log.error('Could not init Plex playlist with plex_id %s and '
'kodi_item %s', plex_id, kodi_item)
return False
playlist.items.append(item) playlist.items.append(item)
log.debug('Initialized the playlist on the Plex side: %s' % playlist) log.debug('Initialized the playlist on the Plex side: %s' % playlist)
return True
def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None, def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None,
@ -351,41 +361,56 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
log.debug('add_item_to_playlist. Playlist before adding: %s' % playlist) log.debug('add_item_to_playlist. Playlist before adding: %s' % playlist)
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file} kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
if playlist.ID is None: if playlist.ID is None:
init_Plex_playlist(playlist, plex_id, kodi_item) success = init_Plex_playlist(playlist, plex_id, kodi_item)
else: else:
add_item_to_PMS_playlist(playlist, pos, plex_id, kodi_item) success = add_item_to_PMS_playlist(playlist, pos, plex_id, kodi_item)
kodi_id = playlist.items[pos].kodi_id if success is False:
kodi_type = playlist.items[pos].kodi_type return False
file = playlist.items[pos].file # Now add the item to the Kodi playlist - WITHOUT adding it to our PKC pl
add_item_to_kodi_playlist(playlist, pos, kodi_id, kodi_type, file) item = playlist.items[pos]
params = {
'playlistid': playlist.playlistid,
'position': pos
}
if item.kodi_id is not None:
params['item'] = {'%sid' % item.kodi_type: int(item.kodi_id)}
else:
params['item'] = {'file': item.file}
reply = JSONRPC('Playlist.Insert').execute(params)
if reply.get('error') is not None:
log.error('Could not add item to playlist. Kodi reply. %s', reply)
return False
return True
def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None): def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
""" """
Adds a new item to the playlist at position pos [int] only on the Plex Adds a new item to the playlist at position pos [int] only on the Plex
side of things (e.g. because the user changed the Kodi side) side of things (e.g. because the user changed the Kodi side)
WILL ALSO UPDATE OUR PLAYLISTS WILL ALSO UPDATE OUR PLAYLISTS
Returns True if successful, False otherwise
""" """
if plex_id: if plex_id:
try: try:
item = playlist_item_from_plex(plex_id) item = playlist_item_from_plex(plex_id)
except KeyError: except KeyError:
log.error('Could not add new item to the PMS playlist') log.error('Could not add new item to the PMS playlist')
return return False
else: else:
item = playlist_item_from_kodi(kodi_item) item = playlist_item_from_kodi(kodi_item)
url = "{server}/%ss/%s?uri=%s" % (playlist.kind, playlist.ID, item.uri) url = "{server}/%ss/%s?uri=%s" % (playlist.kind, playlist.ID, item.uri)
# Will always put the new item at the end of the Plex playlist # Will always put the new item at the end of the Plex playlist
xml = DU().downloadUrl(url, action_type="PUT") xml = DU().downloadUrl(url, action_type="PUT")
try: try:
item.xml = xml[-1]
item.ID = xml[-1].attrib['%sItemID' % playlist.kind] item.ID = xml[-1].attrib['%sItemID' % playlist.kind]
except IndexError: except IndexError:
log.info('Could not get playlist children. Adding a dummy') log.info('Could not get playlist children. Adding a dummy')
except (TypeError, AttributeError, KeyError): except (TypeError, AttributeError, KeyError):
log.error('Could not add item %s to playlist %s' log.error('Could not add item %s to playlist %s'
% (kodi_item, playlist)) % (kodi_item, playlist))
return return False
# Get the guid for this item # Get the guid for this item
for plex_item in xml: for plex_item in xml:
if plex_item.attrib['%sItemID' % playlist.kind] == item.ID: if plex_item.attrib['%sItemID' % playlist.kind] == item.ID:
@ -400,6 +425,7 @@ def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
len(playlist.items) - 1, len(playlist.items) - 1,
pos) pos)
log.debug('Successfully added item on the Plex side: %s' % playlist) log.debug('Successfully added item on the Plex side: %s' % playlist)
return True
def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None, def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
@ -426,10 +452,16 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
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)
return False return False
else: item = playlist_item_from_kodi(
playlist.items.insert(pos, playlist_item_from_kodi( {'id': kodi_id, 'type': kodi_type, 'file': file}, playlist)
{'id': kodi_id, 'type': kodi_type, 'file': file})) if item.plex_id is not None:
return True xml = GetPlexMetadata(item.plex_id)
try:
item.xml = xml[-1]
except (TypeError, IndexError):
log.error('Could not get metadata for playlist item %s', item)
playlist.items.insert(pos, item)
return True
def move_playlist_item(playlist, before_pos, after_pos): def move_playlist_item(playlist, before_pos, after_pos):
@ -563,8 +595,7 @@ def add_to_Kodi_playlist(playlist, xml_video_element):
log.error('Could not add item %s to Kodi playlist. Error: %s' log.error('Could not add item %s to Kodi playlist. Error: %s'
% (xml_video_element, reply)) % (xml_video_element, reply))
return None return None
else: return item
return item
def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file, def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file,