From 57e6d40af220aa614bc30420ff22898b622dc175 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Tue, 12 Jan 2016 11:47:48 +0100 Subject: [PATCH] Plex trailers directly playable now --- resources/lib/PlexAPI.py | 46 +++++++++++++++++++++++++++++++++++- resources/lib/entrypoint.py | 9 +++++-- resources/lib/itemtypes.py | 12 ++++++++-- resources/lib/librarysync.py | 16 ++++++------- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index 90561230..b4f841d7 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -1914,6 +1914,45 @@ class API(): } return videoCodec + def getExtras(self): + """ + Returns a list of trailer and extras from PMS XML. Returns None if + no extras are found. + Extratypes: + '1': Trailer + '5': Behind the scenes + + Output: list of dicts with one entry of the form: + 'key': e.g. /library/metadata/xxxx + 'title': + 'thumb': artwork + 'duration': + 'extraType': + 'originallyAvailableAt': + 'year': + """ + extras = self.item[0].find('Extras') + if not extras: + return None + elements = [] + for extra in extras: + # Trailer: + key = extra.attrib['key'] + title = extra.attrib['title'] + thumb = extra.attrib['thumb'] + duration = extra.attrib['duration'] + year = extra.attrib['year'] + extraType = extra.attrib['extraType'] + originallyAvailableAt = extra.attrib['originallyAvailableAt'] + elements.append({'key': key, + 'title': title, + 'thumb': thumb, + 'duration': duration, + 'extraType': extraType, + 'originallyAvailableAt': originallyAvailableAt, + 'year': year}) + return elements + def getMediaStreams(self): """ Returns the media streams @@ -2291,7 +2330,12 @@ class API(): """ item = self.item key = self.getKey() - uuid = item.attrib['librarySectionUUID'] + try: + uuid = item.attrib['librarySectionUUID'] + # if not found: probably trying to start a trailer directly + # Hence no playlist needed + except KeyError: + return None mediatype = item[self.child].tag.lower() trailerNumber = utils.settings('trailerNumber') if not trailerNumber: diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index 8a7e138f..aff64718 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -34,8 +34,13 @@ def doPlayback(itemid, dbid): # Get a first XML to get the librarySectionUUID item = PlexAPI.PlexAPI().GetPlexMetadata(itemid) # Use that to call the playlist - item = PlexAPI.API(item).GetPlexPlaylist() - pbutils.PlaybackUtils(item).play(itemid, dbid) + playlist = PlexAPI.API(item).GetPlexPlaylist() + if playlist: + pbutils.PlaybackUtils(playlist).play(itemid, dbid) + + else: + # No playlist received e.g. when directly playing trailers + pbutils.PlaybackUtils(item).play(itemid, dbid) ##### DO RESET AUTH ##### def resetAuth(): diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 0107bdbe..2195e8c2 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -349,9 +349,17 @@ class Movies(Items): studio = None self.logMsg("Retrieved metadata for %s" % itemid, 2) - # TODO: trailers + # Find one trailer trailer = None - self.logMsg("Retrieved trailer for %s" % itemid, 2) + extras = API.getExtras() + if extras: + for item in extras: + # Only get 1st trailer element + if item['extraType'] == '1': + trailer = item['key'] + trailer = "plugin://plugin.video.plexkodiconnect/trailer/?id=%s&mode=play" % trailer + self.logMsg("Trailer for %s: %s" % (itemid, trailer), 2) + break ##### GET THE FILE AND PATH ##### playurl = API.getFilePath() diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index db3f40ef..fe337fde 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -121,14 +121,14 @@ class ThreadedProcessMetadata(threading.Thread): title = updateItem['title'] itemSubFkt = getattr(item, method) with self.lock: - itemSubFkt( - plexitem, - viewtag=viewName, - viewid=viewId - ) - # Keep track of where we are at - processMetadataCount += 1 - processingViewName = title + itemSubFkt( + plexitem, + viewtag=viewName, + viewid=viewId + ) + # Keep track of where we are at + processMetadataCount += 1 + processingViewName = title # signals to queue job is done self.queue.task_done()