Cleanup userclient.py
This commit is contained in:
parent
044f1a5550
commit
3ff15ba772
1 changed files with 48 additions and 67 deletions
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import xbmc
|
import xbmc
|
||||||
|
@ -9,7 +9,8 @@ import xbmcgui
|
||||||
import xbmcaddon
|
import xbmcaddon
|
||||||
import xbmcvfs
|
import xbmcvfs
|
||||||
|
|
||||||
import utils
|
from utils import window, settings, language as lang, ThreadMethods, \
|
||||||
|
tryDecode, ThreadMethodsAdditionalSuspend
|
||||||
import downloadutils
|
import downloadutils
|
||||||
|
|
||||||
import PlexAPI
|
import PlexAPI
|
||||||
|
@ -17,10 +18,13 @@ from PlexFunctions import GetMachineIdentifier
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
log = logging.getLogger("PLEX."+__name__)
|
||||||
|
|
||||||
@utils.logging
|
###############################################################################
|
||||||
@utils.ThreadMethodsAdditionalSuspend('suspend_Userclient')
|
|
||||||
@utils.ThreadMethods
|
|
||||||
|
@ThreadMethodsAdditionalSuspend('suspend_Userclient')
|
||||||
|
@ThreadMethods
|
||||||
class UserClient(threading.Thread):
|
class UserClient(threading.Thread):
|
||||||
|
|
||||||
# Borg - multiple instances, shared state
|
# Borg - multiple instances, shared state
|
||||||
|
@ -50,22 +54,16 @@ class UserClient(threading.Thread):
|
||||||
"""
|
"""
|
||||||
Returns username as unicode
|
Returns username as unicode
|
||||||
"""
|
"""
|
||||||
|
username = settings('username')
|
||||||
username = utils.settings('username')
|
|
||||||
|
|
||||||
if not username:
|
if not username:
|
||||||
self.logMsg("No username saved, trying to get Plex username", 0)
|
log.debug("No username saved, trying to get Plex username")
|
||||||
username = utils.settings('plexLogin')
|
username = settings('plexLogin')
|
||||||
if not username:
|
if not username:
|
||||||
self.logMsg("Also no Plex username found", 0)
|
log.debug("Also no Plex username found")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
return username
|
return username
|
||||||
|
|
||||||
def getServer(self, prefix=True):
|
def getServer(self, prefix=True):
|
||||||
|
|
||||||
settings = utils.settings
|
|
||||||
|
|
||||||
# Original host
|
# Original host
|
||||||
self.servername = settings('plex_servername')
|
self.servername = settings('plex_servername')
|
||||||
HTTPS = settings('https') == "true"
|
HTTPS = settings('https') == "true"
|
||||||
|
@ -76,7 +74,7 @@ class UserClient(threading.Thread):
|
||||||
server = host + ":" + port
|
server = host + ":" + port
|
||||||
|
|
||||||
if not host:
|
if not host:
|
||||||
self.logMsg("No server information saved.", 2)
|
log.debug("No server information saved.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# If https is true
|
# If https is true
|
||||||
|
@ -91,25 +89,25 @@ class UserClient(threading.Thread):
|
||||||
if self.machineIdentifier is None:
|
if self.machineIdentifier is None:
|
||||||
self.machineIdentifier = ''
|
self.machineIdentifier = ''
|
||||||
settings('plex_machineIdentifier', value=self.machineIdentifier)
|
settings('plex_machineIdentifier', value=self.machineIdentifier)
|
||||||
self.logMsg('Returning active server: %s' % server)
|
log.info('Returning active server: %s' % server)
|
||||||
return server
|
return server
|
||||||
|
|
||||||
def getSSLverify(self):
|
def getSSLverify(self):
|
||||||
# Verify host certificate
|
# Verify host certificate
|
||||||
return None if utils.settings('sslverify') == 'true' else False
|
return None if settings('sslverify') == 'true' else False
|
||||||
|
|
||||||
def getSSL(self):
|
def getSSL(self):
|
||||||
# Client side certificate
|
# Client side certificate
|
||||||
return None if utils.settings('sslcert') == 'None' \
|
return None if settings('sslcert') == 'None' \
|
||||||
else utils.settings('sslcert')
|
else settings('sslcert')
|
||||||
|
|
||||||
def setUserPref(self):
|
def setUserPref(self):
|
||||||
self.logMsg('Setting user preferences', 0)
|
log.info('Setting user preferences')
|
||||||
# Only try to get user avatar if there is a token
|
# Only try to get user avatar if there is a token
|
||||||
if self.currToken:
|
if self.currToken:
|
||||||
url = PlexAPI.PlexAPI().GetUserArtworkURL(self.currUser)
|
url = PlexAPI.PlexAPI().GetUserArtworkURL(self.currUser)
|
||||||
if url:
|
if url:
|
||||||
utils.window('PlexUserImage', value=url)
|
window('PlexUserImage', value=url)
|
||||||
# Set resume point max
|
# Set resume point max
|
||||||
# url = "{server}/emby/System/Configuration?format=json"
|
# url = "{server}/emby/System/Configuration?format=json"
|
||||||
# result = doUtils.downloadUrl(url)
|
# result = doUtils.downloadUrl(url)
|
||||||
|
@ -118,15 +116,12 @@ class UserClient(threading.Thread):
|
||||||
# Plex: always return True for now
|
# Plex: always return True for now
|
||||||
return True
|
return True
|
||||||
# hasAccess is verified in service.py
|
# hasAccess is verified in service.py
|
||||||
log = self.logMsg
|
|
||||||
window = utils.window
|
|
||||||
|
|
||||||
url = "{server}/emby/Users?format=json"
|
url = "{server}/emby/Users?format=json"
|
||||||
result = self.doUtils.downloadUrl(url)
|
result = self.doUtils.downloadUrl(url)
|
||||||
|
|
||||||
if result is False:
|
if result is False:
|
||||||
# Access is restricted, set in downloadutils.py via exception
|
# Access is restricted, set in downloadutils.py via exception
|
||||||
log("Access is restricted.", 1)
|
log.info("Access is restricted.")
|
||||||
self.HasAccess = False
|
self.HasAccess = False
|
||||||
|
|
||||||
elif window('plex_online') != "true":
|
elif window('plex_online') != "true":
|
||||||
|
@ -134,16 +129,14 @@ class UserClient(threading.Thread):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif window('plex_serverStatus') == "restricted":
|
elif window('plex_serverStatus') == "restricted":
|
||||||
log("Access is granted.", 1)
|
log.info("Access is granted.")
|
||||||
self.HasAccess = True
|
self.HasAccess = True
|
||||||
window('plex_serverStatus', clear=True)
|
window('plex_serverStatus', clear=True)
|
||||||
xbmcgui.Dialog().notification(self.addonName,
|
xbmcgui.Dialog().notification(self.addonName,
|
||||||
utils.language(33007))
|
lang(33007))
|
||||||
|
|
||||||
def loadCurrUser(self, username, userId, usertoken, authenticated=False):
|
def loadCurrUser(self, username, userId, usertoken, authenticated=False):
|
||||||
self.logMsg('Loading current user', 0)
|
log.info('Loading current user')
|
||||||
window = utils.window
|
|
||||||
settings = utils.settings
|
|
||||||
doUtils = self.doUtils
|
doUtils = self.doUtils
|
||||||
|
|
||||||
self.currUserId = userId
|
self.currUserId = userId
|
||||||
|
@ -153,18 +146,18 @@ class UserClient(threading.Thread):
|
||||||
self.sslcert = self.getSSL()
|
self.sslcert = self.getSSL()
|
||||||
|
|
||||||
if authenticated is False:
|
if authenticated is False:
|
||||||
self.logMsg('Testing validity of current token', 0)
|
log.info('Testing validity of current token')
|
||||||
res = PlexAPI.PlexAPI().CheckConnection(self.currServer,
|
res = PlexAPI.PlexAPI().CheckConnection(self.currServer,
|
||||||
token=self.currToken,
|
token=self.currToken,
|
||||||
verifySSL=self.ssl)
|
verifySSL=self.ssl)
|
||||||
if res is False:
|
if res is False:
|
||||||
self.logMsg('Answer from PMS is not as expected. Retrying', -1)
|
log.error('Answer from PMS is not as expected. Retrying')
|
||||||
return False
|
return False
|
||||||
elif res == 401:
|
elif res == 401:
|
||||||
self.logMsg('Token is no longer valid', -1)
|
log.warn('Token is no longer valid')
|
||||||
return False
|
return False
|
||||||
elif res >= 400:
|
elif res >= 400:
|
||||||
self.logMsg('Answer from PMS is not as expected. Retrying', -1)
|
log.error('Answer from PMS is not as expected. Retrying')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Set to windows property
|
# Set to windows property
|
||||||
|
@ -182,11 +175,11 @@ class UserClient(threading.Thread):
|
||||||
window('plex_serverowned', value=settings('plex_serverowned'))
|
window('plex_serverowned', value=settings('plex_serverowned'))
|
||||||
|
|
||||||
window('useDirectPaths', value='true'
|
window('useDirectPaths', value='true'
|
||||||
if utils.settings('useDirectPaths') == "1" else 'false')
|
if settings('useDirectPaths') == "1" else 'false')
|
||||||
window('replaceSMB', value='true'
|
window('replaceSMB', value='true'
|
||||||
if utils.settings('replaceSMB') == "true" else 'false')
|
if settings('replaceSMB') == "true" else 'false')
|
||||||
window('remapSMB', value='true'
|
window('remapSMB', value='true'
|
||||||
if utils.settings('remapSMB') == "true" else 'false')
|
if settings('remapSMB') == "true" else 'false')
|
||||||
if window('remapSMB') == 'true':
|
if window('remapSMB') == 'true':
|
||||||
items = ('movie', 'tv', 'music')
|
items = ('movie', 'tv', 'music')
|
||||||
for item in items:
|
for item in items:
|
||||||
|
@ -213,7 +206,7 @@ class UserClient(threading.Thread):
|
||||||
settings('accessToken', value=usertoken)
|
settings('accessToken', value=usertoken)
|
||||||
|
|
||||||
dialog = xbmcgui.Dialog()
|
dialog = xbmcgui.Dialog()
|
||||||
if utils.settings('connectMsg') == "true":
|
if settings('connectMsg') == "true":
|
||||||
if username:
|
if username:
|
||||||
dialog.notification(
|
dialog.notification(
|
||||||
heading=self.addonName,
|
heading=self.addonName,
|
||||||
|
@ -227,16 +220,12 @@ class UserClient(threading.Thread):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def authenticate(self):
|
def authenticate(self):
|
||||||
log = self.logMsg
|
log.info('Authenticating user')
|
||||||
log('Authenticating user', 1)
|
|
||||||
lang = utils.language
|
|
||||||
window = utils.window
|
|
||||||
settings = utils.settings
|
|
||||||
dialog = xbmcgui.Dialog()
|
dialog = xbmcgui.Dialog()
|
||||||
|
|
||||||
# Give attempts at entering password / selecting user
|
# Give attempts at entering password / selecting user
|
||||||
if self.retry >= 2:
|
if self.retry >= 2:
|
||||||
log("Too many retries to login.", -1)
|
log.error("Too many retries to login.")
|
||||||
window('plex_serverStatus', value="Stop")
|
window('plex_serverStatus', value="Stop")
|
||||||
dialog.ok(lang(33001),
|
dialog.ok(lang(33001),
|
||||||
lang(39023))
|
lang(39023))
|
||||||
|
@ -245,19 +234,19 @@ class UserClient(threading.Thread):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Get /profile/addon_data
|
# Get /profile/addon_data
|
||||||
addondir = utils.tryDecode(xbmc.translatePath(
|
addondir = tryDecode(xbmc.translatePath(
|
||||||
self.addon.getAddonInfo('profile')))
|
self.addon.getAddonInfo('profile')))
|
||||||
hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir)
|
hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir)
|
||||||
|
|
||||||
# If there's no settings.xml
|
# If there's no settings.xml
|
||||||
if not hasSettings:
|
if not hasSettings:
|
||||||
log("Error, no settings.xml found.", -1)
|
log.error("Error, no settings.xml found.")
|
||||||
self.auth = False
|
self.auth = False
|
||||||
return False
|
return False
|
||||||
server = self.getServer()
|
server = self.getServer()
|
||||||
# If there is no server we can connect to
|
# If there is no server we can connect to
|
||||||
if not server:
|
if not server:
|
||||||
log("Missing server information.", 0)
|
log.info("Missing server information.")
|
||||||
self.auth = False
|
self.auth = False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -268,7 +257,7 @@ class UserClient(threading.Thread):
|
||||||
enforceLogin = settings('enforceUserLogin')
|
enforceLogin = settings('enforceUserLogin')
|
||||||
# Found a user in the settings, try to authenticate
|
# Found a user in the settings, try to authenticate
|
||||||
if username and enforceLogin == 'false':
|
if username and enforceLogin == 'false':
|
||||||
log('Trying to authenticate with old settings', 0)
|
log.info('Trying to authenticate with old settings')
|
||||||
if self.loadCurrUser(username,
|
if self.loadCurrUser(username,
|
||||||
userId,
|
userId,
|
||||||
usertoken,
|
usertoken,
|
||||||
|
@ -277,7 +266,7 @@ class UserClient(threading.Thread):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
# Failed to use the settings - delete them!
|
# Failed to use the settings - delete them!
|
||||||
log("Failed to use the settings credentials. Deleting them", 1)
|
log.info("Failed to use settings credentials. Deleting them")
|
||||||
settings('username', value='')
|
settings('username', value='')
|
||||||
settings('userid', value='')
|
settings('userid', value='')
|
||||||
settings('accessToken', value='')
|
settings('accessToken', value='')
|
||||||
|
@ -287,7 +276,7 @@ class UserClient(threading.Thread):
|
||||||
# Could not use settings - try to get Plex user list from plex.tv
|
# Could not use settings - try to get Plex user list from plex.tv
|
||||||
plextoken = settings('plexToken')
|
plextoken = settings('plexToken')
|
||||||
if plextoken:
|
if plextoken:
|
||||||
log("Trying to connect to plex.tv to get a user list", 0)
|
log.info("Trying to connect to plex.tv to get a user list")
|
||||||
userInfo = plx.ChoosePlexHomeUser(plextoken)
|
userInfo = plx.ChoosePlexHomeUser(plextoken)
|
||||||
if userInfo is False:
|
if userInfo is False:
|
||||||
# FAILURE: Something went wrong, try again
|
# FAILURE: Something went wrong, try again
|
||||||
|
@ -298,7 +287,7 @@ class UserClient(threading.Thread):
|
||||||
userId = userInfo['userid']
|
userId = userInfo['userid']
|
||||||
usertoken = userInfo['token']
|
usertoken = userInfo['token']
|
||||||
else:
|
else:
|
||||||
log("Trying to authenticate without a token", 0)
|
log.info("Trying to authenticate without a token")
|
||||||
username = ''
|
username = ''
|
||||||
userId = ''
|
userId = ''
|
||||||
usertoken = ''
|
usertoken = ''
|
||||||
|
@ -313,13 +302,9 @@ class UserClient(threading.Thread):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def resetClient(self):
|
def resetClient(self):
|
||||||
self.logMsg("Reset UserClient authentication.", 1)
|
log.info("Reset UserClient authentication.")
|
||||||
|
|
||||||
self.doUtils.stopSession()
|
self.doUtils.stopSession()
|
||||||
|
|
||||||
settings = utils.settings
|
|
||||||
window = utils.window
|
|
||||||
|
|
||||||
window('plex_authenticated', clear=True)
|
window('plex_authenticated', clear=True)
|
||||||
window('pms_token', clear=True)
|
window('pms_token', clear=True)
|
||||||
window('plex_token', clear=True)
|
window('plex_token', clear=True)
|
||||||
|
@ -346,10 +331,7 @@ class UserClient(threading.Thread):
|
||||||
self.retry = 0
|
self.retry = 0
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
log = self.logMsg
|
log.info("----===## Starting UserClient ##===----")
|
||||||
window = utils.window
|
|
||||||
|
|
||||||
log("----===## Starting UserClient ##===----", 0)
|
|
||||||
while not self.threadStopped():
|
while not self.threadStopped():
|
||||||
while self.threadSuspended():
|
while self.threadSuspended():
|
||||||
if self.threadStopped():
|
if self.threadStopped():
|
||||||
|
@ -381,10 +363,9 @@ class UserClient(threading.Thread):
|
||||||
self.auth = False
|
self.auth = False
|
||||||
if self.authenticate():
|
if self.authenticate():
|
||||||
# Successfully authenticated and loaded a user
|
# Successfully authenticated and loaded a user
|
||||||
log("Successfully authenticated!", 1)
|
log.info("Successfully authenticated!")
|
||||||
log("Current user: %s" % self.currUser, 1)
|
log.info("Current user: %s" % self.currUser)
|
||||||
log("Current userId: %s" % self.currUserId, 1)
|
log.info("Current userId: %s" % self.currUserId)
|
||||||
log("Current accessToken: xxxx", 1)
|
|
||||||
self.retry = 0
|
self.retry = 0
|
||||||
window('suspend_LibraryThread', clear=True)
|
window('suspend_LibraryThread', clear=True)
|
||||||
window('plex_serverStatus', clear=True)
|
window('plex_serverStatus', clear=True)
|
||||||
|
@ -397,11 +378,11 @@ class UserClient(threading.Thread):
|
||||||
# Or retried too many times
|
# Or retried too many times
|
||||||
if server and status != "Stop":
|
if server and status != "Stop":
|
||||||
# Only if there's information found to login
|
# Only if there's information found to login
|
||||||
log("Server found: %s" % server, 2)
|
log.info("Server found: %s" % server)
|
||||||
self.auth = True
|
self.auth = True
|
||||||
|
|
||||||
# Minimize CPU load
|
# Minimize CPU load
|
||||||
xbmc.sleep(100)
|
xbmc.sleep(100)
|
||||||
|
|
||||||
self.doUtils.stopSession()
|
self.doUtils.stopSession()
|
||||||
log("##===---- UserClient Stopped ----===##", 0)
|
log.info("##===---- UserClient Stopped ----===##")
|
||||||
|
|
Loading…
Reference in a new issue