From bbb35856e0995438a26262a10d209040a1a90e24 Mon Sep 17 00:00:00 2001 From: croneter Date: Thu, 8 Feb 2018 11:16:39 +0100 Subject: [PATCH] Fix Alexa playback --- resources/lib/PlexCompanion.py | 37 +++++++++++++++++++++++----------- resources/lib/playback.py | 23 +++++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index f8437d5f..c79e99c3 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -9,12 +9,14 @@ from urllib import urlencode from xbmc import sleep, executebuiltin -from utils import settings, thread_methods +from utils import settings, thread_methods, language as lang, dialog from plexbmchelper import listener, plexgdm, subscribers, httppersist from plexbmchelper.subscribers import LOCKER -from PlexFunctions import ParseContainerKey, GetPlexMetadata +from PlexFunctions import ParseContainerKey, GetPlexMetadata, DownloadChunks from PlexAPI import API -from playlist_func import get_pms_playqueue, get_plextype_from_xml +from playlist_func import get_pms_playqueue, get_plextype_from_xml, \ + get_playlist_details_from_xml +from playback import playback_triage, play_xml import json_rpc as js import player import variables as v @@ -60,17 +62,28 @@ class PlexCompanion(Thread): PQ.init_playqueue_from_plex_children( api.getRatingKey(), transient_token=data.get('token')) + elif data['containerKey'].startswith('/playQueues/'): + _, container_key, _ = ParseContainerKey(data['containerKey']) + xml = DownloadChunks('{server}/playQueues/%s?' % container_key) + if xml is None: + # "Play error" + dialog('notification', lang(29999), lang(30128), icon='{error}') + return + playqueue = PQ.get_playqueue_from_type( + v.KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()]) + playqueue.clear() + get_playlist_details_from_xml(playqueue, xml) + if data.get('offset') != '0': + offset = float(data['offset']) / 1000.0 + else: + offset = None + play_xml(playqueue, xml, offset) else: state.PLEX_TRANSIENT_TOKEN = data.get('token') - params = { - 'mode': 'plex_node', - 'key': '{server}%s' % data.get('key'), - 'view_offset': data.get('offset'), - 'play_directly': 'true', - 'node': 'false' - } - executebuiltin('RunPlugin(plugin://%s?%s)' - % (v.ADDON_ID, urlencode(params))) + if data.get('offset') != '0': + state.RESUMABLE = True + state.RESUME_PLAYBACK = True + playback_triage(api.getRatingKey(), api.getType(), resolve=False) @staticmethod def _process_node(data): diff --git a/resources/lib/playback.py b/resources/lib/playback.py index 1bb3c492..ed8747de 100644 --- a/resources/lib/playback.py +++ b/resources/lib/playback.py @@ -336,19 +336,30 @@ def process_indirect(key, offset, resolve=True): thread.start() -def play_xml(playqueue, xml, offset=None): +def play_xml(playqueue, xml, offset=None, start_plex_id=None): """ Play all items contained in the xml passed in. Called by Plex Companion. + + Either supply the ratingKey of the starting Plex element. Or set + playqueue.selectedItemID """ - LOG.info("play_xml called") + LOG.info("play_xml called with offset %s, start_plex_id %s", + offset, start_plex_id) stack = _prep_playlist_stack(xml) _process_stack(playqueue, stack) LOG.debug('Playqueue after play_xml update: %s', playqueue) - for startpos, item in enumerate(playqueue.items): - if item.id == playqueue.selectedItemID: - break + if start_plex_id is not None: + for startpos, item in enumerate(playqueue.items): + if item.plex_id == start_plex_id: + break + else: + startpos = 0 else: - startpos = 0 + for startpos, item in enumerate(playqueue.items): + if item.id == playqueue.selectedItemID: + break + else: + startpos = 0 thread = Thread(target=threaded_playback, args=(playqueue.kodi_pl, startpos, offset)) LOG.info('Done play_xml, starting Kodi player at position %s', startpos)