Cleanup Plex Companion

This commit is contained in:
tomkat83 2016-04-11 12:34:38 +02:00
parent 18ff3715ac
commit 8be55d898c

View file

@ -27,118 +27,120 @@ class MyHandler(BaseHTTPRequestHandler):
return server
return {}
def do_HEAD(s):
s.logMsg("Serving HEAD request...", 2)
s.answer_request(0)
def do_HEAD(self):
self.logMsg("Serving HEAD request...", 2)
self.answer_request(0)
def do_GET(s):
s.logMsg("Serving GET request...", 2)
s.answer_request(1)
def do_GET(self):
self.logMsg("Serving GET request...", 2)
self.answer_request(1)
def do_OPTIONS(s):
s.send_response(200)
s.send_header('Content-Length', '0')
s.send_header('X-Plex-Client-Identifier', s.server.settings['uuid'])
s.send_header('Content-Type', 'text/plain')
s.send_header('Connection', 'close')
s.send_header('Access-Control-Max-Age', '1209600')
s.send_header('Access-Control-Allow-Origin', '*')
s.send_header('Access-Control-Allow-Methods',
def do_OPTIONS(self):
self.send_response(200)
self.send_header('Content-Length', '0')
self.send_header('X-Plex-Client-Identifier', self.server.settings['uuid'])
self.send_header('Content-Type', 'text/plain')
self.send_header('Connection', 'close')
self.send_header('Access-Control-Max-Age', '1209600')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods',
'POST, GET, OPTIONS, DELETE, PUT, HEAD')
s.send_header('Access-Control-Allow-Headers',
self.send_header('Access-Control-Allow-Headers',
'x-plex-version, x-plex-platform-version, '
'x-plex-username, x-plex-client-identifier, '
'x-plex-target-client-identifier, x-plex-device-name, '
'x-plex-platform, x-plex-product, accept, x-plex-device')
s.end_headers()
s.wfile.close()
self.end_headers()
self.wfile.close()
def response(s, body, headers={}, code=200):
def sendOK(self):
self.send_response(200)
def response(self, body, headers={}, code=200):
try:
s.send_response(code)
self.send_response(code)
for key in headers:
s.send_header(key, headers[key])
s.send_header('Content-Length', len(body))
s.send_header('Connection', "close")
s.end_headers()
s.wfile.write(body)
s.wfile.close()
self.send_header(key, headers[key])
self.send_header('Content-Length', len(body))
self.send_header('Connection', "close")
self.end_headers()
self.wfile.write(body)
self.wfile.close()
except:
pass
def answer_request(s, sendData):
s.serverlist = s.server.client.getServerList()
s.subMgr = s.server.subscriptionManager
s.js = s.server.jsonClass
s.settings = s.server.settings
def answer_request(self, sendData):
self.serverlist = self.server.client.getServerList()
self.subMgr = self.server.subscriptionManager
self.js = self.server.jsonClass
self.settings = self.server.settings
try:
request_path = s.path[1:]
request_path = self.path[1:]
request_path = re.sub(r"\?.*", "", request_path)
url = urlparse(s.path)
url = urlparse(self.path)
paramarrays = parse_qs(url.query)
params = {}
for key in paramarrays:
params[key] = paramarrays[key][0]
s.logMsg("request path is: [%s]" % (request_path,), 2)
s.logMsg("params are: %s" % params, 2)
s.subMgr.updateCommandID(s.headers.get('X-Plex-Client-Identifier', s.client_address[0]), params.get('commandID', False))
self.logMsg("params received from remote: %s" % params, 2)
self.subMgr.updateCommandID(self.headers.get('X-Plex-Client-Identifier', self.client_address[0]), params.get('commandID', False))
if request_path=="version":
s.response("PleXBMC Helper Remote Redirector: Running\r\nVersion: %s" % s.settings['version'])
self.response("PleXBMC Helper Remote Redirector: Running\r\nVersion: %s" % self.settings['version'])
elif request_path=="verify":
result=s.js.jsonrpc("ping")
s.response("XBMC JSON connection test:\r\n"+result)
result=self.js.jsonrpc("ping")
self.response("XBMC JSON connection test:\r\n"+result)
elif "resources" == request_path:
resp = getXMLHeader()
resp += "<MediaContainer>"
resp += "<Player"
resp += ' title="%s"' % s.settings['client_name']
resp += ' title="%s"' % self.settings['client_name']
resp += ' protocol="plex"'
resp += ' protocolVersion="1"'
resp += ' protocolCapabilities="navigation,playback,timeline"'
resp += ' machineIdentifier="%s"' % s.settings['uuid']
resp += ' machineIdentifier="%s"' % self.settings['uuid']
resp += ' product="PlexKodiConnect"'
resp += ' platform="%s"' % s.settings['platform']
resp += ' platformVersion="%s"' % s.settings['plexbmc_version']
resp += ' platform="%s"' % self.settings['platform']
resp += ' platformVersion="%s"' % self.settings['plexbmc_version']
resp += ' deviceClass="pc"'
resp += "/>"
resp += "</MediaContainer>"
s.logMsg("crafted resources response: %s" % resp, 2)
s.response(resp, s.js.getPlexHeaders())
self.logMsg("crafted resources response: %s" % resp, 2)
self.response(resp, self.js.getPlexHeaders())
elif "/subscribe" in request_path:
s.response(getOKMsg(), s.js.getPlexHeaders())
self.response(getOKMsg(), self.js.getPlexHeaders())
protocol = params.get('protocol', False)
host = s.client_address[0]
host = self.client_address[0]
port = params.get('port', False)
uuid = s.headers.get('X-Plex-Client-Identifier', "")
uuid = self.headers.get('X-Plex-Client-Identifier', "")
commandID = params.get('commandID', 0)
s.subMgr.addSubscriber(protocol, host, port, uuid, commandID)
self.subMgr.addSubscriber(protocol, host, port, uuid, commandID)
elif "/poll" in request_path:
if params.get('wait', False) == '1':
sleep(950)
commandID = params.get('commandID', 0)
s.response(re.sub(r"INSERTCOMMANDID", str(commandID), s.subMgr.msg(s.js.getPlayers())), {
'X-Plex-Client-Identifier': s.settings['uuid'],
self.response(re.sub(r"INSERTCOMMANDID", str(commandID), self.subMgr.msg(self.js.getPlayers())), {
'X-Plex-Client-Identifier': self.settings['uuid'],
'Access-Control-Expose-Headers': 'X-Plex-Client-Identifier',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/xml'
})
elif "/unsubscribe" in request_path:
s.response(getOKMsg(), s.js.getPlexHeaders())
uuid = s.headers.get('X-Plex-Client-Identifier', False) or s.client_address[0]
s.subMgr.removeSubscriber(uuid)
self.response(getOKMsg(), self.js.getPlexHeaders())
uuid = self.headers.get('X-Plex-Client-Identifier', False) or self.client_address[0]
self.subMgr.removeSubscriber(uuid)
elif request_path == "player/playback/setParameters":
s.response(getOKMsg(), s.js.getPlexHeaders())
self.response(getOKMsg(), self.js.getPlexHeaders())
if 'volume' in params:
volume = int(params['volume'])
s.logMsg("adjusting the volume to %s%%" % volume, 2)
s.js.jsonrpc("Application.SetVolume", {"volume": volume})
self.logMsg("adjusting the volume to %s%%" % volume, 2)
self.js.jsonrpc("Application.SetVolume", {"volume": volume})
elif "/playMedia" in request_path:
s.response(getOKMsg(), s.js.getPlexHeaders())
self.response(getOKMsg(), self.js.getPlexHeaders())
offset = params.get('viewOffset', params.get('offset', "0"))
protocol = params.get('protocol', "http")
address = params.get('address', s.client_address[0])
server = s.getServerByHost(address)
address = params.get('address', self.client_address[0])
server = self.getServerByHost(address)
port = params.get('port', server.get('port', '32400'))
try:
containerKey = urlparse(params.get('containerKey')).path
@ -150,76 +152,95 @@ class MyHandler(BaseHTTPRequestHandler):
except IndexError:
playQueueID = ''
s.js.jsonrpc("playmedia", params)
s.subMgr.lastkey = params['key']
s.subMgr.containerKey = containerKey
s.subMgr.playQueueID = playQueueID
s.subMgr.server = server.get('server', 'localhost')
s.subMgr.port = port
s.subMgr.protocol = protocol
s.subMgr.notify()
self.js.jsonrpc("playmedia", params)
self.subMgr.lastkey = params['key']
self.subMgr.containerKey = containerKey
self.subMgr.playQueueID = playQueueID
self.subMgr.server = server.get('server', 'localhost')
self.subMgr.port = port
self.subMgr.protocol = protocol
self.subMgr.notify()
elif request_path == "player/playback/play":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.PlayPause", {"playerid" : playerid, "play": True})
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.PlayPause", {"playerid" : playerid, "play": True})
elif request_path == "player/playback/pause":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.PlayPause", {"playerid" : playerid, "play": False})
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.PlayPause", {"playerid" : playerid, "play": False})
elif request_path == "player/playback/stop":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Stop", {"playerid" : playerid})
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Stop", {"playerid" : playerid})
elif request_path == "player/playback/seekTo":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":millisToTime(params.get('offset', 0))})
s.subMgr.notify()
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":millisToTime(params.get('offset', 0))})
self.subMgr.notify()
elif request_path == "player/playback/stepForward":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"smallforward"})
s.subMgr.notify()
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"smallforward"})
self.subMgr.notify()
elif request_path == "player/playback/stepBack":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"smallbackward"})
s.subMgr.notify()
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"smallbackward"})
self.subMgr.notify()
elif request_path == "player/playback/skipNext":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"bigforward"})
s.subMgr.notify()
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"bigforward"})
self.subMgr.notify()
elif request_path == "player/playback/skipPrevious":
s.response(getOKMsg(), s.js.getPlexHeaders())
for playerid in s.js.getPlayerIds():
s.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"bigbackward"})
s.subMgr.notify()
self.response(getOKMsg(), self.js.getPlexHeaders())
for playerid in self.js.getPlayerIds():
self.js.jsonrpc("Player.Seek", {"playerid":playerid, "value":"bigbackward"})
self.subMgr.notify()
elif request_path == "player/navigation/moveUp":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Up")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Up")
elif request_path == "player/navigation/moveDown":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Down")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Down")
elif request_path == "player/navigation/moveLeft":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Left")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Left")
elif request_path == "player/navigation/moveRight":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Right")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Right")
elif request_path == "player/navigation/select":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Select")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Select")
elif request_path == "player/navigation/home":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Home")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Home")
elif request_path == "player/navigation/back":
s.response(getOKMsg(), s.js.getPlexHeaders())
s.js.jsonrpc("Input.Back")
self.response(getOKMsg(), self.js.getPlexHeaders())
self.js.jsonrpc("Input.Back")
# elif 'player/mirror/details' in request_path:
# # Detailed e.g. Movie information page was opened
# # CURRENTLY NOT POSSIBLE DUE TO KODI RESTRICTIONS
# plexId = params.get('key', params.get('ratingKey'))
# if plexId is None:
# self.logMsg('Could not get plex id from params: %s'
# % params, -1)
# return
# if 'library/metadata' in plexId:
# plexId = plexId.rsplit('/', 1)[1]
# with embydb.GetEmbyDB() as emby_db:
# emby_dbitem = emby_db.getItem_byId(plexId)
# try:
# kodiid = emby_dbitem[0]
# mediatype = emby_dbitem[4]
# except TypeError:
# self.log("No Plex id returned for plexId %s" % plexId, 0)
# return
# getDBfromPlexType(mediatype)
except:
s.logMsg('Error encountered. Traceback:', -1)
s.logMsg(traceback.print_exc(), -1)
self.logMsg('Error encountered. Traceback:', -1)
self.logMsg(traceback.print_exc(), -1)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):