This commit is contained in:
croneter 2019-05-26 11:53:59 +02:00
parent d3752e1958
commit fb21bc7d71
2 changed files with 47 additions and 39 deletions

View file

@ -12,7 +12,7 @@ PLAYQUEUES = []
class PlayqueueError(Exception): class PlayqueueError(Exception):
""" """
Exception for our playlist constructs Exception for our playqueue constructs
""" """
pass pass
@ -60,7 +60,7 @@ class PlaylistItem(object):
self.kodi_type = kodi_type self.kodi_type = kodi_type
self.file = None self.file = None
if kodi_item: if kodi_item:
self.kodi_id = kodi_item.get('id') self.kodi_id = utils.cast(int, kodi_item.get('id'))
self.kodi_type = kodi_item.get('type') self.kodi_type = kodi_item.get('type')
self.file = kodi_item.get('file') self.file = kodi_item.get('file')
self.uri = None self.uri = None
@ -76,15 +76,9 @@ class PlaylistItem(object):
# False: do NOT resume, don't ask user # False: do NOT resume, don't ask user
# True: do resume, don't ask user # True: do resume, don't ask user
self.resume = None self.resume = None
if (self.plex_id is None and if self.plex_id is None:
(self.kodi_id is not None and self.kodi_type is not None)): self._from_plex_db()
with PlexDB(lock=False) as plexdb: if grab_xml and self.plex_id is not None and xml_video_element is None:
db_item = plexdb.item_by_kodi_id(self.kodi_id, self.kodi_type)
if db_item:
self.plex_id = db_item['plex_id']
self.plex_type = db_item['plex_type']
self.plex_uuid = db_item['section_uuid']
if grab_xml and plex_id is not None and xml_video_element is None:
xml_video_element = PF.GetPlexMetadata(plex_id) xml_video_element = PF.GetPlexMetadata(plex_id)
try: try:
xml_video_element = xml_video_element[0] xml_video_element = xml_video_element[0]
@ -99,11 +93,13 @@ class PlaylistItem(object):
if db_item is not None: if db_item is not None:
self.kodi_id = db_item['kodi_id'] self.kodi_id = db_item['kodi_id']
self.kodi_type = db_item['kodi_type'] self.kodi_type = db_item['kodi_type']
self.plex_type = db_item['plex_type']
self.plex_uuid = db_item['section_uuid'] self.plex_uuid = db_item['section_uuid']
if (lookup_kodi and (self.kodi_id is None or self.kodi_type is None) and if (lookup_kodi and (self.kodi_id is None or self.kodi_type is None) and
self.plex_type != v.PLEX_TYPE_CLIP): self.plex_type != v.PLEX_TYPE_CLIP):
self._guess_id_from_file() self._guess_id_from_file()
self.set_uri() self._from_plex_db()
self._set_uri()
def __eq__(self, other): def __eq__(self, other):
if self.plex_id is not None and other.plex_id is not None: if self.plex_id is not None and other.plex_id is not None:
@ -142,6 +138,20 @@ class PlaylistItem(object):
return unicode(self).encode('utf-8') return unicode(self).encode('utf-8')
__repr__ = __str__ __repr__ = __str__
def _from_plex_db(self):
"""
Uses self.kodi_id and self.kodi_type to look up the item in the Plex
DB. Thus potentially sets self.plex_id, plex_type, plex_uuid
"""
if self.kodi_id is None or not self.kodi_type:
return
with PlexDB(lock=False) as plexdb:
db_item = plexdb.item_by_kodi_id(self.kodi_id, self.kodi_type)
if db_item:
self.plex_id = db_item['plex_id']
self.plex_type = db_item['plex_type']
self.plex_uuid = db_item['section_uuid']
def from_xml(self, xml_video_element): def from_xml(self, xml_video_element):
""" """
xml_video_element: etree xml piece 1 level underneath <MediaContainer> xml_video_element: etree xml piece 1 level underneath <MediaContainer>
@ -157,7 +167,9 @@ class PlaylistItem(object):
self.playcount = api.viewcount() self.playcount = api.viewcount()
self.offset = api.resume_point() self.offset = api.resume_point()
self.xml = xml_video_element self.xml = xml_video_element
self.set_uri() if self.kodi_id is None or not self.kodi_type:
self._from_plex_db()
self._set_uri()
def from_kodi(self, playlist_item): def from_kodi(self, playlist_item):
""" """
@ -167,17 +179,12 @@ class PlaylistItem(object):
If kodi_id & kodi_type are provided, plex_id and plex_type will be If kodi_id & kodi_type are provided, plex_id and plex_type will be
looked up (if not already set) looked up (if not already set)
""" """
self.kodi_id = playlist_item.get('id') self.kodi_id = utils.cast(int, playlist_item.get('id'))
self.kodi_type = playlist_item.get('type') self.kodi_type = playlist_item.get('type')
self.file = playlist_item.get('file') self.file = playlist_item.get('file')
if self.plex_id is None and self.kodi_id is not None and self.kodi_type: if self.plex_id is None and self.kodi_id is not None and self.kodi_type:
with PlexDB(lock=False) as plexdb: self._from_plex_db()
db_item = plexdb.item_by_kodi_id(self.kodi_id, self.kodi_type) if self.plex_id is None and self.file:
if db_item:
self.plex_id = db_item['plex_id']
self.plex_type = db_item['plex_type']
self.plex_uuid = db_item['section_uuid']
if self.plex_id is None and self.file is not None:
try: try:
query = self.file.split('?', 1)[1] query = self.file.split('?', 1)[1]
except IndexError: except IndexError:
@ -185,14 +192,13 @@ class PlaylistItem(object):
query = dict(utils.parse_qsl(query)) query = dict(utils.parse_qsl(query))
self.plex_id = utils.cast(int, query.get('plex_id')) self.plex_id = utils.cast(int, query.get('plex_id'))
self.plex_type = query.get('itemType') self.plex_type = query.get('itemType')
self.set_uri() self._set_uri()
def set_uri(self): def _set_uri(self):
if self.plex_id is None and self.file is not None: if self.plex_id is None and self.file is not None:
self.uri = ('library://whatever/item/%s' self.uri = ('library://whatever/item/%s'
% utils.quote(self.file, safe='')) % utils.quote(self.file, safe=''))
elif self.plex_id is not None and self.plex_uuid is not None: elif self.plex_id is not None and self.plex_uuid is not None:
# TO BE VERIFIED - PLEX DOESN'T LIKE PLAYLIST ADDS IN THIS MANNER
self.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' % self.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' %
(self.plex_uuid, self.plex_id)) (self.plex_uuid, self.plex_id))
elif self.plex_id is not None: elif self.plex_id is not None:
@ -203,6 +209,8 @@ class PlaylistItem(object):
def _guess_id_from_file(self): def _guess_id_from_file(self):
""" """
If self.file is set, will try to guess kodi_id and kodi_type from the
filename and path using the Kodi video and music databases
""" """
if not self.file: if not self.file:
return return
@ -219,12 +227,12 @@ class PlaylistItem(object):
self.file.startswith('http://127.0.0.1:%s' % v.WEBSERVICE_PORT))): self.file.startswith('http://127.0.0.1:%s' % v.WEBSERVICE_PORT))):
return return
# Try the VIDEO DB first - will find both movies and episodes # Try the VIDEO DB first - will find both movies and episodes
self.kodi_id, self.kodi_type = kodi_db.kodiid_from_filename(self.file, self.kodi_id, self.kodi_type = kodi_db.kodiid_from_filename(
db_type='video') self.file, db_type='video')
if self.kodi_id is None: if self.kodi_id is None:
# No movie or episode found - try MUSIC DB now for songs # No movie or episode found - try MUSIC DB now for songs
self.kodi_id, self.kodi_type = kodi_db.kodiid_from_filename(self.file, self.kodi_id, self.kodi_type = kodi_db.kodiid_from_filename(
db_type='music') self.file, db_type='music')
self.kodi_type = None if self.kodi_id is None else self.kodi_type self.kodi_type = None if self.kodi_id is None else self.kodi_type
def plex_stream_index(self, kodi_stream_index, stream_type): def plex_stream_index(self, kodi_stream_index, stream_type):
@ -289,6 +297,6 @@ class PlaylistItemDummy(PlaylistItem):
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(PlaylistItemDummy, self).__init__(*args, **kwargs) super(PlaylistItemDummy, self).__init__(*args, **kwargs)
self.name = 'dummy item' self.name = 'PKC Dummy playqueue item'
self.id = 0 self.id = 0
self.plex_id = 0 self.plex_id = 0

View file

@ -201,7 +201,7 @@ class PlayQueue(object):
xml = PF.GetPlexMetadata(plex_id) xml = PF.GetPlexMetadata(plex_id)
if xml in (None, 401): if xml in (None, 401):
raise PlayqueueError('Could not get Plex metadata %s for %s', raise PlayqueueError('Could not get Plex metadata %s for %s',
plex_id, self.items[startpos]) plex_id, self.items[startpos])
api = API(xml[0]) api = API(xml[0])
if playlistitem.resume is None: if playlistitem.resume is None:
# Potentially ask user to resume # Potentially ask user to resume
@ -383,10 +383,10 @@ class PlayQueue(object):
""" """
if not isinstance(item, PlaylistItem): if not isinstance(item, PlaylistItem):
raise PlayqueueError('Wrong item %s of type %s received' raise PlayqueueError('Wrong item %s of type %s received'
% (item, type(item))) % (item, type(item)))
if pos > len(self.items): if pos > len(self.items):
raise PlayqueueError('Position %s too large for playlist length %s' raise PlayqueueError('Position %s too large for playlist length %s'
% (pos, len(self.items))) % (pos, len(self.items)))
LOG.debug('Adding item to Kodi playlist at position %s: %s', pos, item) LOG.debug('Adding item to Kodi playlist at position %s: %s', pos, item)
if listitem: if listitem:
self.kodi_pl.add(url=listitem.getPath(), self.kodi_pl.add(url=listitem.getPath(),
@ -404,7 +404,7 @@ class PlayQueue(object):
'item': {'%sid' % item.kodi_type: item.kodi_id}}) 'item': {'%sid' % item.kodi_type: item.kodi_id}})
if 'error' in answ: if 'error' in answ:
raise PlayqueueError('Kodi did not add item to playlist: %s', raise PlayqueueError('Kodi did not add item to playlist: %s',
answ) answ)
else: else:
if item.xml is None: if item.xml is None:
LOG.debug('Need to get metadata for item %s', item) LOG.debug('Need to get metadata for item %s', item)
@ -435,10 +435,10 @@ class PlayQueue(object):
""" """
if not isinstance(item, PlaylistItem) or not item.uri: if not isinstance(item, PlaylistItem) or not item.uri:
raise PlayqueueError('Wrong item %s of type %s received' raise PlayqueueError('Wrong item %s of type %s received'
% (item, type(item))) % (item, type(item)))
if pos > len(self.items): if pos > len(self.items):
raise PlayqueueError('Position %s too large for playlist length %s' raise PlayqueueError('Position %s too large for playlist length %s'
% (pos, len(self.items))) % (pos, len(self.items)))
LOG.debug('Adding item to Plex playlist at position %s: %s', pos, item) LOG.debug('Adding item to Plex playlist at position %s: %s', pos, item)
url = '{server}/%ss/%s?uri=%s' % (self.kind, self.id, item.uri) url = '{server}/%ss/%s?uri=%s' % (self.kind, self.id, item.uri)
# Will usually put the new item at the end of the Plex playlist # Will usually put the new item at the end of the Plex playlist
@ -447,7 +447,7 @@ class PlayQueue(object):
xml[0].attrib xml[0].attrib
except (TypeError, AttributeError, KeyError, IndexError): except (TypeError, AttributeError, KeyError, IndexError):
raise PlayqueueError('Could not add item %s to playlist %s' raise PlayqueueError('Could not add item %s to playlist %s'
% (item, self)) % (item, self))
for actual_pos, xml_video_element in enumerate(xml): for actual_pos, xml_video_element in enumerate(xml):
api = API(xml_video_element) api = API(xml_video_element)
if api.plex_id() == item.plex_id: if api.plex_id() == item.plex_id:
@ -497,8 +497,8 @@ class PlayQueue(object):
""" """
if before > len(self.items) or after > len(self.items) or after == before: if before > len(self.items) or after > len(self.items) or after == before:
raise PlayqueueError('Illegal original position %s and/or desired ' raise PlayqueueError('Illegal original position %s and/or desired '
'position %s for playlist length %s' % 'position %s for playlist length %s' %
(before, after, len(self.items))) (before, after, len(self.items)))
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, after, self) before, after, self)
if after == 0: if after == 0:
@ -523,7 +523,7 @@ class PlayQueue(object):
xml[0].attrib xml[0].attrib
except (TypeError, IndexError, AttributeError): except (TypeError, IndexError, AttributeError):
raise PlayqueueError('Could not move playlist item from %s to %s ' raise PlayqueueError('Could not move playlist item from %s to %s '
'for %s' % (before, after, self)) 'for %s' % (before, after, self))
self.update_details_from_xml(xml) self.update_details_from_xml(xml)
self.items.insert(after, self.items.pop(before)) self.items.insert(after, self.items.pop(before))
LOG.debug('Done moving items for %s', self) LOG.debug('Done moving items for %s', self)