2017-12-14 06:14:27 +11:00
|
|
|
"""
|
|
|
|
Manages getting playstate from Kodi and sending it to the PMS as well as
|
|
|
|
subscribed Plex Companion clients.
|
|
|
|
"""
|
|
|
|
from logging import getLogger
|
2018-01-01 23:28:39 +11:00
|
|
|
from threading import Thread, RLock
|
2017-12-09 23:47:19 +11:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
from downloadutils import DownloadUtils as DU
|
2017-12-21 19:28:06 +11:00
|
|
|
from utils import window, kodi_time_to_millis, Lock_Function
|
2017-05-18 04:22:16 +10:00
|
|
|
import state
|
2017-12-09 23:47:19 +11:00
|
|
|
import variables as v
|
|
|
|
import json_rpc as js
|
2016-01-15 22:12:52 +11:00
|
|
|
|
2016-09-03 01:20:19 +10:00
|
|
|
###############################################################################
|
|
|
|
|
2017-12-14 06:14:27 +11:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2017-12-21 19:28:06 +11:00
|
|
|
# Need to lock all methods and functions messing with subscribers or state
|
2018-01-01 23:28:39 +11:00
|
|
|
LOCK = RLock()
|
2017-12-21 19:28:06 +11:00
|
|
|
LOCKER = Lock_Function(LOCK)
|
2016-09-03 01:20:19 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2017-12-10 03:23:50 +11:00
|
|
|
# What is Companion controllable?
|
|
|
|
CONTROLLABLE = {
|
2018-01-01 23:28:39 +11:00
|
|
|
v.PLEX_PLAYLIST_TYPE_VIDEO: 'playPause,stop,volume,shuffle,audioStream,'
|
|
|
|
'subtitleStream,seekTo,skipPrevious,skipNext,'
|
|
|
|
'stepBack,stepForward',
|
|
|
|
v.PLEX_PLAYLIST_TYPE_AUDIO: 'playPause,stop,volume,shuffle,repeat,seekTo,'
|
|
|
|
'skipPrevious,skipNext,stepBack,stepForward',
|
2018-01-02 23:28:25 +11:00
|
|
|
v.PLEX_PLAYLIST_TYPE_PHOTO: 'playPause,stop,skipPrevious,skipNext'
|
2017-12-10 03:23:50 +11:00
|
|
|
}
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2017-12-14 20:22:48 +11:00
|
|
|
STREAM_DETAILS = {
|
|
|
|
'video': 'currentvideostream',
|
|
|
|
'audio': 'currentaudiostream',
|
|
|
|
'subtitle': 'currentsubtitle'
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:28:39 +11:00
|
|
|
XML = ('%s<MediaContainer commandID="{command_id}" location="{location}">\n'
|
|
|
|
' <Timeline {%s}/>\n'
|
|
|
|
' <Timeline {%s}/>\n'
|
|
|
|
' <Timeline {%s}/>\n'
|
|
|
|
'</MediaContainer>\n') % (v.XML_HEADER,
|
|
|
|
v.PLEX_PLAYLIST_TYPE_VIDEO,
|
|
|
|
v.PLEX_PLAYLIST_TYPE_AUDIO,
|
|
|
|
v.PLEX_PLAYLIST_TYPE_PHOTO)
|
|
|
|
|
2018-01-02 21:48:44 +11:00
|
|
|
# Headers are different for Plex Companion - use these for PMS notifications
|
|
|
|
HEADERS_PMS = {
|
2018-01-02 23:28:25 +11:00
|
|
|
'Connection': 'keep-alive',
|
2018-01-02 21:48:44 +11:00
|
|
|
'Accept': 'text/plain, */*; q=0.01',
|
|
|
|
'Accept-Language': 'en',
|
2018-01-02 23:28:25 +11:00
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
|
|
'User-Agent': '%s %s (%s)' % (v.ADDON_NAME, v.ADDON_VERSION, v.PLATFORM)
|
2018-01-02 21:48:44 +11:00
|
|
|
}
|
|
|
|
|
2018-01-01 23:28:39 +11:00
|
|
|
|
2018-01-02 21:48:44 +11:00
|
|
|
def params_pms():
|
2018-01-02 04:36:28 +11:00
|
|
|
"""
|
2018-01-02 21:48:44 +11:00
|
|
|
Returns the url parameters for communicating with the PMS
|
2018-01-02 04:36:28 +11:00
|
|
|
"""
|
|
|
|
return {
|
2018-01-02 23:28:25 +11:00
|
|
|
# 'X-Plex-Client-Capabilities': 'protocols=shoutcast,http-video;'
|
|
|
|
# 'videoDecoders=h264{profile:high&resolution:2160&level:52};'
|
|
|
|
# 'audioDecoders=mp3,aac,dts{bitrate:800000&channels:2},'
|
|
|
|
# 'ac3{bitrate:800000&channels:2}',
|
2018-01-02 04:36:28 +11:00
|
|
|
'X-Plex-Client-Identifier': v.PKC_MACHINE_IDENTIFIER,
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Device': v.PLATFORM,
|
|
|
|
'X-Plex-Device-Name': v.DEVICENAME,
|
2018-01-02 23:28:25 +11:00
|
|
|
# 'X-Plex-Device-Screen-Resolution': '1916x1018,1920x1080',
|
|
|
|
'X-Plex-Model': 'unknown',
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Platform': v.PLATFORM,
|
2018-01-02 23:28:25 +11:00
|
|
|
'X-Plex-Platform-Version': 'unknown',
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Product': v.ADDON_NAME,
|
2018-01-02 23:28:25 +11:00
|
|
|
'X-Plex-Provider-Version': v.ADDON_VERSION,
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Version': v.ADDON_VERSION,
|
|
|
|
'hasMDE': '1',
|
|
|
|
# 'X-Plex-Session-Identifier': ['vinuvirm6m20iuw9c4cx1dcx'],
|
2018-01-02 04:36:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def headers_companion_client():
|
|
|
|
"""
|
|
|
|
Headers are different for Plex Companion - use these for a Plex Companion
|
|
|
|
client
|
|
|
|
"""
|
|
|
|
return {
|
2018-01-02 22:13:41 +11:00
|
|
|
'Content-Type': 'application/xml',
|
2018-01-02 04:36:28 +11:00
|
|
|
'Connection': 'Keep-Alive',
|
|
|
|
'X-Plex-Client-Identifier': v.PKC_MACHINE_IDENTIFIER,
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Device-Name': v.DEVICENAME,
|
|
|
|
'X-Plex-Platform': v.PLATFORM,
|
2018-01-02 22:13:41 +11:00
|
|
|
'X-Plex-Platform-Version': 'unknown',
|
2018-01-02 21:48:44 +11:00
|
|
|
'X-Plex-Product': v.ADDON_NAME,
|
|
|
|
'X-Plex-Version': v.ADDON_VERSION,
|
2018-01-02 04:36:28 +11:00
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
|
|
'Accept-Language': 'en,*'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 23:28:39 +11:00
|
|
|
def update_player_info(playerid):
|
|
|
|
"""
|
|
|
|
Updates all player info for playerid [int] in state.py.
|
|
|
|
"""
|
|
|
|
state.PLAYER_STATES[playerid].update(js.get_player_props(playerid))
|
|
|
|
state.PLAYER_STATES[playerid]['volume'] = js.get_volume()
|
|
|
|
state.PLAYER_STATES[playerid]['muted'] = js.get_muted()
|
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
|
|
|
|
class SubscriptionMgr(object):
|
2017-12-14 06:14:27 +11:00
|
|
|
"""
|
|
|
|
Manages Plex companion subscriptions
|
|
|
|
"""
|
2017-12-14 18:29:38 +11:00
|
|
|
def __init__(self, request_mgr, player, mgr):
|
2016-04-03 01:46:23 +11:00
|
|
|
self.serverlist = []
|
2016-01-15 22:12:52 +11:00
|
|
|
self.subscribers = {}
|
|
|
|
self.info = {}
|
|
|
|
self.server = ""
|
|
|
|
self.protocol = "http"
|
|
|
|
self.port = ""
|
2017-12-21 19:28:06 +11:00
|
|
|
self.isplaying = False
|
2018-01-02 04:36:28 +11:00
|
|
|
self.last_timelines = {}
|
2017-12-14 06:14:27 +11:00
|
|
|
# In order to be able to signal a stop at the end
|
|
|
|
self.last_params = {}
|
|
|
|
self.lastplayers = {}
|
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
self.xbmcplayer = player
|
2016-12-28 23:14:21 +11:00
|
|
|
self.playqueue = mgr.playqueue
|
2017-12-14 18:29:38 +11:00
|
|
|
self.request_mgr = request_mgr
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2018-01-02 04:36:28 +11:00
|
|
|
|
2017-12-14 06:14:27 +11:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
def _server_by_host(self, host):
|
2016-04-03 01:46:23 +11:00
|
|
|
if len(self.serverlist) == 1:
|
|
|
|
return self.serverlist[0]
|
|
|
|
for server in self.serverlist:
|
|
|
|
if (server.get('serverName') in host or
|
|
|
|
server.get('server') in host):
|
|
|
|
return server
|
|
|
|
return {}
|
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-14 20:22:48 +11:00
|
|
|
def msg(self, players):
|
|
|
|
"""
|
|
|
|
Returns a timeline xml as str
|
|
|
|
(xml containing video, audio, photo player state)
|
|
|
|
"""
|
2018-01-01 23:28:39 +11:00
|
|
|
self.isplaying = False
|
|
|
|
answ = str(XML)
|
|
|
|
timelines = {
|
|
|
|
v.PLEX_PLAYLIST_TYPE_VIDEO: None,
|
|
|
|
v.PLEX_PLAYLIST_TYPE_AUDIO: None,
|
|
|
|
v.PLEX_PLAYLIST_TYPE_PHOTO: None
|
|
|
|
}
|
|
|
|
for typus in timelines:
|
|
|
|
if players.get(v.KODI_PLAYLIST_TYPE_FROM_PLEX_PLAYLIST_TYPE[typus]) is None:
|
2018-01-02 04:36:28 +11:00
|
|
|
timeline = self._dict_to_xml({
|
2018-01-01 23:28:39 +11:00
|
|
|
'controllable': CONTROLLABLE[typus],
|
|
|
|
'type': typus,
|
|
|
|
'state': 'stopped'
|
2018-01-02 04:36:28 +11:00
|
|
|
})
|
2018-01-01 23:28:39 +11:00
|
|
|
else:
|
2018-01-02 04:36:28 +11:00
|
|
|
try:
|
|
|
|
timeline = self._dict_to_xml(self._timeline_dict(players[
|
|
|
|
v.KODI_PLAYLIST_TYPE_FROM_PLEX_PLAYLIST_TYPE[typus]],
|
|
|
|
typus))
|
|
|
|
except RuntimeError:
|
|
|
|
try:
|
|
|
|
timeline = self.last_timelines[typus]
|
|
|
|
except KeyError:
|
|
|
|
# On startup
|
|
|
|
timeline = self._dict_to_xml({
|
|
|
|
'controllable': CONTROLLABLE[typus],
|
|
|
|
'type': typus,
|
|
|
|
'state': 'stopped'
|
|
|
|
})
|
|
|
|
timelines[typus] = timeline
|
2018-01-01 23:28:39 +11:00
|
|
|
location = 'fullScreenVideo' if self.isplaying else 'navigation'
|
2018-01-02 04:36:28 +11:00
|
|
|
self.last_timelines = dict(timelines)
|
2018-01-01 23:28:39 +11:00
|
|
|
timelines.update({'command_id': '{command_id}', 'location': location})
|
|
|
|
return answ.format(**timelines)
|
2017-12-14 20:22:48 +11:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@staticmethod
|
2018-01-01 23:28:39 +11:00
|
|
|
def _dict_to_xml(dictionary):
|
2017-12-21 19:28:06 +11:00
|
|
|
"""
|
2018-01-01 23:28:39 +11:00
|
|
|
Returns the string 'key1="value1" key2="value2" ...' for dictionary
|
2017-12-21 19:28:06 +11:00
|
|
|
"""
|
2018-01-01 23:28:39 +11:00
|
|
|
answ = ''
|
|
|
|
for key, value in dictionary.iteritems():
|
|
|
|
answ += '%s="%s" ' % (key, value)
|
|
|
|
return answ
|
|
|
|
|
|
|
|
def _timeline_dict(self, player, ptype):
|
2017-12-14 06:14:27 +11:00
|
|
|
playerid = player['playerid']
|
2018-01-01 23:28:39 +11:00
|
|
|
info = state.PLAYER_STATES[playerid]
|
2017-12-21 19:28:06 +11:00
|
|
|
playqueue = self.playqueue.playqueues[playerid]
|
|
|
|
pos = info['position']
|
|
|
|
try:
|
2018-01-02 04:36:28 +11:00
|
|
|
item = playqueue.items[pos]
|
2017-12-21 19:28:06 +11:00
|
|
|
except IndexError:
|
|
|
|
# E.g. for direct path playback for single item
|
2018-01-01 23:28:39 +11:00
|
|
|
return {
|
|
|
|
'controllable': CONTROLLABLE[ptype],
|
|
|
|
'type': ptype,
|
|
|
|
'state': 'stopped'
|
|
|
|
}
|
2018-01-02 04:36:28 +11:00
|
|
|
self.isplaying = True
|
|
|
|
if item.plex_id != info['plex_id']:
|
|
|
|
# Kodi playqueue already progressed; need to wait until everything
|
|
|
|
# is loaded
|
|
|
|
raise RuntimeError
|
2016-03-23 01:34:59 +11:00
|
|
|
pbmc_server = window('pms_server')
|
2016-03-23 00:58:51 +11:00
|
|
|
if pbmc_server:
|
2017-12-11 05:01:22 +11:00
|
|
|
(self.protocol, self.server, self.port) = pbmc_server.split(':')
|
2016-03-23 00:58:51 +11:00
|
|
|
self.server = self.server.replace('/', '')
|
2018-01-01 23:28:39 +11:00
|
|
|
status = 'paused' if info['speed'] == '0' else 'playing'
|
|
|
|
duration = kodi_time_to_millis(info['totaltime'])
|
|
|
|
shuffle = '1' if info['shuffled'] else '0'
|
|
|
|
mute = '1' if info['muted'] is True else '0'
|
|
|
|
answ = {
|
|
|
|
'location': 'fullScreenVideo',
|
|
|
|
'controllable': CONTROLLABLE[ptype],
|
|
|
|
'protocol': self.protocol,
|
|
|
|
'address': self.server,
|
|
|
|
'port': self.port,
|
|
|
|
'machineIdentifier': window('plex_machineIdentifier'),
|
|
|
|
'state': status,
|
|
|
|
'type': ptype,
|
|
|
|
'itemType': ptype,
|
|
|
|
'time': kodi_time_to_millis(info['time']),
|
|
|
|
'duration': duration,
|
|
|
|
'seekRange': '0-%s' % duration,
|
|
|
|
'shuffle': shuffle,
|
|
|
|
'repeat': v.PLEX_REPEAT_FROM_KODI_REPEAT[info['repeat']],
|
|
|
|
'volume': info['volume'],
|
|
|
|
'mute': mute,
|
2018-01-02 23:28:25 +11:00
|
|
|
'mediaIndex': 0, # Still to implement from here
|
2018-01-02 00:23:08 +11:00
|
|
|
'partIndex':0,
|
2018-01-02 23:28:25 +11:00
|
|
|
'partCount': 1,
|
2018-01-01 23:28:39 +11:00
|
|
|
'providerIdentifier': 'com.plexapp.plugins.library',
|
|
|
|
}
|
2018-01-02 04:36:28 +11:00
|
|
|
# Get the plex id from the PKC playqueue not info, as Kodi jumps to next
|
|
|
|
# playqueue element way BEFORE kodi monitor onplayback is called
|
|
|
|
if item.plex_id:
|
|
|
|
answ['key'] = '/library/metadata/%s' % item.plex_id
|
|
|
|
answ['ratingKey'] = item.plex_id
|
2017-12-11 05:01:22 +11:00
|
|
|
# PlayQueue stuff
|
2018-01-01 23:28:39 +11:00
|
|
|
if info['container_key']:
|
|
|
|
answ['containerKey'] = info['container_key']
|
|
|
|
if (info['container_key'] is not None and
|
|
|
|
info['container_key'].startswith('/playQueues')):
|
|
|
|
answ['playQueueID'] = playqueue.id
|
|
|
|
answ['playQueueVersion'] = playqueue.version
|
2018-01-02 04:36:28 +11:00
|
|
|
answ['playQueueItemID'] = item.id
|
2018-01-01 23:28:39 +11:00
|
|
|
if playqueue.items[pos].guid:
|
2018-01-02 04:36:28 +11:00
|
|
|
answ['guid'] = item.guid
|
2017-12-11 05:01:22 +11:00
|
|
|
# Temp. token set?
|
2017-05-18 04:22:16 +10:00
|
|
|
if state.PLEX_TRANSIENT_TOKEN:
|
2018-01-01 23:28:39 +11:00
|
|
|
answ['token'] = state.PLEX_TRANSIENT_TOKEN
|
2017-12-11 05:01:22 +11:00
|
|
|
elif playqueue.plex_transient_token:
|
2018-01-01 23:28:39 +11:00
|
|
|
answ['token'] = playqueue.plex_transient_token
|
2017-12-21 19:28:06 +11:00
|
|
|
# Process audio and subtitle streams
|
2018-01-01 23:28:39 +11:00
|
|
|
if ptype != v.PLEX_PLAYLIST_TYPE_PHOTO:
|
2017-12-21 19:28:06 +11:00
|
|
|
strm_id = self._plex_stream_index(playerid, 'audio')
|
2018-01-01 23:28:39 +11:00
|
|
|
if strm_id:
|
|
|
|
answ['audioStreamID'] = strm_id
|
2017-12-14 20:22:48 +11:00
|
|
|
else:
|
|
|
|
LOG.error('We could not select a Plex audiostream')
|
2018-01-01 23:28:39 +11:00
|
|
|
if ptype == v.PLEX_PLAYLIST_TYPE_VIDEO:
|
|
|
|
strm_id = self._plex_stream_index(playerid, 'video')
|
|
|
|
if strm_id:
|
|
|
|
answ['videoStreamID'] = strm_id
|
|
|
|
else:
|
|
|
|
LOG.error('We could not select a Plex videostream')
|
|
|
|
if info['subtitleenabled']:
|
|
|
|
try:
|
|
|
|
strm_id = self._plex_stream_index(playerid, 'subtitle')
|
|
|
|
except KeyError:
|
|
|
|
# subtitleenabled can be True while currentsubtitle can
|
|
|
|
# still be {}
|
|
|
|
strm_id = None
|
|
|
|
if strm_id is not None:
|
|
|
|
# If None, then the subtitle is only present on Kodi side
|
|
|
|
answ['subtitleStreamID'] = strm_id
|
|
|
|
return answ
|
|
|
|
|
|
|
|
def signal_stop(self):
|
|
|
|
"""
|
|
|
|
Externally called on PKC shutdown to ensure that PKC signals a stop to
|
|
|
|
the PMS. Otherwise, PKC might be stuck at "currently playing"
|
|
|
|
"""
|
|
|
|
LOG.info('Signaling a complete stop to PMS')
|
|
|
|
# To avoid RuntimeError, don't use self.lastplayers
|
|
|
|
for playerid in (0, 1, 2):
|
|
|
|
self.last_params['state'] = 'stopped'
|
|
|
|
self._send_pms_notification(playerid, self.last_params)
|
|
|
|
|
|
|
|
def _plex_stream_index(self, playerid, stream_type):
|
|
|
|
"""
|
|
|
|
Returns the current Plex stream index [str] for the player playerid
|
|
|
|
|
|
|
|
stream_type: 'video', 'audio', 'subtitle'
|
|
|
|
"""
|
|
|
|
playqueue = self.playqueue.playqueues[playerid]
|
|
|
|
info = state.PLAYER_STATES[playerid]
|
|
|
|
return playqueue.items[info['position']].plex_stream_index(
|
|
|
|
info[STREAM_DETAILS[stream_type]]['index'], stream_type)
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-14 18:29:38 +11:00
|
|
|
def update_command_id(self, uuid, command_id):
|
|
|
|
"""
|
|
|
|
Updates the Plex Companien client with the machine identifier uuid with
|
|
|
|
command_id
|
|
|
|
"""
|
|
|
|
if command_id and self.subscribers.get(uuid):
|
|
|
|
self.subscribers[uuid].command_id = int(command_id)
|
2016-08-11 03:03:37 +10:00
|
|
|
|
2018-01-01 23:28:39 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-14 18:29:38 +11:00
|
|
|
def notify(self):
|
|
|
|
"""
|
|
|
|
Causes PKC to tell the PMS and Plex Companion players to receive a
|
|
|
|
notification what's being played.
|
|
|
|
"""
|
2018-01-01 23:28:39 +11:00
|
|
|
self._cleanup()
|
|
|
|
# Get all the active/playing Kodi players (video, audio, pictures)
|
2017-12-09 23:47:19 +11:00
|
|
|
players = js.get_players()
|
2018-01-01 23:28:39 +11:00
|
|
|
# Update the PKC info with what's playing on the Kodi side
|
|
|
|
for player in players.values():
|
|
|
|
update_player_info(player['playerid'])
|
2018-01-02 04:36:28 +11:00
|
|
|
self._notify_server(players)
|
2018-01-03 00:30:54 +11:00
|
|
|
if self.subscribers:
|
2018-01-01 23:28:39 +11:00
|
|
|
msg = self.msg(players)
|
2017-12-21 19:28:06 +11:00
|
|
|
if self.isplaying is True:
|
|
|
|
# If we don't check here, Plex Companion devices will simply
|
|
|
|
# drop out of the Plex Companion playback screen
|
2017-12-14 06:14:27 +11:00
|
|
|
for subscriber in self.subscribers.values():
|
2018-01-02 20:58:28 +11:00
|
|
|
subscriber.send_update(msg)
|
2018-01-01 23:28:39 +11:00
|
|
|
self.lastplayers = players
|
2016-08-11 03:03:37 +10:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
def _notify_server(self, players):
|
2017-12-14 06:14:27 +11:00
|
|
|
for typus, player in players.iteritems():
|
|
|
|
self._send_pms_notification(
|
|
|
|
player['playerid'], self._get_pms_params(player['playerid']))
|
2016-08-11 03:31:54 +10:00
|
|
|
try:
|
|
|
|
del self.lastplayers[typus]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
# Process the players we have left (to signal a stop)
|
2018-01-01 23:28:39 +11:00
|
|
|
for player in self.lastplayers.values():
|
2017-12-14 06:14:27 +11:00
|
|
|
self.last_params['state'] = 'stopped'
|
|
|
|
self._send_pms_notification(player['playerid'], self.last_params)
|
2016-08-11 03:31:54 +10:00
|
|
|
|
2017-12-14 06:14:27 +11:00
|
|
|
def _get_pms_params(self, playerid):
|
|
|
|
info = state.PLAYER_STATES[playerid]
|
2018-01-02 04:36:28 +11:00
|
|
|
playqueue = self.playqueue.playqueues[playerid]
|
|
|
|
try:
|
|
|
|
item = playqueue.items[info['position']]
|
|
|
|
except IndexError:
|
|
|
|
return self.last_params
|
2017-12-14 06:14:27 +11:00
|
|
|
status = 'paused' if info['speed'] == '0' else 'playing'
|
2017-12-14 06:41:29 +11:00
|
|
|
params = {
|
|
|
|
'state': status,
|
2018-01-02 04:36:28 +11:00
|
|
|
'ratingKey': item.plex_id,
|
|
|
|
'key': '/library/metadata/%s' % item.plex_id,
|
2017-12-14 06:14:27 +11:00
|
|
|
'time': kodi_time_to_millis(info['time']),
|
|
|
|
'duration': kodi_time_to_millis(info['totaltime'])
|
2016-08-11 03:31:54 +10:00
|
|
|
}
|
2018-01-01 23:28:39 +11:00
|
|
|
if info['container_key'] is not None:
|
2018-01-02 21:48:44 +11:00
|
|
|
# params['containerKey'] = info['container_key']
|
2018-01-01 23:28:39 +11:00
|
|
|
if info['container_key'].startswith('/playQueues/'):
|
2018-01-02 21:48:44 +11:00
|
|
|
# params['playQueueVersion'] = playqueue.version
|
|
|
|
# params['playQueueID'] = playqueue.id
|
2018-01-02 04:36:28 +11:00
|
|
|
params['playQueueItemID'] = item.id
|
2017-12-14 06:14:27 +11:00
|
|
|
self.last_params = params
|
|
|
|
return params
|
|
|
|
|
|
|
|
def _send_pms_notification(self, playerid, params):
|
2017-12-14 18:29:38 +11:00
|
|
|
serv = self._server_by_host(self.server)
|
2017-12-14 06:14:27 +11:00
|
|
|
playqueue = self.playqueue.playqueues[playerid]
|
2018-01-02 21:48:44 +11:00
|
|
|
xargs = params_pms()
|
|
|
|
xargs.update(params)
|
2017-05-18 04:22:16 +10:00
|
|
|
if state.PLEX_TRANSIENT_TOKEN:
|
2017-05-23 04:54:04 +10:00
|
|
|
xargs['X-Plex-Token'] = state.PLEX_TRANSIENT_TOKEN
|
2017-05-25 22:21:27 +10:00
|
|
|
elif playqueue.plex_transient_token:
|
|
|
|
xargs['X-Plex-Token'] = playqueue.plex_transient_token
|
2017-12-21 19:28:06 +11:00
|
|
|
elif state.PLEX_TOKEN:
|
|
|
|
xargs['X-Plex-Token'] = state.PLEX_TOKEN
|
2016-08-11 03:31:54 +10:00
|
|
|
url = '%s://%s:%s/:/timeline' % (serv.get('protocol', 'http'),
|
|
|
|
serv.get('server', 'localhost'),
|
|
|
|
serv.get('port', '32400'))
|
2018-01-02 21:48:44 +11:00
|
|
|
DU().downloadUrl(url,
|
|
|
|
authenticate=False,
|
|
|
|
parameters=xargs,
|
|
|
|
headerOverride=HEADERS_PMS)
|
2017-12-14 06:14:27 +11:00
|
|
|
LOG.debug("Sent server notification with parameters: %s to %s",
|
2018-01-02 21:48:44 +11:00
|
|
|
xargs, url)
|
2016-01-27 22:18:54 +11:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-14 18:29:38 +11:00
|
|
|
def add_subscriber(self, protocol, host, port, uuid, command_id):
|
|
|
|
"""
|
|
|
|
Adds a new Plex Companion subscriber to PKC.
|
|
|
|
"""
|
2017-12-14 06:14:27 +11:00
|
|
|
subscriber = Subscriber(protocol,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
uuid,
|
2017-12-14 18:29:38 +11:00
|
|
|
command_id,
|
2017-12-14 06:14:27 +11:00
|
|
|
self,
|
2017-12-14 18:29:38 +11:00
|
|
|
self.request_mgr)
|
2017-12-21 19:28:06 +11:00
|
|
|
self.subscribers[subscriber.uuid] = subscriber
|
2017-12-14 06:14:27 +11:00
|
|
|
return subscriber
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-14 18:29:38 +11:00
|
|
|
def remove_subscriber(self, uuid):
|
|
|
|
"""
|
|
|
|
Removes a connected Plex Companion subscriber with machine identifier
|
|
|
|
uuid from PKC notifications.
|
|
|
|
(Calls the cleanup() method of the subscriber)
|
|
|
|
"""
|
2017-12-21 19:28:06 +11:00
|
|
|
for subscriber in self.subscribers.values():
|
|
|
|
if subscriber.uuid == uuid or subscriber.host == uuid:
|
|
|
|
subscriber.cleanup()
|
|
|
|
del self.subscribers[subscriber.uuid]
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
def _cleanup(self):
|
2017-12-21 19:28:06 +11:00
|
|
|
for subscriber in self.subscribers.values():
|
|
|
|
if subscriber.age > 30:
|
|
|
|
subscriber.cleanup()
|
|
|
|
del self.subscribers[subscriber.uuid]
|
2016-01-15 22:12:52 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
class Subscriber(object):
|
|
|
|
"""
|
|
|
|
Plex Companion subscribing device
|
|
|
|
"""
|
|
|
|
def __init__(self, protocol, host, port, uuid, command_id, sub_mgr,
|
|
|
|
request_mgr):
|
2016-01-15 22:12:52 +11:00
|
|
|
self.protocol = protocol or "http"
|
|
|
|
self.host = host
|
|
|
|
self.port = port or 32400
|
|
|
|
self.uuid = uuid or host
|
2017-12-14 18:29:38 +11:00
|
|
|
self.command_id = int(command_id) or 0
|
2016-01-15 22:12:52 +11:00
|
|
|
self.age = 0
|
2017-12-14 18:29:38 +11:00
|
|
|
self.sub_mgr = sub_mgr
|
|
|
|
self.request_mgr = request_mgr
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.uuid == other.uuid
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def cleanup(self):
|
2017-12-14 18:29:38 +11:00
|
|
|
"""
|
|
|
|
Closes the connection to the Plex Companion client
|
|
|
|
"""
|
|
|
|
self.request_mgr.closeConnection(self.protocol, self.host, self.port)
|
2016-04-03 01:46:23 +11:00
|
|
|
|
2018-01-02 20:58:28 +11:00
|
|
|
def send_update(self, msg):
|
2017-12-14 18:29:38 +11:00
|
|
|
"""
|
|
|
|
Sends msg to the Plex Companion client (via .../:/timeline)
|
|
|
|
"""
|
2016-01-15 22:12:52 +11:00
|
|
|
self.age += 1
|
2018-01-01 23:28:39 +11:00
|
|
|
msg = msg.format(command_id=self.command_id)
|
2017-12-14 06:14:27 +11:00
|
|
|
LOG.debug("sending xml to subscriber uuid=%s,commandID=%i:\n%s",
|
2017-12-14 18:29:38 +11:00
|
|
|
self.uuid, self.command_id, msg)
|
2018-01-01 23:28:39 +11:00
|
|
|
url = '%s://%s:%s/:/timeline' % (self.protocol, self.host, self.port)
|
2017-12-14 18:29:38 +11:00
|
|
|
thread = Thread(target=self._threaded_send, args=(url, msg))
|
|
|
|
thread.start()
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2017-12-14 18:29:38 +11:00
|
|
|
def _threaded_send(self, url, msg):
|
2016-02-11 20:35:16 +11:00
|
|
|
"""
|
2018-01-02 21:48:44 +11:00
|
|
|
Threaded POST request, because they stall due to response missing
|
2016-02-11 20:35:16 +11:00
|
|
|
the Content-Length header :-(
|
|
|
|
"""
|
2018-01-02 04:36:28 +11:00
|
|
|
response = DU().downloadUrl(url,
|
|
|
|
action_type="POST",
|
2018-01-02 21:48:44 +11:00
|
|
|
postBody=msg,
|
|
|
|
authenticate=False,
|
|
|
|
headerOverride=headers_companion_client())
|
2017-12-14 18:29:38 +11:00
|
|
|
if response in (False, None, 401):
|
|
|
|
self.sub_mgr.remove_subscriber(self.uuid)
|