PlexKodiConnect/resources/lib/companion.py

121 lines
4.0 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-06 04:28:30 +11:00
from xbmc import Player
2018-06-22 03:24:37 +10:00
from . import playqueue as PQ
from . import plex_functions as PF
from . import json_rpc as js
from . import variables as v
from . import state
2017-03-05 03:54:24 +11:00
###############################################################################
2018-06-22 03:24:37 +10:00
LOG = getLogger('PLEX.companion')
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-29 07:46:48 +11:00
playqueue_item_id = params.get('playQueueItemID')
2018-06-22 03:24:37 +10:00
_, plex_id = PF.GetPlexKeyNumber(params.get('key'))
2017-12-08 17:53:01 +11:00
LOG.debug('Skipping to playQueueItemID %s, plex_id %s',
playqueue_item_id, plex_id)
2017-03-06 04:28:30 +11:00
found = True
2017-12-29 07:46:48 +11:00
for player in js.get_players().values():
playqueue = PQ.PLAYQUEUES[player['playerid']]
2017-03-06 04:28:30 +11:00
for i, item in enumerate(playqueue.items):
2017-12-29 07:46:48 +11:00
if item.id == playqueue_item_id:
found = True
2017-03-06 04:28:30 +11:00
break
else:
2017-12-29 07:46:48 +11:00
for i, item in enumerate(playqueue.items):
if item.plex_id == plex_id:
found = True
break
if found is True:
2017-03-06 04:28:30 +11:00
Player().play(playqueue.kodi_pl, None, False, i)
2017-12-29 07:46:48 +11:00
else:
LOG.error('Item not found to skip to')
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:
2018-06-22 03:24:37 +10:00
if key in v.ALEXA_TO_COMPANION:
dictionary[v.ALEXA_TO_COMPANION[key]] = dictionary[key]
2017-03-05 03:54:24 +11:00
del dictionary[key]
def process_command(request_path, params):
2017-03-05 03:54:24 +11:00
"""
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-12-21 19:28:06 +11:00
if request_path == 'player/playback/playMedia':
2017-03-05 03:54:24 +11:00
# We need to tell service.py
2017-03-06 03:51:58 +11:00
action = 'alexa' if params.get('deviceName') == 'Alexa' else 'playlist'
state.COMPANION_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':
state.COMPANION_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":
state.COMPANION_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)