Introduce PlaylistError exception
This commit is contained in:
parent
a6e9869a14
commit
fc9ea2444e
1 changed files with 53 additions and 62 deletions
|
@ -21,8 +21,12 @@ LOG = getLogger("PLEX." + __name__)
|
||||||
REGEX = re_compile(r'''metadata%2F(\d+)''')
|
REGEX = re_compile(r'''metadata%2F(\d+)''')
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
# kodi_item dict:
|
|
||||||
# {u'type': u'movie', u'id': 3, 'file': path-to-file}
|
class PlaylistError(Exception):
|
||||||
|
"""
|
||||||
|
Exception for our playlist constructs
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PlaylistObjectBaseclase(object):
|
class PlaylistObjectBaseclase(object):
|
||||||
|
@ -361,30 +365,36 @@ def playlist_item_from_xml(playlist, xml_video_element, kodi_id=None,
|
||||||
def _get_playListVersion_from_xml(playlist, xml):
|
def _get_playListVersion_from_xml(playlist, xml):
|
||||||
"""
|
"""
|
||||||
Takes a PMS xml as input to overwrite the playlist version (e.g. Plex
|
Takes a PMS xml as input to overwrite the playlist version (e.g. Plex
|
||||||
playQueueVersion). Returns True if successful, False otherwise
|
playQueueVersion).
|
||||||
|
|
||||||
|
Raises PlaylistError if unsuccessful
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
playlist.version = int(xml.attrib['%sVersion' % playlist.kind])
|
playlist.version = int(xml.attrib['%sVersion' % playlist.kind])
|
||||||
except (TypeError, AttributeError, KeyError):
|
except (TypeError, AttributeError, KeyError):
|
||||||
LOG.error('Could not get new playlist Version for playlist %s',
|
raise PlaylistError('Could not get new playlist Version for playlist '
|
||||||
playlist)
|
'%s', playlist)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def get_playlist_details_from_xml(playlist, xml):
|
def get_playlist_details_from_xml(playlist, xml):
|
||||||
"""
|
"""
|
||||||
Takes a PMS xml as input and overwrites all the playlist's details, e.g.
|
Takes a PMS xml as input and overwrites all the playlist's details, e.g.
|
||||||
playlist.id with the XML's playQueueID
|
playlist.id with the XML's playQueueID
|
||||||
|
|
||||||
|
Raises PlaylistError if something went wrong.
|
||||||
"""
|
"""
|
||||||
playlist.id = xml.attrib['%sID' % playlist.kind]
|
try:
|
||||||
playlist.version = xml.attrib['%sVersion' % playlist.kind]
|
playlist.id = xml.attrib['%sID' % playlist.kind]
|
||||||
playlist.shuffled = xml.attrib['%sShuffled' % playlist.kind]
|
playlist.version = xml.attrib['%sVersion' % playlist.kind]
|
||||||
playlist.selectedItemID = xml.attrib.get(
|
playlist.shuffled = xml.attrib['%sShuffled' % playlist.kind]
|
||||||
'%sSelectedItemID' % playlist.kind)
|
playlist.selectedItemID = xml.attrib.get(
|
||||||
playlist.selectedItemOffset = xml.attrib.get(
|
'%sSelectedItemID' % playlist.kind)
|
||||||
'%sSelectedItemOffset' % playlist.kind)
|
playlist.selectedItemOffset = xml.attrib.get(
|
||||||
LOG.debug('Updated playlist from xml: %s', playlist)
|
'%sSelectedItemOffset' % playlist.kind)
|
||||||
|
LOG.debug('Updated playlist from xml: %s', playlist)
|
||||||
|
except (TypeError, KeyError, AttributeError) as msg:
|
||||||
|
raise PlaylistError('Could not get playlist details from xml: %s',
|
||||||
|
msg)
|
||||||
|
|
||||||
|
|
||||||
def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
|
def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
|
||||||
|
@ -399,11 +409,7 @@ def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
|
||||||
# Clear our existing playlist and the associated Kodi playlist
|
# Clear our existing playlist and the associated Kodi playlist
|
||||||
playlist.clear()
|
playlist.clear()
|
||||||
# Set new values
|
# Set new values
|
||||||
try:
|
get_playlist_details_from_xml(playlist, xml)
|
||||||
get_playlist_details_from_xml(playlist, xml)
|
|
||||||
except KeyError:
|
|
||||||
LOG.error('Could not update playlist from PMS')
|
|
||||||
return
|
|
||||||
for plex_item in xml:
|
for plex_item in xml:
|
||||||
playlist_item = add_to_Kodi_playlist(playlist, plex_item)
|
playlist_item = add_to_Kodi_playlist(playlist, plex_item)
|
||||||
if playlist_item is not None:
|
if playlist_item is not None:
|
||||||
|
@ -415,7 +421,7 @@ 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
|
Returns the first PKC playlist item or raises PlaylistError
|
||||||
"""
|
"""
|
||||||
LOG.debug('Initializing the playlist %s on the Plex side', playlist)
|
LOG.debug('Initializing the playlist %s on the Plex side', playlist)
|
||||||
playlist.clear()
|
playlist.clear()
|
||||||
|
@ -436,12 +442,11 @@ def init_Plex_playlist(playlist, plex_id=None, kodi_item=None):
|
||||||
# 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(playlist, xml[0])
|
||||||
except (KeyError, IndexError, TypeError):
|
except (KeyError, IndexError, TypeError):
|
||||||
LOG.error('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)
|
||||||
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
|
return item
|
||||||
|
|
||||||
|
|
||||||
def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None,
|
def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None,
|
||||||
|
@ -479,19 +484,16 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
||||||
plex_id=None, file=None):
|
plex_id=None, file=None):
|
||||||
"""
|
"""
|
||||||
Adds an item to BOTH the Kodi and Plex playlist at position pos [int]
|
Adds an item to BOTH the Kodi and Plex playlist at position pos [int]
|
||||||
|
file: str!
|
||||||
|
|
||||||
file: str!
|
Raises PlaylistError if something went wrong
|
||||||
"""
|
"""
|
||||||
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:
|
||||||
success = init_Plex_playlist(playlist, plex_id, kodi_item)
|
item = init_Plex_playlist(playlist, plex_id, kodi_item)
|
||||||
else:
|
else:
|
||||||
success = add_item_to_PMS_playlist(playlist, pos, plex_id, kodi_item)
|
item = add_item_to_PMS_playlist(playlist, pos, plex_id, kodi_item)
|
||||||
if success is False:
|
|
||||||
return False
|
|
||||||
# Now add the item to the Kodi playlist - WITHOUT adding it to our PKC pl
|
|
||||||
item = playlist.items[pos]
|
|
||||||
params = {
|
params = {
|
||||||
'playlistid': playlist.playlistid,
|
'playlistid': playlist.playlistid,
|
||||||
'position': pos
|
'position': pos
|
||||||
|
@ -503,10 +505,10 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
||||||
playlist.kodi_onadd()
|
playlist.kodi_onadd()
|
||||||
reply = js.playlist_insert(params)
|
reply = js.playlist_insert(params)
|
||||||
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)
|
|
||||||
playlist.is_kodi_onadd()
|
playlist.is_kodi_onadd()
|
||||||
return False
|
raise PlaylistError('Could not add item to playlist. Kodi reply. %s',
|
||||||
return True
|
reply)
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
||||||
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):
|
||||||
|
@ -515,14 +517,10 @@ def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
|
||||||
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
|
Returns the PKC PlayList item or raises PlaylistError
|
||||||
"""
|
"""
|
||||||
if plex_id:
|
if plex_id:
|
||||||
try:
|
item = playlist_item_from_plex(plex_id)
|
||||||
item = playlist_item_from_plex(plex_id)
|
|
||||||
except KeyError:
|
|
||||||
LOG.error('Could not add new item to the PMS playlist')
|
|
||||||
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)
|
||||||
|
@ -534,8 +532,8 @@ def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
|
||||||
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', kodi_item, playlist)
|
raise PlaylistError('Could not add item %s to playlist %s',
|
||||||
return False
|
kodi_item, playlist)
|
||||||
# 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:
|
||||||
|
@ -550,7 +548,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
|
return item
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
|
@ -558,7 +556,7 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
||||||
"""
|
"""
|
||||||
Adds an item to the KODI playlist only. WILL ALSO UPDATE OUR PLAYLISTS
|
Adds an item to the KODI playlist only. WILL ALSO UPDATE OUR PLAYLISTS
|
||||||
|
|
||||||
Returns the playlist item that was just added or None
|
Returns the playlist item that was just added or raises PlaylistError
|
||||||
|
|
||||||
file: str!
|
file: str!
|
||||||
"""
|
"""
|
||||||
|
@ -576,9 +574,9 @@ def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
||||||
playlist.kodi_onadd()
|
playlist.kodi_onadd()
|
||||||
reply = js.playlist_insert(params)
|
reply = js.playlist_insert(params)
|
||||||
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)
|
|
||||||
playlist.is_kodi_onadd()
|
playlist.is_kodi_onadd()
|
||||||
return
|
raise PlaylistError('Could not add item to playlist. Kodi reply. %s',
|
||||||
|
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(playlist, xml_video_element)
|
||||||
item.kodi_id = kodi_id
|
item.kodi_id = kodi_id
|
||||||
|
@ -598,7 +596,7 @@ def move_playlist_item(playlist, before_pos, after_pos):
|
||||||
"""
|
"""
|
||||||
Moves playlist item from before_pos [int] to after_pos [int] for Plex only.
|
Moves playlist item from before_pos [int] to after_pos [int] for Plex only.
|
||||||
|
|
||||||
WILL ALSO CHANGE OUR PLAYLISTS. Returns True if successful
|
WILL ALSO CHANGE OUR PLAYLISTS.
|
||||||
"""
|
"""
|
||||||
LOG.debug('Moving item from %s to %s on the Plex side for %s',
|
LOG.debug('Moving item from %s to %s on the Plex side for %s',
|
||||||
before_pos, after_pos, playlist)
|
before_pos, after_pos, playlist)
|
||||||
|
@ -614,13 +612,11 @@ def move_playlist_item(playlist, before_pos, after_pos):
|
||||||
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
|
# We need to increment the playlistVersion
|
||||||
if _get_playListVersion_from_xml(
|
_get_playListVersion_from_xml(
|
||||||
playlist, DU().downloadUrl(url, action_type="PUT")) is False:
|
playlist, DU().downloadUrl(url, action_type="PUT"))
|
||||||
return False
|
|
||||||
# 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)
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def get_PMS_playlist(playlist, playlist_id=None):
|
def get_PMS_playlist(playlist, playlist_id=None):
|
||||||
|
@ -646,11 +642,7 @@ def refresh_playlist_from_PMS(playlist):
|
||||||
Only updates the selected item from the PMS side (e.g.
|
Only updates the selected item from the PMS side (e.g.
|
||||||
playQueueSelectedItemID). Will NOT check whether items still make sense.
|
playQueueSelectedItemID). Will NOT check whether items still make sense.
|
||||||
"""
|
"""
|
||||||
xml = get_PMS_playlist(playlist)
|
get_playlist_details_from_xml(playlist, get_PMS_playlist(playlist))
|
||||||
try:
|
|
||||||
get_playlist_details_from_xml(playlist, xml)
|
|
||||||
except KeyError:
|
|
||||||
LOG.error('Could not refresh playlist from PMS')
|
|
||||||
|
|
||||||
|
|
||||||
def delete_playlist_item_from_PMS(playlist, pos):
|
def delete_playlist_item_from_PMS(playlist, pos):
|
||||||
|
@ -675,7 +667,7 @@ def add_to_Kodi_playlist(playlist, xml_video_element):
|
||||||
Adds a new item to the Kodi playlist via JSON (at the end of the playlist).
|
Adds a new item to the Kodi playlist via JSON (at the end of the playlist).
|
||||||
Pass in the PMS xml's video element (one level underneath MediaContainer).
|
Pass in the PMS xml's video element (one level underneath MediaContainer).
|
||||||
|
|
||||||
Returns a Playlist_Item or None if it did not work
|
Returns a Playlist_Item or raises PlaylistError
|
||||||
"""
|
"""
|
||||||
item = playlist_item_from_xml(playlist, xml_video_element)
|
item = playlist_item_from_xml(playlist, xml_video_element)
|
||||||
if item.kodi_id:
|
if item.kodi_id:
|
||||||
|
@ -684,9 +676,8 @@ def add_to_Kodi_playlist(playlist, xml_video_element):
|
||||||
json_item = {'file': item.file}
|
json_item = {'file': item.file}
|
||||||
reply = js.playlist_add(playlist.playlistid, json_item)
|
reply = js.playlist_add(playlist.playlistid, json_item)
|
||||||
if reply.get('error') is not None:
|
if reply.get('error') is not None:
|
||||||
LOG.error('Could not add item %s to Kodi playlist. Error: %s',
|
raise PlaylistError('Could not add item %s to Kodi playlist. Error: '
|
||||||
xml_video_element, reply)
|
'%s', xml_video_element, reply)
|
||||||
return None
|
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue