Improvements to building PKC playlist elements

This commit is contained in:
croneter 2018-02-23 13:06:18 +01:00
parent 861f6213f1
commit 0b2592be5e

View file

@ -8,7 +8,7 @@ from re import compile as re_compile
import plexdb_functions as plexdb import plexdb_functions as plexdb
from downloadutils import DownloadUtils as DU from downloadutils import DownloadUtils as DU
from utils import try_encode, escape_html from utils import try_encode
from PlexAPI import API from PlexAPI import API
from PlexFunctions import GetPlexMetadata from PlexFunctions import GetPlexMetadata
from kodidb_functions import kodiid_from_filename from kodidb_functions import kodiid_from_filename
@ -308,8 +308,7 @@ def playlist_item_from_plex(plex_id):
return item return item
def playlist_item_from_xml(playlist, xml_video_element, kodi_id=None, def playlist_item_from_xml(xml_video_element, kodi_id=None, kodi_type=None):
kodi_type=None):
""" """
Returns a playlist element for the playqueue using the Plex xml Returns a playlist element for the playqueue using the Plex xml
@ -319,13 +318,9 @@ def playlist_item_from_xml(playlist, xml_video_element, kodi_id=None,
api = API(xml_video_element) api = API(xml_video_element)
item.plex_id = api.plex_id() item.plex_id = api.plex_id()
item.plex_type = api.plex_type() item.plex_type = api.plex_type()
try: # item.id will only be set if you passed in an xml_video_element from e.g.
item.id = xml_video_element.attrib['%sItemID' % playlist.kind] # a playQueue
except KeyError: item.id = api.item_id()
pass
item.guid = xml_video_element.attrib.get('guid')
if item.guid is not None:
item.guid = escape_html(item.guid)
if kodi_id is not None: if kodi_id is not None:
item.kodi_id = kodi_id item.kodi_id = kodi_id
item.kodi_type = kodi_type item.kodi_type = kodi_type
@ -333,9 +328,12 @@ def playlist_item_from_xml(playlist, xml_video_element, kodi_id=None,
with plexdb.Get_Plex_DB() as plex_db: with plexdb.Get_Plex_DB() as plex_db:
db_element = plex_db.getItem_byId(item.plex_id) db_element = plex_db.getItem_byId(item.plex_id)
try: try:
item.kodi_id, item.kodi_type = int(db_element[0]), db_element[4] item.kodi_id, item.kodi_type = db_element[0], db_element[4]
except TypeError: except TypeError:
pass pass
item.guid = api.guid_html_escaped()
item.playcount = api.viewcount()
item.offset = api.resume_point()
item.xml = xml_video_element 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
@ -420,7 +418,7 @@ def init_Plex_playlist(playlist, plex_id=None, kodi_item=None):
parameters=params) parameters=params)
get_playlist_details_from_xml(playlist, xml) get_playlist_details_from_xml(playlist, xml)
# Need to get the details for the playlist item # Need to get the details for the playlist item
item = playlist_item_from_xml(playlist, xml[0]) item = playlist_item_from_xml(xml[0])
except (KeyError, IndexError, TypeError): except (KeyError, IndexError, TypeError):
raise PlaylistError('Could not init Plex playlist with plex_id %s and ' raise PlaylistError('Could not init Plex playlist with plex_id %s and '
'kodi_item %s' % (plex_id, kodi_item)) 'kodi_item %s' % (plex_id, kodi_item))
@ -484,8 +482,8 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
params['item'] = {'file': item.file} params['item'] = {'file': item.file}
reply = js.playlist_insert(params) reply = js.playlist_insert(params)
if reply.get('error') is not None: if reply.get('error') is not None:
raise PlaylistError('Could not add item to playlist. Kodi reply. %s', raise PlaylistError('Could not add item to playlist. Kodi reply. %s'
reply) % reply)
return item return item
@ -506,17 +504,16 @@ def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
# 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] xml[-1].attrib
item.id = xml[-1].attrib['%sItemID' % playlist.kind] except (TypeError, AttributeError, KeyError, IndexError):
except IndexError: raise PlaylistError('Could not add item %s to playlist %s'
LOG.info('Could not get playlist children. Adding a dummy') % (kodi_item, playlist))
except (TypeError, AttributeError, KeyError): api = API(xml[-1])
raise PlaylistError('Could not add item %s to playlist %s', item.xml = xml[-1]
kodi_item, playlist) item.id = api.item_id()
# Get the guid for this item item.guid = api.guid_html_escaped()
for plex_item in xml: item.offset = api.resume_point()
if plex_item.attrib['%sItemID' % playlist.kind] == item.id: item.playcount = api.viewcount()
item.guid = escape_html(plex_item.attrib['guid'])
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
@ -555,7 +552,7 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
raise PlaylistError('Could not add item to playlist. Kodi reply. %s', raise PlaylistError('Could not add item to playlist. Kodi reply. %s',
reply) reply)
if xml_video_element is not None: if xml_video_element is not None:
item = playlist_item_from_xml(playlist, xml_video_element) item = playlist_item_from_xml(xml_video_element)
item.kodi_id = kodi_id item.kodi_id = kodi_id
item.kodi_type = kodi_type item.kodi_type = kodi_type
item.file = file item.file = file
@ -646,7 +643,7 @@ def add_to_Kodi_playlist(playlist, xml_video_element):
Returns a Playlist_Item or raises PlaylistError Returns a Playlist_Item or raises PlaylistError
""" """
item = playlist_item_from_xml(playlist, xml_video_element) item = playlist_item_from_xml(xml_video_element)
if item.kodi_id: if item.kodi_id:
json_item = {'%sid' % item.kodi_type: item.kodi_id} json_item = {'%sid' % item.kodi_type: item.kodi_id}
else: else:
@ -673,7 +670,7 @@ def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file,
playlist.kodi_pl.add(url=file, listitem=listitem, index=pos) playlist.kodi_pl.add(url=file, listitem=listitem, index=pos)
# We need to add this to our internal queue as well # We need to add this to our internal queue as well
if xml_video_element is not None: if xml_video_element is not None:
item = playlist_item_from_xml(playlist, xml_video_element) item = playlist_item_from_xml(xml_video_element)
else: else:
item = playlist_item_from_kodi(kodi_item) item = playlist_item_from_kodi(kodi_item)
if file is not None: if file is not None: