PlexKodiConnect/resources/lib/playlist_func.py

642 lines
22 KiB
Python
Raw Normal View History

2017-12-09 05:43:06 +11:00
"""
Collection of functions associated with Kodi and Plex playlists and playqueues
"""
2017-12-10 00:35:08 +11:00
from logging import getLogger
2016-12-28 03:33:52 +11:00
from urllib import quote
2017-03-26 23:09:43 +11:00
from urlparse import parse_qsl, urlsplit
from re import compile as re_compile
2016-12-28 03:33:52 +11:00
import plexdb_functions as plexdb
2016-12-28 03:33:52 +11:00
from downloadutils import DownloadUtils as DU
2017-12-09 05:43:06 +11:00
from utils import tryEncode, escape_html
2016-12-28 23:14:21 +11:00
from PlexAPI import API
from PlexFunctions import GetPlexMetadata
2017-12-09 05:43:06 +11:00
import json_rpc as js
2016-12-28 03:33:52 +11:00
###############################################################################
2017-12-10 00:35:08 +11:00
LOG = getLogger("PLEX." + __name__)
2016-12-28 03:33:52 +11:00
REGEX = re_compile(r'''metadata%2F(\d+)''')
2016-12-28 03:33:52 +11:00
###############################################################################
# kodi_item dict:
2017-01-03 00:07:24 +11:00
# {u'type': u'movie', u'id': 3, 'file': path-to-file}
2016-12-28 03:33:52 +11:00
class Playlist_Object_Baseclase(object):
2017-12-06 21:40:27 +11:00
"""
Base class
"""
playlistid = None
type = None
kodi_pl = None
items = []
old_kodi_pl = []
2017-12-09 05:43:06 +11:00
id = None
2017-12-06 21:40:27 +11:00
version = None
2016-12-28 03:33:52 +11:00
selectedItemID = None
selectedItemOffset = None
2017-12-06 21:40:27 +11:00
shuffled = 0
repeat = 0
plex_transient_token = None
2016-12-28 03:33:52 +11:00
def __repr__(self):
2017-12-06 21:40:27 +11:00
"""
Print the playlist, e.g. to log
"""
2016-12-28 23:14:21 +11:00
answ = "<%s: " % (self.__class__.__name__)
2016-12-29 00:48:23 +11:00
# For some reason, can't use dir directly
2017-12-09 05:43:06 +11:00
answ += "id: %s, " % self.id
2016-12-29 00:52:14 +11:00
answ += "items: %s, " % self.items
2016-12-28 03:33:52 +11:00
for key in self.__dict__:
2017-12-09 05:43:06 +11:00
if key not in ("id", 'items'):
if type(getattr(self, key)) in (str, unicode):
answ += '%s: %s, ' % (key, tryEncode(getattr(self, key)))
else:
# e.g. int
answ += '%s: %s, ' % (key, str(getattr(self, key)))
2016-12-28 03:33:52 +11:00
return answ[:-2] + ">"
2016-12-28 23:14:21 +11:00
def clear(self):
"""
Resets the playlist object to an empty playlist
"""
2017-12-07 04:05:01 +11:00
self.kodi_pl.clear() # Clear Kodi playlist object
2016-12-28 23:14:21 +11:00
self.items = []
self.old_kodi_pl = []
2017-12-09 05:43:06 +11:00
self.id = None
2016-12-28 23:14:21 +11:00
self.version = None
self.selectedItemID = None
self.selectedItemOffset = None
self.shuffled = 0
self.repeat = 0
self.plex_transient_token = None
2017-12-09 05:43:06 +11:00
LOG.debug('Playlist cleared: %s', self)
2016-12-28 23:14:21 +11:00
2016-12-28 03:33:52 +11:00
class Playlist_Object(Playlist_Object_Baseclase):
2017-12-06 21:40:27 +11:00
"""
To be done for synching Plex playlists to Kodi
"""
2016-12-28 03:33:52 +11:00
kind = 'playList'
class Playqueue_Object(Playlist_Object_Baseclase):
2017-12-06 21:40:27 +11:00
"""
PKC object to represent PMS playQueues and Kodi playlist for queueing
2017-12-09 05:43:06 +11:00
playlistid = None [int] Kodi playlist id (0, 1, 2)
2017-12-06 21:40:27 +11:00
type = None [str] Kodi type: 'audio', 'video', 'picture'
kodi_pl = None Kodi xbmc.PlayList object
items = [] [list] of Playlist_Items
old_kodi_pl = [] [list] store old Kodi JSON result with all pl items
2017-12-09 05:43:06 +11:00
id = None [str] Plex playQueueID, unique Plex identifier
2017-12-06 21:40:27 +11:00
version = None [int] Plex version of the playQueue
selectedItemID = None
[str] Plex selectedItemID, playing element in queue
selectedItemOffset = None
[str] Offset of the playing element in queue
shuffled = 0 [int] 0: not shuffled, 1: ??? 2: ???
repeat = 0 [int] 0: not repeated, 1: ??? 2: ???
If Companion playback is initiated by another user:
plex_transient_token = None
"""
2016-12-28 03:33:52 +11:00
kind = 'playQueue'
class Playlist_Item(object):
2017-12-06 21:40:27 +11:00
"""
Object to fill our playqueues and playlists with.
2017-12-09 05:43:06 +11:00
id = None [str] Plex playlist/playqueue id, e.g. playQueueItemID
2017-12-07 04:05:01 +11:00
plex_id = None [str] Plex unique item id, "ratingKey"
plex_type = None [str] Plex type, e.g. 'movie', 'clip'
plex_UUID = None [str] Plex librarySectionUUID
2017-12-06 21:40:27 +11:00
kodi_id = None Kodi unique kodi id (unique only within type!)
2017-12-07 04:05:01 +11:00
kodi_type = None [str] Kodi type: 'movie'
file = None [str] Path to the item's file. STRING!!
uri = None [str] Weird Plex uri path involving plex_UUID. STRING!
guid = None [str] Weird Plex guid
xml = None [etree] XML from PMS, 1 lvl below <MediaContainer>
2017-12-06 21:40:27 +11:00
"""
2017-12-09 05:43:06 +11:00
id = None
2017-12-06 21:40:27 +11:00
plex_id = None
plex_type = None
plex_UUID = None
kodi_id = None
kodi_type = None
file = None
uri = None
guid = None
xml = None
2016-12-28 23:14:21 +11:00
2017-12-08 03:25:24 +11:00
# Yet to be implemented: handling of a movie with several parts
part = 0
2016-12-28 23:14:21 +11:00
def __repr__(self):
2017-12-06 21:40:27 +11:00
"""
Print the playlist item, e.g. to log
"""
2016-12-28 23:14:21 +11:00
answ = "<%s: " % (self.__class__.__name__)
for key in self.__dict__:
if type(getattr(self, key)) in (str, unicode):
answ += '%s: %s, ' % (key, tryEncode(getattr(self, key)))
else:
# e.g. int
answ += '%s: %s, ' % (key, str(getattr(self, key)))
2016-12-28 23:14:21 +11:00
return answ[:-2] + ">"
2016-12-28 03:33:52 +11:00
2017-01-03 00:07:24 +11:00
def playlist_item_from_kodi(kodi_item):
2016-12-28 03:33:52 +11:00
"""
Turns the JSON answer from Kodi into a playlist element
Supply with data['item'] as returned from Kodi JSON-RPC interface.
kodi_item dict contains keys 'id', 'type', 'file' (if applicable)
"""
item = Playlist_Item()
2016-12-29 00:48:23 +11:00
item.kodi_id = kodi_item.get('id')
item.kodi_type = kodi_item.get('type')
2016-12-29 00:48:23 +11:00
if item.kodi_id:
with plexdb.Get_Plex_DB() as plex_db:
plex_dbitem = plex_db.getItem_byKodiId(kodi_item['id'],
2016-12-28 03:33:52 +11:00
kodi_item['type'])
try:
item.plex_id = plex_dbitem[0]
item.plex_type = plex_dbitem[2]
item.plex_UUID = plex_dbitem[0] # we dont need the uuid yet :-)
2016-12-28 03:33:52 +11:00
except TypeError:
pass
2017-01-03 00:07:24 +11:00
item.file = kodi_item.get('file')
if item.plex_id is None and item.file is not None:
query = dict(parse_qsl(urlsplit(item.file).query))
item.plex_id = query.get('plex_id')
item.plex_type = query.get('itemType')
2016-12-28 03:33:52 +11:00
if item.plex_id is None:
item.uri = 'library://whatever/item/%s' % quote(item.file, safe='')
else:
2017-01-03 00:07:24 +11:00
# TO BE VERIFIED - PLEX DOESN'T LIKE PLAYLIST ADDS IN THIS MANNER
2016-12-28 03:33:52 +11:00
item.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' %
(item.plex_UUID, item.plex_id))
2017-12-09 05:43:06 +11:00
LOG.debug('Made playlist item from Kodi: %s', item)
2016-12-28 03:33:52 +11:00
return item
def playlist_item_from_plex(plex_id):
"""
Returns a playlist element providing the plex_id ("ratingKey")
2017-01-03 00:07:24 +11:00
Returns a Playlist_Item
2016-12-28 03:33:52 +11:00
"""
item = Playlist_Item()
item.plex_id = plex_id
with plexdb.Get_Plex_DB() as plex_db:
plex_dbitem = plex_db.getItem_byId(plex_id)
2016-12-28 03:33:52 +11:00
try:
item.plex_type = plex_dbitem[5]
item.kodi_id = plex_dbitem[0]
item.kodi_type = plex_dbitem[4]
2016-12-28 03:33:52 +11:00
except:
raise KeyError('Could not find plex_id %s in database' % plex_id)
2017-03-06 03:51:58 +11:00
item.plex_UUID = plex_id
item.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' %
(item.plex_UUID, plex_id))
2017-12-09 05:43:06 +11:00
LOG.debug('Made playlist item from plex: %s', item)
2016-12-28 03:33:52 +11:00
return item
2017-01-03 00:07:24 +11:00
def playlist_item_from_xml(playlist, xml_video_element):
"""
Returns a playlist element for the playqueue using the Plex xml
2017-12-08 03:25:24 +11:00
xml_video_element: etree xml piece 1 level underneath <MediaContainer>
2017-01-03 00:07:24 +11:00
"""
item = Playlist_Item()
api = API(xml_video_element)
item.plex_id = api.getRatingKey()
item.plex_type = api.getType()
2017-12-09 05:43:06 +11:00
item.id = xml_video_element.attrib['%sItemID' % playlist.kind]
2017-01-03 00:07:24 +11:00
item.guid = xml_video_element.attrib.get('guid')
2017-05-07 02:36:24 +10:00
if item.guid is not None:
item.guid = escape_html(item.guid)
2017-01-03 00:07:24 +11:00
if item.plex_id:
with plexdb.Get_Plex_DB() as plex_db:
db_element = plex_db.getItem_byId(item.plex_id)
2017-01-03 00:07:24 +11:00
try:
item.kodi_id, item.kodi_type = int(db_element[0]), db_element[4]
except TypeError:
pass
2017-12-08 03:25:24 +11:00
item.xml = xml_video_element
2017-12-09 05:43:06 +11:00
LOG.debug('Created new playlist item from xml: %s', item)
2017-01-03 00:07:24 +11:00
return item
2016-12-28 03:33:52 +11:00
def _get_playListVersion_from_xml(playlist, xml):
"""
Takes a PMS xml as input to overwrite the playlist version (e.g. Plex
playQueueVersion). Returns True if successful, False otherwise
"""
try:
playlist.version = int(xml.attrib['%sVersion' % playlist.kind])
except (TypeError, AttributeError, KeyError):
2017-12-09 05:43:06 +11:00
LOG.error('Could not get new playlist Version for playlist %s',
playlist)
2016-12-28 03:33:52 +11:00
return False
return True
2017-01-03 00:07:24 +11:00
def get_playlist_details_from_xml(playlist, xml):
2016-12-28 03:33:52 +11:00
"""
Takes a PMS xml as input and overwrites all the playlist's details, e.g.
playlist.ID with the XML's playQueueID
"""
try:
2017-12-09 05:43:06 +11:00
playlist.id = xml.attrib['%sID' % playlist.kind]
2016-12-28 03:33:52 +11:00
playlist.version = xml.attrib['%sVersion' % playlist.kind]
playlist.shuffled = xml.attrib['%sShuffled' % playlist.kind]
2017-01-03 02:42:07 +11:00
playlist.selectedItemID = xml.attrib.get(
'%sSelectedItemID' % playlist.kind)
playlist.selectedItemOffset = xml.attrib.get(
'%sSelectedItemOffset' % playlist.kind)
2016-12-28 03:33:52 +11:00
except:
2017-12-09 05:43:06 +11:00
LOG.error('Could not parse xml answer from PMS for playlist %s',
playlist)
2016-12-28 03:33:52 +11:00
import traceback
2017-12-09 05:43:06 +11:00
LOG.error(traceback.format_exc())
2016-12-28 03:33:52 +11:00
raise KeyError
2017-12-09 05:43:06 +11:00
LOG.debug('Updated playlist from xml: %s', playlist)
2017-01-03 00:07:24 +11:00
def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
"""
Updates Kodi playlist using a new PMS playlist. Pass in playlist_id if we
need to fetch a new playqueue
If an xml is passed in, the playlist will be overwritten with its info
"""
if xml is None:
xml = get_PMS_playlist(playlist, playlist_id)
# Clear our existing playlist and the associated Kodi playlist
playlist.clear()
# Set new values
try:
get_playlist_details_from_xml(playlist, xml)
except KeyError:
2017-12-09 05:43:06 +11:00
LOG.error('Could not update playlist from PMS')
return
2017-01-03 00:07:24 +11:00
for plex_item in xml:
2017-01-22 03:20:42 +11:00
playlist_item = add_to_Kodi_playlist(playlist, plex_item)
if playlist_item is not None:
playlist.items.append(playlist_item)
2016-12-28 03:33:52 +11:00
def init_Plex_playlist(playlist, plex_id=None, kodi_item=None):
"""
2017-01-03 00:07:24 +11:00
Initializes the Plex side without changing the Kodi playlists
2017-12-08 03:25:24 +11:00
WILL ALSO UPDATE OUR PLAYLISTS.
2017-01-03 00:07:24 +11:00
2017-12-08 03:25:24 +11:00
Returns True if successful, False otherwise
2016-12-28 03:33:52 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Initializing the playlist %s on the Plex side', playlist)
try:
if plex_id:
item = playlist_item_from_plex(plex_id)
else:
item = playlist_item_from_kodi(kodi_item)
params = {
'next': 0,
'type': playlist.type,
'uri': item.uri
}
xml = DU().downloadUrl(url="{server}/%ss" % playlist.kind,
action_type="POST",
parameters=params)
get_playlist_details_from_xml(playlist, xml)
2017-12-08 03:25:24 +11:00
item.xml = xml[0]
except (KeyError, IndexError, TypeError):
2017-12-09 05:43:06 +11:00
LOG.error('Could not init Plex playlist with plex_id %s and '
2017-12-08 03:25:24 +11:00
'kodi_item %s', plex_id, kodi_item)
return False
2016-12-28 03:33:52 +11:00
playlist.items.append(item)
2017-12-09 05:43:06 +11:00
LOG.debug('Initialized the playlist on the Plex side: %s' % playlist)
2017-12-08 03:25:24 +11:00
return True
2017-01-03 00:07:24 +11:00
def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None,
kodi_type=None, plex_id=None, file=None):
"""
Adds a listitem to both the Kodi and Plex playlist at position pos [int].
If file is not None, file will overrule kodi_id!
file: str!!
2017-01-03 00:07:24 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('add_listitem_to_playlist at position %s. Playlist before add: '
'%s', pos, playlist)
2017-01-03 00:07:24 +11:00
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
2017-12-09 05:43:06 +11:00
if playlist.id is None:
2017-01-03 00:07:24 +11:00
init_Plex_playlist(playlist, plex_id, kodi_item)
else:
add_item_to_PMS_playlist(playlist, pos, plex_id, kodi_item)
if kodi_id is None and playlist.items[pos].kodi_id:
kodi_id = playlist.items[pos].kodi_id
kodi_type = playlist.items[pos].kodi_type
if file is None:
file = playlist.items[pos].file
# Otherwise we double the item!
del playlist.items[pos]
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
add_listitem_to_Kodi_playlist(playlist,
pos,
listitem,
file,
kodi_item=kodi_item)
def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
plex_id=None, file=None):
"""
Adds an item to BOTH the Kodi and Plex playlist at position pos [int]
file: str!
2017-01-03 00:07:24 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('add_item_to_playlist. Playlist before adding: %s', playlist)
2017-01-03 00:07:24 +11:00
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
2017-12-09 05:43:06 +11:00
if playlist.id is None:
2017-12-08 03:25:24 +11:00
success = init_Plex_playlist(playlist, plex_id, kodi_item)
2017-01-03 00:07:24 +11:00
else:
2017-12-08 03:25:24 +11:00
success = 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 = {
'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}
2017-12-09 05:43:06 +11:00
reply = js.playlist_insert(params)
2017-12-08 03:25:24 +11:00
if reply.get('error') is not None:
2017-12-09 05:43:06 +11:00
LOG.error('Could not add item to playlist. Kodi reply. %s', reply)
2017-12-08 03:25:24 +11:00
return False
return True
2016-12-28 03:33:52 +11:00
2017-01-03 00:07:24 +11:00
def add_item_to_PMS_playlist(playlist, pos, plex_id=None, kodi_item=None):
2016-12-28 03:33:52 +11:00
"""
2017-01-03 00:07:24 +11:00
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)
WILL ALSO UPDATE OUR PLAYLISTS
2017-12-08 03:25:24 +11:00
Returns True if successful, False otherwise
2016-12-28 03:33:52 +11:00
"""
2017-01-03 00:07:24 +11:00
if plex_id:
try:
item = playlist_item_from_plex(plex_id)
except KeyError:
2017-12-09 05:43:06 +11:00
LOG.error('Could not add new item to the PMS playlist')
2017-12-08 03:25:24 +11:00
return False
2017-01-03 00:07:24 +11:00
else:
item = playlist_item_from_kodi(kodi_item)
2017-12-09 05:43:06 +11:00
url = "{server}/%ss/%s?uri=%s" % (playlist.kind, playlist.id, item.uri)
2017-01-03 00:07:24 +11:00
# Will always put the new item at the end of the Plex playlist
2016-12-28 03:33:52 +11:00
xml = DU().downloadUrl(url, action_type="PUT")
try:
2017-12-08 03:25:24 +11:00
item.xml = xml[-1]
2017-01-03 00:07:24 +11:00
item.ID = xml[-1].attrib['%sItemID' % playlist.kind]
2017-01-03 02:42:07 +11:00
except IndexError:
2017-12-09 05:43:06 +11:00
LOG.info('Could not get playlist children. Adding a dummy')
2016-12-28 03:33:52 +11:00
except (TypeError, AttributeError, KeyError):
2017-12-09 05:43:06 +11:00
LOG.error('Could not add item %s to playlist %s', kodi_item, playlist)
2017-12-08 03:25:24 +11:00
return False
2016-12-28 23:14:21 +11:00
# Get the guid for this item
for plex_item in xml:
2017-12-09 05:43:06 +11:00
if plex_item.attrib['%sItemID' % playlist.kind] == item.id:
2017-05-07 02:36:24 +10:00
item.guid = escape_html(plex_item.attrib['guid'])
2016-12-28 03:33:52 +11:00
playlist.items.append(item)
2017-01-03 00:07:24 +11:00
if pos == len(playlist.items) - 1:
2016-12-28 03:33:52 +11:00
# Item was added at the end
_get_playListVersion_from_xml(playlist, xml)
else:
# Move the new item to the correct position
move_playlist_item(playlist,
len(playlist.items) - 1,
2017-01-03 00:07:24 +11:00
pos)
2017-12-09 05:43:06 +11:00
LOG.debug('Successfully added item on the Plex side: %s', playlist)
2017-12-08 03:25:24 +11:00
return True
2017-01-03 00:07:24 +11:00
def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
file=None):
"""
Adds an item to the KODI playlist only. WILL ALSO UPDATE OUR PLAYLISTS
2017-01-03 00:07:24 +11:00
Returns False if unsuccessful
file: str!
2017-01-03 00:07:24 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Adding new item kodi_id: %s, kodi_type: %s, file: %s to Kodi '
'only at position %s for %s',
kodi_id, kodi_type, file, pos, playlist)
2017-01-03 00:07:24 +11:00
params = {
'playlistid': playlist.playlistid,
'position': pos
}
if kodi_id is not None:
params['item'] = {'%sid' % kodi_type: int(kodi_id)}
else:
params['item'] = {'file': file}
2017-12-09 05:43:06 +11:00
reply = js.playlist_insert(params)
if reply.get('error') is not None:
2017-12-09 05:43:06 +11:00
LOG.error('Could not add item to playlist. Kodi reply. %s', reply)
return False
2017-12-08 03:25:24 +11:00
item = playlist_item_from_kodi(
2017-12-09 05:43:06 +11:00
{'id': kodi_id, 'type': kodi_type, 'file': file})
2017-12-08 03:25:24 +11:00
if item.plex_id is not None:
xml = GetPlexMetadata(item.plex_id)
try:
item.xml = xml[-1]
except (TypeError, IndexError):
2017-12-09 05:43:06 +11:00
LOG.error('Could not get metadata for playlist item %s', item)
2017-12-08 03:25:24 +11:00
playlist.items.insert(pos, item)
return True
2016-12-28 03:33:52 +11:00
def move_playlist_item(playlist, before_pos, after_pos):
"""
2017-01-03 00:07:24 +11:00
Moves playlist item from before_pos [int] to after_pos [int] for Plex only.
2017-02-03 05:03:50 +11:00
WILL ALSO CHANGE OUR PLAYLISTS. Returns True if successful
2016-12-28 03:33:52 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Moving item from %s to %s on the Plex side for %s',
before_pos, after_pos, playlist)
2016-12-28 03:33:52 +11:00
if after_pos == 0:
url = "{server}/%ss/%s/items/%s/move?after=0" % \
(playlist.kind,
2017-12-09 05:43:06 +11:00
playlist.id,
playlist.items[before_pos].id)
2016-12-28 03:33:52 +11:00
else:
url = "{server}/%ss/%s/items/%s/move?after=%s" % \
(playlist.kind,
2017-12-09 05:43:06 +11:00
playlist.id,
playlist.items[before_pos].id,
playlist.items[after_pos - 1].id)
2016-12-28 03:33:52 +11:00
# We need to increment the playlistVersion
2017-02-03 05:03:50 +11:00
if _get_playListVersion_from_xml(
playlist, DU().downloadUrl(url, action_type="PUT")) is False:
return False
2016-12-28 03:33:52 +11:00
# Move our item's position in our internal playlist
playlist.items.insert(after_pos, playlist.items.pop(before_pos))
2017-12-09 05:43:06 +11:00
LOG.debug('Done moving for %s' % playlist)
2017-02-03 05:03:50 +11:00
return True
2016-12-28 03:33:52 +11:00
2016-12-29 05:38:43 +11:00
def get_PMS_playlist(playlist, playlist_id=None):
"""
Fetches the PMS playlist/playqueue as an XML. Pass in playlist_id if we
need to fetch a new playlist
Returns None if something went wrong
"""
2017-12-09 05:43:06 +11:00
playlist_id = playlist_id if playlist_id else playlist.id
2016-12-29 05:38:43 +11:00
xml = DU().downloadUrl(
"{server}/%ss/%s" % (playlist.kind, playlist_id),
headerOptions={'Accept': 'application/xml'})
try:
xml.attrib['%sID' % playlist.kind]
except (AttributeError, KeyError):
xml = None
return xml
def refresh_playlist_from_PMS(playlist):
"""
Only updates the selected item from the PMS side (e.g.
playQueueSelectedItemID). Will NOT check whether items still make sense.
"""
xml = get_PMS_playlist(playlist)
try:
get_playlist_details_from_xml(playlist, xml)
except KeyError:
2017-12-09 05:43:06 +11:00
LOG.error('Could not refresh playlist from PMS')
2016-12-29 05:38:43 +11:00
2017-01-03 00:07:24 +11:00
def delete_playlist_item_from_PMS(playlist, pos):
2016-12-29 05:38:43 +11:00
"""
2017-01-03 00:07:24 +11:00
Delete the item at position pos [int] on the Plex side and our playlists
2016-12-28 03:33:52 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Deleting position %s for %s on the Plex side', pos, playlist)
2016-12-28 03:33:52 +11:00
xml = DU().downloadUrl("{server}/%ss/%s/items/%s?repeat=%s" %
(playlist.kind,
2017-12-09 05:43:06 +11:00
playlist.id,
playlist.items[pos].id,
2016-12-28 03:33:52 +11:00
playlist.repeat),
action_type="DELETE")
_get_playListVersion_from_xml(playlist, xml)
2017-01-03 00:07:24 +11:00
del playlist.items[pos]
2016-12-28 03:33:52 +11:00
# Functions operating on the Kodi playlist objects ##########
2016-12-28 23:14:21 +11:00
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).
Pass in the PMS xml's video element (one level underneath MediaContainer).
2017-01-22 03:20:42 +11:00
Returns a Playlist_Item or None if it did not work
2016-12-28 23:14:21 +11:00
"""
2017-01-03 00:07:24 +11:00
item = playlist_item_from_xml(playlist, xml_video_element)
2016-12-28 23:14:21 +11:00
if item.kodi_id:
2017-12-09 05:43:06 +11:00
json_item = {'%sid' % item.kodi_type: item.kodi_id}
2016-12-28 23:14:21 +11:00
else:
2017-12-09 05:43:06 +11:00
json_item = {'file': item.file}
reply = js.playlist_add(playlist.playlistid, json_item)
2017-01-22 03:20:42 +11:00
if reply.get('error') is not None:
2017-12-09 05:43:06 +11:00
LOG.error('Could not add item %s to Kodi playlist. Error: %s',
xml_video_element, reply)
2017-01-22 03:20:42 +11:00
return None
2017-12-08 03:25:24 +11:00
return item
2016-12-28 23:14:21 +11:00
2017-01-03 00:07:24 +11:00
def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file,
xml_video_element=None, kodi_item=None):
2016-12-29 05:38:43 +11:00
"""
2017-01-03 00:07:24 +11:00
Adds an xbmc listitem to the Kodi playlist.xml_video_element
2016-12-29 05:38:43 +11:00
2017-01-03 00:07:24 +11:00
WILL NOT UPDATE THE PLEX SIDE, BUT WILL UPDATE OUR PLAYLISTS
file: string!
2017-01-03 00:07:24 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Insert listitem at position %s for Kodi only for %s',
pos, playlist)
2017-01-03 00:07:24 +11:00
# Add the item into Kodi playlist
playlist.kodi_pl.add(file, listitem, index=pos)
# We need to add this to our internal queue as well
if xml_video_element is not None:
item = playlist_item_from_xml(playlist, xml_video_element)
2016-12-28 03:33:52 +11:00
else:
2017-01-03 00:07:24 +11:00
item = playlist_item_from_kodi(kodi_item)
2017-05-08 00:58:12 +10:00
if file is not None:
item.file = file
2017-01-03 00:07:24 +11:00
playlist.items.insert(pos, item)
2017-12-09 05:43:06 +11:00
LOG.debug('Done inserting for %s', playlist)
2016-12-28 03:33:52 +11:00
2017-12-09 05:43:06 +11:00
def remove_from_kodi_playlist(playlist, pos):
2016-12-28 03:33:52 +11:00
"""
2017-01-03 00:07:24 +11:00
Removes the item at position pos from the Kodi playlist using JSON.
WILL NOT UPDATE THE PLEX SIDE, BUT WILL UPDATE OUR PLAYLISTS
2016-12-28 03:33:52 +11:00
"""
2017-12-09 05:43:06 +11:00
LOG.debug('Removing position %s from Kodi only from %s', pos, playlist)
reply = js.playlist_remove(playlist.playlistid, pos)
2017-01-22 03:20:42 +11:00
if reply.get('error') is not None:
2017-12-09 05:43:06 +11:00
LOG.error('Could not delete the item from the playlist. Error: %s',
reply)
2017-01-22 03:20:42 +11:00
return
2017-12-09 05:43:06 +11:00
try:
del playlist.items[pos]
except IndexError:
LOG.error('Cannot delete position %s for %s', pos, playlist)
def get_pms_playqueue(playqueue_id):
"""
Returns the Plex playqueue as an etree XML or None if unsuccessful
"""
xml = DU().downloadUrl(
"{server}/playQueues/%s" % playqueue_id,
headerOptions={'Accept': 'application/xml'})
try:
xml.attrib
except AttributeError:
2017-12-09 05:43:06 +11:00
LOG.error('Could not download Plex playqueue %s', playqueue_id)
xml = None
return xml
def get_plextype_from_xml(xml):
"""
Needed if PMS returns an empty playqueue. Will get the Plex type from the
empty playlist playQueueSourceURI. Feed with (empty) etree xml
returns None if unsuccessful
"""
try:
plex_id = REGEX.findall(xml.attrib['playQueueSourceURI'])[0]
except IndexError:
2017-12-09 05:43:06 +11:00
LOG.error('Could not get plex_id from xml: %s', xml.attrib)
return
new_xml = GetPlexMetadata(plex_id)
try:
new_xml[0].attrib
except (TypeError, IndexError, AttributeError):
2017-12-09 05:43:06 +11:00
LOG.error('Could not get plex metadata for plex id %s', plex_id)
return
return new_xml[0].attrib.get('type')