Fixes to Plex Companion

- Fixes #224
This commit is contained in:
tomkat83 2017-02-19 17:07:42 +01:00
parent b4a44c9d7d
commit 861736d8d3
2 changed files with 68 additions and 60 deletions

View file

@ -35,7 +35,7 @@ class PlexCompanion(Thread):
# Start GDM for server/client discovery # Start GDM for server/client discovery
self.client = plexgdm.plexgdm() self.client = plexgdm.plexgdm()
self.client.clientDetails(self.settings) self.client.clientDetails(self.settings)
log.debug("Registration string is: %s " log.debug("Registration string is:\n%s"
% self.client.getClientDetails()) % self.client.getClientDetails())
# kodi player instance # kodi player instance
self.player = player.Player() self.player = player.Player()
@ -188,6 +188,7 @@ class PlexCompanion(Thread):
log.debug("Client is no longer registered. " log.debug("Client is no longer registered. "
"Plex Companion still running on port %s" "Plex Companion still running on port %s"
% self.settings['myport']) % self.settings['myport'])
client.register_as_client()
# Get and set servers # Get and set servers
if message_count % 30 == 0: if message_count % 30 == 0:
subscriptionManager.serverlist = client.getServerList() subscriptionManager.serverlist = client.getServerList()

View file

@ -57,23 +57,22 @@ class plexgdm:
self._discovery_is_running = False self._discovery_is_running = False
self._registration_is_running = False self._registration_is_running = False
self.discovery_complete = False
self.client_registered = False self.client_registered = False
self.download = downloadutils.DownloadUtils().downloadUrl self.download = downloadutils.DownloadUtils().downloadUrl
def clientDetails(self, options): def clientDetails(self, options):
self.client_data = ( self.client_data = (
"Content-Type: plex/media-player\r\n" "Content-Type: plex/media-player\n"
"Resource-Identifier: %s\r\n" "Resource-Identifier: %s\n"
"Name: %s\r\n" "Name: %s\n"
"Port: %s\r\n" "Port: %s\n"
"Product: %s\r\n" "Product: %s\n"
"Version: %s\r\n" "Version: %s\n"
"Protocol: plex\r\n" "Protocol: plex\n"
"Protocol-Version: 1\r\n" "Protocol-Version: 1\n"
"Protocol-Capabilities: timeline,playback,navigation," "Protocol-Capabilities: timeline,playback,navigation,"
"playqueues\r\n" "playqueues\n"
"Device-Class: HTPC" "Device-Class: HTPC\n"
) % ( ) % (
options['uuid'], options['uuid'],
options['client_name'], options['client_name'],
@ -86,10 +85,25 @@ class plexgdm:
def getClientDetails(self): def getClientDetails(self):
return self.client_data return self.client_data
def register_as_client(self):
"""
Registers PKC's Plex Companion to the PMS
"""
try:
log.debug("Sending registration data: HELLO %s\n%s"
% (self.client_header, self.client_data))
self.update_sock.sendto("HELLO %s\n%s"
% (self.client_header, self.client_data),
self.client_register_group)
log.debug('(Re-)registering PKC Plex Companion successful')
except:
log.error("Unable to send registration message")
def client_update(self): def client_update(self):
update_sock = socket.socket(socket.AF_INET, self.update_sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM, socket.SOCK_DGRAM,
socket.IPPROTO_UDP) socket.IPPROTO_UDP)
update_sock = self.update_sock
# Set socket reuse, may not work on all OSs. # Set socket reuse, may not work on all OSs.
try: try:
@ -129,16 +143,9 @@ class plexgdm:
self._multicast_address) + self._multicast_address) +
socket.inet_aton('0.0.0.0')) socket.inet_aton('0.0.0.0'))
update_sock.setblocking(0) update_sock.setblocking(0)
log.debug("Sending registration data: HELLO %s\r\n%s"
% (self.client_header, self.client_data))
# Send initial client registration # Send initial client registration
try: self.register_as_client()
update_sock.sendto("HELLO %s\r\n%s"
% (self.client_header, self.client_data),
self.client_register_group)
except:
log.error("Unable to send registration message")
# Now, listen format client discovery reguests and respond. # Now, listen format client discovery reguests and respond.
while self._registration_is_running: while self._registration_is_running:
@ -153,7 +160,7 @@ class plexgdm:
log.debug("Detected client discovery request from %s. " log.debug("Detected client discovery request from %s. "
" Replying" % str(addr)) " Replying" % str(addr))
try: try:
update_sock.sendto("HTTP/1.0 200 OK\r\n%s" update_sock.sendto("HTTP/1.0 200 OK\n%s"
% self.client_data, % self.client_data,
addr) addr)
except: except:
@ -165,10 +172,10 @@ class plexgdm:
log.info("Client Update loop stopped") log.info("Client Update loop stopped")
# When we are finished, then send a final goodbye message to # When we are finished, then send a final goodbye message to
# deregister cleanly. # deregister cleanly.
log.debug("Sending registration data: BYE %s\r\n%s" log.debug("Sending registration data: BYE %s\n%s"
% (self.client_header, self.client_data)) % (self.client_header, self.client_data))
try: try:
update_sock.sendto("BYE %s\r\n%s" update_sock.sendto("BYE %s\n%s"
% (self.client_header, self.client_data), % (self.client_header, self.client_data),
self.client_register_group) self.client_register_group)
except: except:
@ -176,41 +183,41 @@ class plexgdm:
self.client_registered = False self.client_registered = False
def check_client_registration(self): def check_client_registration(self):
if not self.client_registered:
log.debug('Client has not been marked as registered')
return False
if not self.server_list:
log.info("Server list is empty. Unable to check")
return False
for server in self.server_list:
if server['uuid'] == window('plex_machineIdentifier'):
media_server = server['server']
media_port = server['port']
scheme = server['protocol']
break
else:
log.info("Did not find our server!")
return False
if self.client_registered and self.discovery_complete: log.debug("Checking server [%s] on port [%s]"
if not self.server_list: % (media_server, media_port))
log.info("Server list is empty. Unable to check") xml = self.download(
return False '%s://%s:%s/clients' % (scheme, media_server, media_port))
try: try:
for server in self.server_list: xml[0].attrib
if server['uuid'] == window('plex_machineIdentifier'): except (TypeError, IndexError, AttributeError):
media_server = server['server'] log.error('Could not download clients for %s' % media_server)
media_port = server['port'] return False
scheme = server['protocol'] registered = False
break for client in xml:
else: if (client.attrib.get('machineIdentifier') ==
log.info("Did not find our server!") self.client_id):
return False registered = True
if registered:
log.debug("Checking server [%s] on port [%s]" return True
% (media_server, media_port)) else:
client_result = self.download( log.info("Client registration not found. "
'%s://%s:%s/clients' % (scheme, media_server, media_port)) "Client data is: %s" % xml)
registered = False
for client in client_result:
if (client.attrib.get('machineIdentifier') ==
self.client_id):
registered = True
if registered:
log.debug("Client registration successful. "
"Client data is: %s" % client_result)
return True
else:
log.info("Client registration not found. "
"Client data is: %s" % client_result)
except:
log.error("Unable to check status")
pass
return False return False
def getServerList(self): def getServerList(self):