2016-01-23 01:37:20 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
|
|
|
import utils
|
2016-04-03 01:46:23 +11:00
|
|
|
from plexbmchelper import listener, plexgdm, subscribers, functions, \
|
2016-04-05 18:57:30 +10:00
|
|
|
httppersist, settings
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2016-05-31 16:06:42 +10:00
|
|
|
@utils.ThreadMethodsAdditionalSuspend('plex_serverStatus')
|
2016-01-27 01:13:03 +11:00
|
|
|
@utils.ThreadMethods
|
2016-01-23 01:37:20 +11:00
|
|
|
class PlexCompanion(threading.Thread):
|
|
|
|
def __init__(self):
|
2016-04-03 01:46:23 +11:00
|
|
|
self.logMsg("----===## Starting PlexCompanion ##===----", 1)
|
2016-04-05 18:57:30 +10:00
|
|
|
self.settings = settings.getSettings()
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
# Start GDM for server/client discovery
|
2016-04-05 18:57:30 +10:00
|
|
|
self.client = plexgdm.plexgdm()
|
|
|
|
self.client.clientDetails(self.settings)
|
2016-01-23 01:37:20 +11:00
|
|
|
self.logMsg("Registration string is: %s "
|
2016-04-27 01:15:05 +10:00
|
|
|
% self.client.getClientDetails(), 2)
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
2016-04-27 01:15:05 +10:00
|
|
|
httpd = False
|
2016-04-03 01:46:23 +11:00
|
|
|
# Cache for quicker while loops
|
|
|
|
log = self.logMsg
|
|
|
|
client = self.client
|
|
|
|
threadStopped = self.threadStopped
|
|
|
|
threadSuspended = self.threadSuspended
|
|
|
|
|
|
|
|
# Start up instances
|
|
|
|
requestMgr = httppersist.RequestMgr()
|
2016-04-05 18:57:30 +10:00
|
|
|
jsonClass = functions.jsonClass(requestMgr, self.settings)
|
2016-04-03 01:46:23 +11:00
|
|
|
subscriptionManager = subscribers.SubscriptionManager(
|
|
|
|
jsonClass, requestMgr)
|
|
|
|
|
2016-04-27 01:15:05 +10:00
|
|
|
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)
|
2016-01-23 01:37:20 +11:00
|
|
|
|
2016-04-27 01:15:05 +10:00
|
|
|
xbmc.sleep(3000)
|
2016-04-27 01:10:11 +10:00
|
|
|
|
2016-04-27 01:15:05 +10:00
|
|
|
if start_count == 3:
|
|
|
|
log("Error: Unable to start web helper.", -1)
|
|
|
|
httpd = False
|
|
|
|
break
|
2016-01-23 01:37:20 +11:00
|
|
|
|
2016-04-27 01:15:05 +10:00
|
|
|
start_count += 1
|
|
|
|
else:
|
|
|
|
self.logMsg('User deactivated Plex Companion', 0)
|
2016-01-23 01:37:20 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
client.start_all()
|
|
|
|
|
2016-01-23 01:37:20 +11:00
|
|
|
message_count = 0
|
2016-04-03 01:46:23 +11:00
|
|
|
while not threadStopped():
|
2016-03-11 02:02:46 +11:00
|
|
|
# If we are not authorized, sleep
|
|
|
|
# Otherwise, we trigger a download which leads to a
|
|
|
|
# re-authorizations
|
2016-04-03 01:46:23 +11:00
|
|
|
while threadSuspended():
|
|
|
|
if threadStopped():
|
2016-03-24 02:07:09 +11:00
|
|
|
break
|
|
|
|
xbmc.sleep(1000)
|
2016-01-23 01:37:20 +11:00
|
|
|
try:
|
2016-04-27 01:15:05 +10:00
|
|
|
if httpd:
|
|
|
|
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
|
2016-01-23 01:37:20 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
# Get and set servers
|
|
|
|
subscriptionManager.serverlist = client.getServerList()
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
subscriptionManager.notify()
|
2016-03-08 18:43:12 +11:00
|
|
|
xbmc.sleep(50)
|
2016-01-23 01:37:20 +11:00
|
|
|
except:
|
2016-04-27 01:15:05 +10:00
|
|
|
log("Error in loop, continuing anyway. Traceback:", 1)
|
2016-04-03 01:46:23 +11:00
|
|
|
log(traceback.format_exc(), 1)
|
2016-03-08 18:43:12 +11:00
|
|
|
xbmc.sleep(50)
|
2016-01-23 01:37:20 +11:00
|
|
|
|
2016-04-03 01:46:23 +11:00
|
|
|
client.stop_all()
|
2016-04-27 01:15:05 +10:00
|
|
|
if httpd:
|
|
|
|
try:
|
|
|
|
httpd.socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
httpd.socket.close()
|
2016-04-03 01:46:23 +11:00
|
|
|
log("----===## Plex Companion stopped ##===----", 0)
|