PlexKodiConnect/resources/lib/companion.py

117 lines
3.9 KiB
Python
Raw Normal View History

2017-12-08 17:53:01 +11:00
"""
Processes Plex companion inputs from the plexbmchelper to Kodi commands
"""
from logging import getLogger
2017-03-05 03:54:24 +11:00
2017-03-06 04:28:30 +11:00
from xbmc import Player
2017-03-05 03:54:24 +11:00
from variables import ALEXA_TO_COMPANION
2017-03-06 04:28:30 +11:00
from playqueue import Playqueue
from PlexFunctions import GetPlexKeyNumber
2017-12-08 17:53:01 +11:00
import json_rpc as js
2017-03-05 03:54:24 +11:00
###############################################################################
2017-12-08 17:53:01 +11:00
LOG = getLogger("PLEX." + __name__)
2017-03-05 03:54:24 +11:00
###############################################################################
2017-12-08 17:53:01 +11:00
def skip_to(params):
2017-03-05 03:54:24 +11:00
"""
2017-12-08 17:53:01 +11:00
Skip to a specific playlist position.
2017-03-05 03:54:24 +11:00
2017-12-08 17:53:01 +11:00
Does not seem to be implemented yet by Plex!
2017-03-05 03:54:24 +11:00
"""
2017-12-08 17:53:01 +11:00
playqueue_item_id = params.get('playQueueItemID', 'not available')
_, plex_id = GetPlexKeyNumber(params.get('key'))
LOG.debug('Skipping to playQueueItemID %s, plex_id %s',
playqueue_item_id, plex_id)
2017-03-06 04:28:30 +11:00
found = True
playqueues = Playqueue()
2017-12-08 17:53:01 +11:00
for (player, _) in js.get_players().iteritems():
2017-03-06 04:28:30 +11:00
playqueue = playqueues.get_playqueue_from_type(player)
for i, item in enumerate(playqueue.items):
2017-12-08 17:53:01 +11:00
if item.ID == playqueue_item_id or item.plex_id == plex_id:
2017-03-06 04:28:30 +11:00
break
else:
2017-12-08 17:53:01 +11:00
LOG.debug('Item not found to skip to')
2017-03-06 04:28:30 +11:00
found = False
if found:
Player().play(playqueue.kodi_pl, None, False, i)
2017-03-05 03:54:24 +11:00
def convert_alexa_to_companion(dictionary):
2017-12-08 17:53:01 +11:00
"""
The params passed by Alexa must first be converted to Companion talk
"""
2017-03-05 03:54:24 +11:00
for key in dictionary:
if key in ALEXA_TO_COMPANION:
dictionary[ALEXA_TO_COMPANION[key]] = dictionary[key]
del dictionary[key]
def process_command(request_path, params, queue=None):
"""
queue: Queue() of PlexCompanion.py
"""
if params.get('deviceName') == 'Alexa':
convert_alexa_to_companion(params)
2017-12-08 17:53:01 +11:00
LOG.debug('Received request_path: %s, params: %s', request_path, params)
2017-03-05 03:54:24 +11:00
if "/playMedia" in request_path:
# We need to tell service.py
2017-03-06 03:51:58 +11:00
action = 'alexa' if params.get('deviceName') == 'Alexa' else 'playlist'
2017-03-05 03:54:24 +11:00
queue.put({
2017-03-06 03:51:58 +11:00
'action': action,
2017-03-05 03:54:24 +11:00
'data': params
})
elif request_path == 'player/playback/refreshPlayQueue':
queue.put({
'action': 'refreshPlayQueue',
'data': params
})
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/setParameters":
if 'volume' in params:
2017-12-08 17:53:01 +11:00
js.set_volume(int(params['volume']))
2017-05-31 20:28:26 +10:00
else:
2017-12-08 17:53:01 +11:00
LOG.error('Unknown parameters: %s', params)
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/play":
2017-12-08 17:53:01 +11:00
js.play()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/pause":
2017-12-08 17:53:01 +11:00
js.pause()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/stop":
2017-12-08 17:53:01 +11:00
js.stop()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/seekTo":
2017-12-08 17:53:01 +11:00
js.seek_to(int(params.get('offset', 0)))
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/stepForward":
2017-12-08 17:53:01 +11:00
js.smallforward()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/stepBack":
2017-12-08 17:53:01 +11:00
js.smallbackward()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/skipNext":
2017-12-08 17:53:01 +11:00
js.skipnext()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/skipPrevious":
2017-12-08 17:53:01 +11:00
js.skipprevious()
2017-03-05 03:54:24 +11:00
elif request_path == "player/playback/skipTo":
2017-12-08 17:53:01 +11:00
skip_to(params)
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/moveUp":
2017-12-08 17:53:01 +11:00
js.input_up()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/moveDown":
2017-12-08 17:53:01 +11:00
js.input_down()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/moveLeft":
2017-12-08 17:53:01 +11:00
js.input_left()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/moveRight":
2017-12-08 17:53:01 +11:00
js.input_right()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/select":
2017-12-08 17:53:01 +11:00
js.input_select()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/home":
2017-12-08 17:53:01 +11:00
js.input_home()
2017-03-05 03:54:24 +11:00
elif request_path == "player/navigation/back":
2017-12-08 17:53:01 +11:00
js.input_back()
elif request_path == "player/playback/setStreams":
queue.put({
'action': 'setStreams',
'data': params
})
2017-03-05 03:54:24 +11:00
else:
2017-12-08 17:53:01 +11:00
LOG.error('Unknown request path: %s', request_path)