Reconnect Plex Cloud without keeping the server awake

This commit is contained in:
JonnyWong16 2018-02-13 10:49:11 -08:00
parent df016243dd
commit e1dca1509a
6 changed files with 77 additions and 28 deletions

View file

@ -397,8 +397,12 @@ def initialize_scheduler():
schedule_job(libraries.refresh_libraries, 'Refresh libraries list', schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
hours=library_hours, minutes=0, seconds=0) hours=library_hours, minutes=0, seconds=0)
schedule_job(activity_pinger.check_server_response, 'Check for server response', if plexpy.CONFIG.PMS_IS_CLOUD:
hours=0, minutes=0, seconds=0) schedule_job(activity_pinger.check_cloud_status, 'Check for server response',
hours=0, minutes=0, seconds=0)
else:
schedule_job(activity_pinger.check_server_response, 'Check for server response',
hours=0, minutes=0, seconds=0)
else: else:
# Cancel all jobs # Cancel all jobs
@ -415,12 +419,15 @@ def initialize_scheduler():
schedule_job(libraries.refresh_libraries, 'Refresh libraries list', schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
hours=0, minutes=0, seconds=0) hours=0, minutes=0, seconds=0)
# Schedule job to reconnect websocket if plexpy.CONFIG.PMS_IS_CLOUD:
response_seconds = CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS * CONFIG.WEBSOCKET_CONNECTION_TIMEOUT # Schedule job to check for cloud server status
response_seconds = 60 if response_seconds < 60 else response_seconds schedule_job(activity_pinger.check_cloud_status, 'Check for server response',
hours=0, minutes=0, seconds=60)
schedule_job(activity_pinger.check_server_response, 'Check for server response', else:
hours=0, minutes=0, seconds=response_seconds) # Schedule job to reconnect websocket
schedule_job(activity_pinger.check_server_response, 'Check for server response',
hours=0, minutes=0, seconds=60)
# Start scheduler # Start scheduler
if start_jobs and len(SCHED.get_jobs()): if start_jobs and len(SCHED.get_jobs()):

View file

@ -326,3 +326,16 @@ def check_server_updates():
else: else:
logger.info(u"Tautulli Monitor :: No PMS update available.") logger.info(u"Tautulli Monitor :: No PMS update available.")
def check_cloud_status():
logger.info(u"Tautulli Monitor :: Checking for Plex Cloud server status...")
plex_tv = plextv.PlexTV()
cloud_status = plex_tv.get_cloud_server_status()
if cloud_status:
logger.info(u"Tautulli Monitor :: Plex Cloud server is active.")
check_server_response()
else:
logger.info(u"Tautulli Monitor :: Plex Cloud server is sleeping.")

View file

@ -39,11 +39,17 @@ class HTTPHandler(object):
else: else:
self.urls = urls self.urls = urls
self.headers = {'X-Plex-Device-Name': 'Tautulli',
'X-Plex-Product': 'Tautulli',
'X-Plex-Version': plexpy.common.VERSION_NUMBER,
'X-Plex-Platform': plexpy.common.PLATFORM,
'X-Plex-Platform-Version': plexpy.common.PLATFORM_VERSION,
'X-Plex-Client-Identifier': plexpy.CONFIG.PMS_UUID,
}
self.token = token self.token = token
if self.token: if self.token:
self.headers = {'X-Plex-Token': self.token} self.headers['X-Plex-Token'] = self.token
else:
self.headers = {}
self.timeout = timeout self.timeout = timeout
self.ssl_verify = ssl_verify self.ssl_verify = ssl_verify

View file

@ -144,14 +144,7 @@ class PlexTV(object):
uri = '/users/sign_in.xml' uri = '/users/sign_in.xml'
base64string = base64.b64encode(('%s:%s' % (self.username, self.password)).encode('utf-8')) base64string = base64.b64encode(('%s:%s' % (self.username, self.password)).encode('utf-8'))
headers = {'Content-Type': 'application/xml; charset=utf-8', headers = {'Content-Type': 'application/xml; charset=utf-8',
'X-Plex-Device-Name': 'Tautulli', 'Authorization': 'Basic %s' % base64string}
'X-Plex-Product': 'Tautulli',
'X-Plex-Version': plexpy.common.VERSION_NUMBER,
'X-Plex-Platform': plexpy.common.PLATFORM,
'X-Plex-Platform-Version': plexpy.common.PLATFORM_VERSION,
'X-Plex-Client-Identifier': plexpy.CONFIG.PMS_UUID,
'Authorization': 'Basic %s' % base64string
}
request = self.request_handler.make_request(uri=uri, request = self.request_handler.make_request(uri=uri,
request_type='POST', request_type='POST',
@ -318,6 +311,14 @@ class PlexTV(object):
return request return request
def cloud_server_status(self, output_format=''):
uri = '/api/v2/cloud_server'
request = self.request_handler.make_request(uri=uri,
request_type='GET',
output_format=output_format)
return request
def get_full_users_list(self): def get_full_users_list(self):
friends_list = self.get_plextv_friends(output_format='xml') friends_list = self.get_plextv_friends(output_format='xml')
own_account = self.get_plextv_user_details(output_format='xml') own_account = self.get_plextv_user_details(output_format='xml')
@ -753,3 +754,21 @@ class PlexTV(object):
devices_list.append(device) devices_list.append(device)
return devices_list return devices_list
def get_cloud_server_status(self):
cloud_status = self.cloud_server_status(output_format='xml')
try:
status_info = cloud_status.getElementsByTagName('info')
except Exception as e:
logger.warn(u"Tautulli PlexTV :: Unable to parse XML for get_cloud_server_status: %s." % e)
return False
for info in status_info:
servers = info.getElementsByTagName('server')
for s in servers:
if helpers.get_xml_attr(s, 'address') == plexpy.CONFIG.PMS_IP:
if helpers.get_xml_attr(info, 'running') == '1':
return True
else:
return False

View file

@ -61,7 +61,7 @@ class PmsConnect(object):
self.url = plexpy.CONFIG.PMS_URL self.url = plexpy.CONFIG.PMS_URL
elif not self.url: elif not self.url:
self.url = 'http://{hostname}:{port}'.format(hostname=plexpy.CONFIG.PMS_IP, self.url = 'http://{hostname}:{port}'.format(hostname=plexpy.CONFIG.PMS_IP,
port=plexpy.CONFIG.PMS_PORT) port=plexpy.CONFIG.PMS_PORT)
self.timeout = plexpy.CONFIG.PMS_TIMEOUT self.timeout = plexpy.CONFIG.PMS_TIMEOUT
if not self.token: if not self.token:

View file

@ -41,23 +41,23 @@ def start_thread():
def on_connect(): def on_connect():
plexpy.initialize_scheduler()
if not plexpy.PLEX_SERVER_UP: if not plexpy.PLEX_SERVER_UP:
logger.info(u"Tautulli WebSocket :: The Plex Media Server is back up.") logger.info(u"Tautulli WebSocket :: The Plex Media Server is back up.")
plexpy.NOTIFY_QUEUE.put({'notify_action': 'on_intup'}) plexpy.NOTIFY_QUEUE.put({'notify_action': 'on_intup'})
plexpy.PLEX_SERVER_UP = True plexpy.PLEX_SERVER_UP = True
def on_disconnect(ws_exception=False):
activity_processor.ActivityProcessor().set_temp_stopped()
plexpy.initialize_scheduler() plexpy.initialize_scheduler()
def on_disconnect(ws_exception=False):
if not ws_exception and plexpy.PLEX_SERVER_UP: if not ws_exception and plexpy.PLEX_SERVER_UP:
logger.info(u"Tautulli WebSocket :: Unable to get an internal response from the server, Plex server is down.") logger.info(u"Tautulli WebSocket :: Unable to get a response from the server, Plex server is down.")
plexpy.NOTIFY_QUEUE.put({'notify_action': 'on_intdown'}) plexpy.NOTIFY_QUEUE.put({'notify_action': 'on_intdown'})
plexpy.PLEX_SERVER_UP = False plexpy.PLEX_SERVER_UP = False
activity_processor.ActivityProcessor().set_temp_stopped()
plexpy.initialize_scheduler()
def reconnect(): def reconnect():
global ws_reconnect global ws_reconnect
@ -89,7 +89,7 @@ def run():
ws_exception = False ws_exception = False
# Try an open the websocket connection # Try an open the websocket connection
while not plexpy.WS_CONNECTED and reconnects <= plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS: while not plexpy.WS_CONNECTED and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
reconnects += 1 reconnects += 1
# Sleep 5 between connection attempts # Sleep 5 between connection attempts
@ -116,7 +116,11 @@ def run():
reconnects = 0 reconnects = 0
except websocket.WebSocketConnectionClosedException: except websocket.WebSocketConnectionClosedException:
if reconnects <= plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS: if plexpy.CONFIG.PMS_IS_CLOUD:
logger.warn(u"Tautulli WebSocket :: Connection has closed.")
activity_pinger.check_cloud_status()
if not plexpy.CONFIG.PMS_IS_CLOUD and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
reconnects += 1 reconnects += 1
# Sleep 5 between connection attempts # Sleep 5 between connection attempts