From f1dc7639ab8dbeb997f0abab6793a2f0662e2083 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sun, 5 Mar 2017 17:51:58 +0100 Subject: [PATCH] Enable Alexa for Plex music --- resources/lib/PlexCompanion.py | 35 +++++++++++++++++++++++----------- resources/lib/companion.py | 3 ++- resources/lib/playlist_func.py | 5 +++++ resources/lib/playqueue.py | 29 +++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index 169db6c3..fd79d8bf 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -79,7 +79,29 @@ class PlexCompanion(Thread): log.debug('Processing: %s' % task) data = task['data'] - if (task['action'] == 'playlist' and + if task['action'] == 'alexa': + # e.g. Alexa + xml = GetPlexMetadata(data['key']) + try: + xml[0].attrib + except (AttributeError, IndexError, TypeError): + log.error('Could not download Plex metadata') + return + api = API(xml[0]) + if api.getType() == v.PLEX_TYPE_ALBUM: + log.debug('Plex music album detected') + self.mgr.playqueue.init_playqueue_from_plex_children( + api.getRatingKey()) + else: + thread = Thread(target=Plex_Node, + args=('{server}%s' % data.get('key'), + data.get('offset'), + True, + False),) + thread.setDaemon(True) + thread.start() + + elif (task['action'] == 'playlist' and data.get('address') == 'node.plexapp.com'): # E.g. watch later initiated by Companion thread = Thread(target=Plex_Node, @@ -88,6 +110,7 @@ class PlexCompanion(Thread): True),) thread.setDaemon(True) thread.start() + elif task['action'] == 'playlist': # Get the playqueue ID try: @@ -97,16 +120,6 @@ class PlexCompanion(Thread): import traceback log.error("Traceback:\n%s" % traceback.format_exc()) return - if typus == 'library/metadata': - # e.g. Alexa - thread = Thread(target=Plex_Node, - args=('{server}%s' % data.get('key'), - data.get('offset'), - True, - False),) - thread.setDaemon(True) - thread.start() - return try: playqueue = self.mgr.playqueue.get_playqueue_from_type( v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[data['type']]) diff --git a/resources/lib/companion.py b/resources/lib/companion.py index 46878cf3..2a0007c3 100644 --- a/resources/lib/companion.py +++ b/resources/lib/companion.py @@ -101,8 +101,9 @@ def process_command(request_path, params, queue=None): log.debug('Received request_path: %s, params: %s' % (request_path, params)) if "/playMedia" in request_path: # We need to tell service.py + action = 'alexa' if params.get('deviceName') == 'Alexa' else 'playlist' queue.put({ - 'action': 'playlist', + 'action': action, 'data': params }) diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index dc0268c7..5ab80b6c 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -118,6 +118,7 @@ def playlist_item_from_kodi(kodi_item): # TO BE VERIFIED - PLEX DOESN'T LIKE PLAYLIST ADDS IN THIS MANNER item.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' % (item.plex_UUID, item.plex_id)) + log.debug('Made playlist item from Kodi: %s' % item) return item @@ -136,6 +137,10 @@ def playlist_item_from_plex(plex_id): item.kodi_type = plex_dbitem[4] except: raise KeyError('Could not find plex_id %s in database' % plex_id) + item.plex_UUID = plex_id + item.uri = ('library://%s/item/library%%2Fmetadata%%2F%s' % + (item.plex_UUID, plex_id)) + log.debug('Made playlist item from plex: %s' % item) return item diff --git a/resources/lib/playqueue.py b/resources/lib/playqueue.py index 0e8939f1..c5a513a8 100644 --- a/resources/lib/playqueue.py +++ b/resources/lib/playqueue.py @@ -7,8 +7,10 @@ from xbmc import sleep, Player, PlayList, PLAYLIST_MUSIC, PLAYLIST_VIDEO from utils import window, ThreadMethods, ThreadMethodsAdditionalSuspend import playlist_func as PL -from PlexFunctions import ConvertPlexToKodiTime +from PlexFunctions import ConvertPlexToKodiTime, GetAllPlexChildren +from PlexAPI import API from playbackutils import PlaybackUtils +import variables as v ############################################################################### log = logging.getLogger("PLEX."+__name__) @@ -69,6 +71,31 @@ class Playqueue(Thread): raise ValueError('Wrong playlist type passed in: %s' % typus) return playqueue + def init_playqueue_from_plex_children(self, plex_id): + """ + Init a new playqueue e.g. from an album. Alexa does this + """ + xml = GetAllPlexChildren(plex_id) + try: + xml[0].attrib + except (TypeError, IndexError, AttributeError): + log.error('Could not download the PMS xml for %s' % plex_id) + return + playqueue = self.get_playqueue_from_type( + v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[xml[0].attrib['type']]) + playqueue.clear() + for i, child in enumerate(xml): + api = API(child) + PL.add_item_to_playlist(playqueue, i, plex_id=api.getRatingKey()) + log.debug('Firing up Kodi player') + thread = Thread(target=Player().play, + args=(playqueue.kodi_pl, + None, + False, + 0)) # starting position + thread.setDaemon(True) + thread.start() + def update_playqueue_from_PMS(self, playqueue, playqueue_id=None,