From f22369a14acd05602c0399a6a70519fda3e6e89c Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Tue, 26 Apr 2016 17:02:29 +0200 Subject: [PATCH] Use Plex Companion for Playstate Reporting - Should fix Unknown answer from PMS https://192.168.178.5:32400/:/timeline?SubtitleStreamIndex=&AudioStreamIndex=1&ratingKey=4338&state=playing&time=424000&duration=8286000000 with status code 400. Message: NOTICE: PlexKodiConnect DownloadUtils -> Bad Request - Merge fixes to player.py --- resources/lib/PlexCompanion.py | 91 +++++++++++++------------- resources/lib/player.py | 34 ++++------ resources/lib/plexbmchelper/plexgdm.py | 19 +++--- service.py | 5 +- 4 files changed, 72 insertions(+), 77 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index 70d83867..b1a0f179 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -22,17 +22,17 @@ class PlexCompanion(threading.Thread): self.client = plexgdm.plexgdm() self.client.clientDetails(self.settings) self.logMsg("Registration string is: %s " - % self.client.getClientDetails(), 1) + % self.client.getClientDetails(), 2) threading.Thread.__init__(self) def run(self): + httpd = False # Cache for quicker while loops log = self.logMsg client = self.client threadStopped = self.threadStopped threadSuspended = self.threadSuspended - start_count = 0 # Start up instances requestMgr = httppersist.RequestMgr() @@ -40,33 +40,35 @@ class PlexCompanion(threading.Thread): subscriptionManager = subscribers.SubscriptionManager( jsonClass, requestMgr) - # Start up httpd - while True: - try: - httpd = listener.ThreadedHTTPServer( - client, - subscriptionManager, - jsonClass, - self.settings, - ('', self.settings['myport']), - listener.MyHandler) - httpd.timeout = 0.95 - break - except: - log("Unable to start PlexCompanion. Traceback:", -1) - log(traceback.print_exc(), -1) + if utils.settings('plexCompanion') == 'true': + self.logMsg('User activated Plex Companion', 0) + # Start up httpd + start_count = 0 + while True: + try: + httpd = listener.ThreadedHTTPServer( + client, + subscriptionManager, + jsonClass, + self.settings, + ('', self.settings['myport']), + listener.MyHandler) + httpd.timeout = 0.95 + break + except: + log("Unable to start PlexCompanion. Traceback:", -1) + log(traceback.print_exc(), -1) - xbmc.sleep(3000) + xbmc.sleep(3000) - if start_count == 3: - log("Error: Unable to start web helper.", -1) - httpd = False - break + if start_count == 3: + log("Error: Unable to start web helper.", -1) + httpd = False + break - start_count += 1 - - if not httpd: - return + start_count += 1 + else: + self.logMsg('User deactivated Plex Companion', 0) client.start_all() @@ -80,18 +82,18 @@ class PlexCompanion(threading.Thread): break xbmc.sleep(1000) try: + if httpd: + httpd.handle_request() + message_count += 1 - httpd.handle_request() - message_count += 1 - - if message_count > 100: - if client.check_client_registration(): - log("Client is still registered", 1) - else: - log("Client is no longer registered", 1) - log("Plex Companion still running on port %s" - % self.settings['myport'], 1) - message_count = 0 + if message_count > 100: + if client.check_client_registration(): + log("Client is still registered", 1) + else: + log("Client is no longer registered", 1) + log("Plex Companion still running on port %s" + % self.settings['myport'], 1) + message_count = 0 # Get and set servers subscriptionManager.serverlist = client.getServerList() @@ -99,15 +101,16 @@ class PlexCompanion(threading.Thread): subscriptionManager.notify() xbmc.sleep(50) except: - log("Error in loop, continuing anyway", 0) + log("Error in loop, continuing anyway. Traceback:", 1) log(traceback.format_exc(), 1) xbmc.sleep(50) client.stop_all() - try: - httpd.socket.shutdown(socket.SHUT_RDWR) - except: - pass - finally: - httpd.socket.close() + if httpd: + try: + httpd.socket.shutdown(socket.SHUT_RDWR) + except: + pass + finally: + httpd.socket.close() log("----===## Plex Companion stopped ##===----", 0) diff --git a/resources/lib/player.py b/resources/lib/player.py index 91d9efb6..ec2f788f 100644 --- a/resources/lib/player.py +++ b/resources/lib/player.py @@ -36,14 +36,6 @@ class Player(xbmc.Player): self.logMsg("Starting playback monitor.", 2) - # Should we start notification or is this done by Plex Companion? - self.doNotify = False if (utils.settings('plexCompanion') == 'true') \ - else True - if self.doNotify: - self.logMsg("PMS notifications not done by Plex Companion", 2) - else: - self.logMsg("PMS notifications done by Plex Companion", 2) - def GetPlayStats(self): return self.playStats @@ -74,7 +66,8 @@ class Player(xbmc.Player): else: count += 1 if not currentFile: - log('Error getting a currently playing file; abort reporting', -1) + self.logMsg('Error getting a currently playing file; abort ' + 'reporting', -1) return # Save currentFile for cleanup later and for references @@ -88,12 +81,13 @@ class Player(xbmc.Player): itemId = window("emby_%s.itemid" % currentFile) # try 20 times or about 10 seconds if count == 20: - log("Could not find itemId, cancelling playback report...", -1) + self.logMsg("Could not find itemId, cancelling playback " + "report!", -1) return count += 1 - log("ONPLAYBACK_STARTED: %s itemid: %s" - % (currentFile.decode('utf-8'), itemId), 0) + self.logMsg("ONPLAYBACK_STARTED: %s itemid: %s" + % (currentFile.decode('utf-8'), itemId), 0) embyitem = "emby_%s" % currentFile runtime = window("%s.runtime" % embyitem) @@ -102,16 +96,16 @@ class Player(xbmc.Player): itemType = window("%s.type" % embyitem) window('emby_skipWatched%s' % itemId, value="true") - log("Playing itemtype is: %s" % itemType, 1) + self.logMsg("Playing itemtype is: %s" % itemType, 1) customseek = window('emby_customPlaylist.seektime') if (window('emby_customPlaylist') == "true" and customseek): # Start at, when using custom playlist (play to Kodi from webclient) - log("Seeking to: %s" % customseek, 1) - xbmcplayer.seekTime(int(customseek)) + self.logMsg("Seeking to: %s" % customseek, 1) + self.xbmcplayer.seekTime(int(customseek)) window('emby_customPlaylist.seektime', clear=True) - seekTime = xbmcplayer.getTime() + seekTime = self.xbmcplayer.getTime() # Get playback volume volume_query = { @@ -191,7 +185,8 @@ class Player(xbmc.Player): if mapping: # Set in playbackutils.py - log("Mapping for external subtitles index: %s" % mapping, 2) + self.logMsg("Mapping for external subtitles index: %s" + % mapping, 2) externalIndex = json.loads(mapping) if externalIndex.get(str(indexSubs)): @@ -257,9 +252,8 @@ class Player(xbmc.Player): self.playStats[playMethod] = 1''' def reportPlayback(self): - # Don't use if Plex Companion is enabled - if not self.doNotify: - return + # Done by Plex Companion + return self.logMsg("reportPlayback Called", 2) diff --git a/resources/lib/plexbmchelper/plexgdm.py b/resources/lib/plexbmchelper/plexgdm.py index 30450391..f62921f2 100644 --- a/resources/lib/plexbmchelper/plexgdm.py +++ b/resources/lib/plexbmchelper/plexgdm.py @@ -33,7 +33,7 @@ from xbmc import sleep import downloadutils from PlexFunctions import PMSHttpsEnabled -from utils import window, logging +from utils import window, logging, settings @logging @@ -60,7 +60,7 @@ class plexgdm: self.client_registered = False self.download = downloadutils.DownloadUtils().downloadUrl - def clientDetails(self, settings): + def clientDetails(self, options): self.client_data = ( "Content-Type: plex/media-player\r\n" "Resource-Identifier: %s\r\n" @@ -74,13 +74,13 @@ class plexgdm: "mirror,playqueues\r\n" "Device-Class: HTPC" ) % ( - settings['uuid'], - settings['client_name'], - settings['myport'], - settings['addonName'], - settings['version'] + options['uuid'], + options['client_name'], + options['myport'], + options['addonName'], + options['version'] ) - self.client_id = settings['uuid'] + self.client_id = options['uuid'] def getClientDetails(self): if not self.client_data: @@ -388,4 +388,5 @@ class plexgdm: def start_all(self, daemon=False): self.start_discovery(daemon) - self.start_registration(daemon) + if settings('plexCompanion') == 'true': + self.start_registration(daemon) diff --git a/service.py b/service.py index 62637e86..18be6ba5 100644 --- a/service.py +++ b/service.py @@ -63,8 +63,6 @@ class Service(): window('emby_kodiProfile', value=xbmc.translatePath("special://profile")) window('emby_pluginpath', value=utils.settings('useDirectPaths')) - self.runPlexCompanion = utils.settings('plexCompanion') - # Initial logging log("======== START %s ========" % self.addonName, 0) log("Platform: %s" % (self.clientInfo.getPlatform()), 0) @@ -206,8 +204,7 @@ class Service(): self.library_running = True library.start() # Start the Plex Companion thread - if not self.plexCompanion_running and \ - self.runPlexCompanion == "true": + if not self.plexCompanion_running: self.plexCompanion_running = True plexCompanion = PlexCompanion.PlexCompanion() plexCompanion.start()