Enable Alexa for Plex music

This commit is contained in:
tomkat83 2017-03-05 17:51:58 +01:00
parent 3d525f7772
commit f1dc7639ab
4 changed files with 59 additions and 13 deletions

View file

@ -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']])

View file

@ -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
})

View file

@ -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

View file

@ -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,