2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
|
|
|
|
import utils
|
|
|
|
import downloadutils
|
|
|
|
|
2015-12-28 03:12:46 +11:00
|
|
|
import PlexAPI
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2016-01-27 01:13:03 +11:00
|
|
|
@utils.ThreadMethods
|
2015-12-25 07:07:00 +11:00
|
|
|
class UserClient(threading.Thread):
|
|
|
|
|
|
|
|
# Borg - multiple instances, shared state
|
2016-01-25 20:36:24 +11:00
|
|
|
__shared_state = {}
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
auth = True
|
|
|
|
retry = 0
|
|
|
|
|
|
|
|
currUser = None
|
|
|
|
currUserId = None
|
|
|
|
currServer = None
|
|
|
|
currToken = None
|
|
|
|
HasAccess = True
|
|
|
|
AdditionalUser = []
|
|
|
|
|
|
|
|
userSettings = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2016-01-25 20:36:24 +11:00
|
|
|
self.__dict__ = self.__shared_state
|
2015-12-25 07:07:00 +11:00
|
|
|
self.addon = xbmcaddon.Addon()
|
|
|
|
|
|
|
|
self.doUtils = downloadutils.DownloadUtils()
|
2016-01-13 03:23:55 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def getAdditionalUsers(self):
|
|
|
|
|
|
|
|
additionalUsers = utils.settings('additionalUsers')
|
|
|
|
|
|
|
|
if additionalUsers:
|
|
|
|
self.AdditionalUser = additionalUsers.split(',')
|
|
|
|
|
|
|
|
def getUsername(self):
|
|
|
|
|
|
|
|
username = utils.settings('username')
|
|
|
|
|
|
|
|
if not username:
|
|
|
|
self.logMsg("No username saved.", 2)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
return username
|
|
|
|
|
|
|
|
def getLogLevel(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
logLevel = int(utils.settings('logLevel'))
|
|
|
|
except ValueError:
|
|
|
|
logLevel = 0
|
|
|
|
|
|
|
|
return logLevel
|
|
|
|
|
|
|
|
def getUserId(self):
|
|
|
|
|
|
|
|
username = self.getUsername()
|
|
|
|
w_userId = utils.window('emby_userId%s' % username)
|
|
|
|
s_userId = utils.settings('userId%s' % username)
|
|
|
|
|
|
|
|
# Verify the window property
|
|
|
|
if w_userId:
|
|
|
|
if not s_userId:
|
|
|
|
# Save access token if it's missing from settings
|
|
|
|
utils.settings('userId%s' % username, value=w_userId)
|
|
|
|
self.logMsg(
|
|
|
|
"Returning userId from WINDOW for username: %s UserId: %s"
|
|
|
|
% (username, w_userId), 2)
|
|
|
|
return w_userId
|
|
|
|
# Verify the settings
|
|
|
|
elif s_userId:
|
|
|
|
self.logMsg(
|
|
|
|
"Returning userId from SETTINGS for username: %s userId: %s"
|
|
|
|
% (username, s_userId), 2)
|
|
|
|
return s_userId
|
|
|
|
# No userId found
|
|
|
|
else:
|
|
|
|
self.logMsg("No userId saved for username: %s." % username, 1)
|
|
|
|
|
|
|
|
def getServer(self, prefix=True):
|
|
|
|
|
|
|
|
alternate = utils.settings('altip') == "true"
|
|
|
|
if alternate:
|
|
|
|
# Alternate host
|
|
|
|
HTTPS = utils.settings('secondhttps') == "true"
|
|
|
|
host = utils.settings('secondipaddress')
|
|
|
|
port = utils.settings('secondport')
|
|
|
|
else:
|
|
|
|
# Original host
|
|
|
|
HTTPS = utils.settings('https') == "true"
|
|
|
|
host = utils.settings('ipaddress')
|
|
|
|
port = utils.settings('port')
|
|
|
|
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
if not host:
|
|
|
|
self.logMsg("No server information saved.", 2)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If https is true
|
|
|
|
if prefix and HTTPS:
|
|
|
|
server = "https://%s" % server
|
|
|
|
return server
|
|
|
|
# If https is false
|
|
|
|
elif prefix and not HTTPS:
|
|
|
|
server = "http://%s" % server
|
|
|
|
return server
|
|
|
|
# If only the host:port is required
|
|
|
|
elif not prefix:
|
|
|
|
return server
|
|
|
|
|
2015-12-27 22:23:59 +11:00
|
|
|
def getServerId(self):
|
2016-01-15 00:47:34 +11:00
|
|
|
serverId = utils.settings('plex_machineIdentifier')
|
2015-12-27 22:23:59 +11:00
|
|
|
return serverId
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def getToken(self):
|
|
|
|
|
|
|
|
username = self.getUsername()
|
|
|
|
w_token = utils.window('emby_accessToken%s' % username)
|
|
|
|
s_token = utils.settings('accessToken')
|
|
|
|
|
|
|
|
# Verify the window property
|
|
|
|
if w_token:
|
|
|
|
if not s_token:
|
|
|
|
# Save access token if it's missing from settings
|
|
|
|
utils.settings('accessToken', value=w_token)
|
2016-01-25 02:12:28 +11:00
|
|
|
self.logMsg("Returning accessToken from WINDOW for username: %s "
|
|
|
|
"accessToken: xxxxx" % username, 2)
|
2015-12-25 07:07:00 +11:00
|
|
|
return w_token
|
|
|
|
# Verify the settings
|
|
|
|
elif s_token:
|
2016-01-25 02:12:28 +11:00
|
|
|
self.logMsg("Returning accessToken from SETTINGS for username: %s "
|
|
|
|
"accessToken: xxxxx" % username, 2)
|
2015-12-25 07:07:00 +11:00
|
|
|
utils.window('emby_accessToken%s' % username, value=s_token)
|
|
|
|
return s_token
|
|
|
|
else:
|
|
|
|
self.logMsg("No token found.", 1)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def getSSLverify(self):
|
|
|
|
# Verify host certificate
|
|
|
|
s_sslverify = utils.settings('sslverify')
|
|
|
|
if utils.settings('altip') == "true":
|
|
|
|
s_sslverify = utils.settings('secondsslverify')
|
|
|
|
|
|
|
|
if s_sslverify == "true":
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getSSL(self):
|
|
|
|
# Client side certificate
|
|
|
|
s_cert = utils.settings('sslcert')
|
|
|
|
if utils.settings('altip') == "true":
|
|
|
|
s_cert = utils.settings('secondsslcert')
|
|
|
|
|
|
|
|
if s_cert == "None":
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return s_cert
|
|
|
|
|
|
|
|
def setUserPref(self):
|
|
|
|
|
2015-12-28 20:35:27 +11:00
|
|
|
url = PlexAPI.PlexAPI().GetUserArtworkURL(self.currUser)
|
|
|
|
if url:
|
2015-12-28 21:04:28 +11:00
|
|
|
utils.window('EmbyUserImage', value=url)
|
2015-12-25 07:07:00 +11:00
|
|
|
# Set resume point max
|
2015-12-28 20:35:27 +11:00
|
|
|
# url = "{server}/emby/System/Configuration?format=json"
|
|
|
|
# result = doUtils.downloadUrl(url)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2015-12-28 20:35:27 +11:00
|
|
|
# utils.settings('markPlayed', value=str(result['MaxResumePct']))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def getPublicUsers(self):
|
|
|
|
|
|
|
|
server = self.getServer()
|
|
|
|
|
|
|
|
# Get public Users
|
|
|
|
url = "%s/emby/Users/Public?format=json" % server
|
|
|
|
result = self.doUtils.downloadUrl(url, authenticate=False)
|
|
|
|
|
|
|
|
if result != "":
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
# Server connection failed
|
|
|
|
return False
|
|
|
|
|
|
|
|
def hasAccess(self):
|
2015-12-28 21:04:28 +11:00
|
|
|
return True
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def loadCurrUser(self, authenticated=False):
|
|
|
|
|
|
|
|
doUtils = self.doUtils
|
|
|
|
username = self.getUsername()
|
|
|
|
userId = self.getUserId()
|
|
|
|
|
|
|
|
# Only to be used if token exists
|
|
|
|
self.currUserId = userId
|
|
|
|
self.currServer = self.getServer()
|
|
|
|
self.currToken = self.getToken()
|
2016-01-02 00:40:40 +11:00
|
|
|
self.machineIdentifier = self.getServerId()
|
2015-12-25 07:07:00 +11:00
|
|
|
self.ssl = self.getSSLverify()
|
|
|
|
self.sslcert = self.getSSL()
|
|
|
|
|
|
|
|
# Test the validity of current token
|
|
|
|
if authenticated == False:
|
2016-01-02 00:40:40 +11:00
|
|
|
url = "%s/clients" % (self.currServer)
|
2015-12-25 07:07:00 +11:00
|
|
|
utils.window('emby_currUser', value=userId)
|
|
|
|
utils.window('emby_accessToken%s' % userId, value=self.currToken)
|
|
|
|
result = doUtils.downloadUrl(url)
|
|
|
|
|
|
|
|
if result == 401:
|
|
|
|
# Token is no longer valid
|
|
|
|
self.resetClient()
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Set to windows property
|
|
|
|
utils.window('emby_currUser', value=userId)
|
|
|
|
utils.window('emby_accessToken%s' % userId, value=self.currToken)
|
|
|
|
utils.window('emby_server%s' % userId, value=self.currServer)
|
|
|
|
utils.window('emby_server_%s' % userId, value=self.getServer(prefix=False))
|
2016-01-02 00:40:40 +11:00
|
|
|
utils.window('plex_machineIdentifier', value=self.machineIdentifier)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
# Set DownloadUtils values
|
|
|
|
doUtils.setUsername(username)
|
|
|
|
doUtils.setUserId(self.currUserId)
|
|
|
|
doUtils.setServer(self.currServer)
|
|
|
|
doUtils.setToken(self.currToken)
|
|
|
|
doUtils.setSSL(self.ssl, self.sslcert)
|
|
|
|
# parental control - let's verify if access is restricted
|
2015-12-28 20:35:27 +11:00
|
|
|
# self.hasAccess()
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
# Start DownloadUtils session
|
|
|
|
doUtils.startSession()
|
|
|
|
self.getAdditionalUsers()
|
|
|
|
# Set user preferences in settings
|
|
|
|
self.currUser = username
|
|
|
|
self.setUserPref()
|
2015-12-28 21:04:28 +11:00
|
|
|
return True
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def authenticate(self):
|
|
|
|
# Get /profile/addon_data
|
2016-01-14 00:56:44 +11:00
|
|
|
plx = PlexAPI.PlexAPI()
|
2015-12-25 07:07:00 +11:00
|
|
|
addondir = xbmc.translatePath(self.addon.getAddonInfo('profile')).decode('utf-8')
|
|
|
|
hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir)
|
|
|
|
|
|
|
|
username = self.getUsername()
|
2016-01-15 00:47:34 +11:00
|
|
|
userId = utils.settings('userId%s' % username)
|
2015-12-25 07:07:00 +11:00
|
|
|
server = self.getServer()
|
|
|
|
|
|
|
|
# If there's no settings.xml
|
|
|
|
if not hasSettings:
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("No settings.xml found.", 0)
|
2015-12-25 07:07:00 +11:00
|
|
|
self.auth = False
|
|
|
|
return
|
|
|
|
# If no user information
|
2015-12-29 20:50:50 +11:00
|
|
|
elif not server:
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("Missing server information.", 0)
|
2015-12-25 07:07:00 +11:00
|
|
|
self.auth = False
|
|
|
|
return
|
|
|
|
# If there's a token, load the user
|
|
|
|
elif self.getToken():
|
|
|
|
result = self.loadCurrUser()
|
|
|
|
|
|
|
|
if result == False:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.logMsg("Current user: %s" % self.currUser, 1)
|
|
|
|
self.logMsg("Current userId: %s" % self.currUserId, 1)
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("Current accessToken: xxxx", 1)
|
2016-01-27 22:18:54 +11:00
|
|
|
|
|
|
|
utils.window('suspend_LibraryThread', value='false')
|
2015-12-25 07:07:00 +11:00
|
|
|
return
|
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
##### AUTHENTICATE USER #####
|
2015-12-28 03:12:46 +11:00
|
|
|
# Choose Plex user login
|
2016-01-15 00:47:34 +11:00
|
|
|
myplexlogin, plexhome, plexLogin, dont_use_accessToken = \
|
|
|
|
plx.GetPlexLoginFromSettings()
|
|
|
|
self.logMsg("myplexlogin: %s, plexhome: %s, plexLogin: %s"
|
|
|
|
% (myplexlogin, plexhome, plexLogin), 2)
|
|
|
|
if myplexlogin == "true" and plexhome == 'true':
|
2016-01-14 00:56:44 +11:00
|
|
|
username, userId, accessToken = plx.ChoosePlexHomeUser()
|
|
|
|
else:
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("Trying to connect to PMS without a token", 0)
|
|
|
|
accessToken = ''
|
2016-01-14 00:56:44 +11:00
|
|
|
# Check connection
|
|
|
|
if plx.CheckConnection(server, accessToken) == 200:
|
2015-12-25 07:07:00 +11:00
|
|
|
self.currUser = username
|
2016-01-25 20:36:24 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2016-01-15 00:47:34 +11:00
|
|
|
if username:
|
2016-01-25 20:36:24 +11:00
|
|
|
dialog.notification(self.addonName,
|
|
|
|
"Welcome %s" % username,
|
|
|
|
"special://home/addons/plugin.video."
|
|
|
|
"plexkodiconnect/icon.png")
|
2016-01-15 00:47:34 +11:00
|
|
|
else:
|
2016-01-25 20:36:24 +11:00
|
|
|
dialog.notification(self.addonName,
|
|
|
|
"Welcome",
|
|
|
|
"special://home/addons/plugin.video."
|
|
|
|
"plexkodiconnect/icon.png")
|
2015-12-25 07:07:00 +11:00
|
|
|
utils.settings('accessToken', value=accessToken)
|
|
|
|
utils.settings('userId%s' % username, value=userId)
|
2016-01-13 03:23:55 +11:00
|
|
|
self.logMsg("User authenticated with an access token", 1)
|
2015-12-25 07:07:00 +11:00
|
|
|
self.loadCurrUser(authenticated=True)
|
|
|
|
utils.window('emby_serverStatus', clear=True)
|
2016-01-15 00:47:34 +11:00
|
|
|
# Write plex_machineIdentifier to window
|
|
|
|
plex_machineIdentifier = utils.settings('plex_machineIdentifier')
|
2016-01-25 20:36:24 +11:00
|
|
|
utils.window('plex_machineIdentifier', plex_machineIdentifier)
|
2015-12-25 07:07:00 +11:00
|
|
|
self.retry = 0
|
2016-01-25 20:36:24 +11:00
|
|
|
# Make sure that lib sync thread is not paused
|
2016-01-27 22:18:54 +11:00
|
|
|
utils.window('suspend_LibraryThread', value='false')
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("Error: user authentication failed.", -1)
|
2015-12-25 07:07:00 +11:00
|
|
|
utils.settings('accessToken', value="")
|
|
|
|
utils.settings('userId%s' % username, value="")
|
2016-01-14 00:56:44 +11:00
|
|
|
|
|
|
|
# Give 3 attempts at entering password / selecting user
|
|
|
|
if self.retry == 3:
|
|
|
|
utils.window('emby_serverStatus', value="Stop")
|
2016-01-15 00:47:34 +11:00
|
|
|
xbmcgui.Dialog().ok(heading=self.addonName,
|
|
|
|
line1="Failed to authenticate too many"
|
|
|
|
"times.",
|
|
|
|
line2="You can retry by resetting attempts"
|
|
|
|
" in the addon settings.")
|
2016-01-14 00:56:44 +11:00
|
|
|
self.retry += 1
|
2015-12-25 07:07:00 +11:00
|
|
|
self.auth = False
|
|
|
|
|
|
|
|
def resetClient(self):
|
|
|
|
|
|
|
|
self.logMsg("Reset UserClient authentication.", 1)
|
|
|
|
username = self.getUsername()
|
2016-01-13 03:23:55 +11:00
|
|
|
|
|
|
|
utils.settings('accessToken', value="")
|
|
|
|
utils.window('emby_accessToken%s' % username, clear=True)
|
|
|
|
self.currToken = None
|
|
|
|
self.logMsg("User token has been removed.", 1)
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
self.auth = True
|
|
|
|
self.currUser = None
|
2016-01-13 03:23:55 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def run(self):
|
|
|
|
|
|
|
|
self.logMsg("----===## Starting UserClient ##===----", 0)
|
|
|
|
|
2016-01-27 01:13:03 +11:00
|
|
|
while not self.threadStopped():
|
|
|
|
while self.threadSuspended():
|
|
|
|
if self.threadStopped():
|
|
|
|
break
|
|
|
|
xbmc.sleep(3000)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
status = utils.window('emby_serverStatus')
|
|
|
|
if status:
|
|
|
|
# Verify the connection status to server
|
|
|
|
if status == "restricted":
|
|
|
|
# Parental control is restricting access
|
|
|
|
self.HasAccess = False
|
|
|
|
|
|
|
|
elif status == "401":
|
|
|
|
# Unauthorized access, revoke token
|
|
|
|
utils.window('emby_serverStatus', value="Auth")
|
|
|
|
self.resetClient()
|
|
|
|
|
|
|
|
if self.auth and (self.currUser is None):
|
|
|
|
# Try to authenticate user
|
|
|
|
status = utils.window('emby_serverStatus')
|
|
|
|
if not status or status == "Auth":
|
|
|
|
# Set auth flag because we no longer need
|
|
|
|
# to authenticate the user
|
|
|
|
self.auth = False
|
|
|
|
self.authenticate()
|
|
|
|
|
|
|
|
|
|
|
|
if not self.auth and (self.currUser is None):
|
|
|
|
# If authenticate failed.
|
|
|
|
server = self.getServer()
|
|
|
|
username = self.getUsername()
|
|
|
|
status = utils.window('emby_serverStatus')
|
|
|
|
|
|
|
|
# The status Stop is for when user cancelled password dialog.
|
|
|
|
if server and username and status != "Stop":
|
|
|
|
# Only if there's information found to login
|
|
|
|
self.logMsg("Server found: %s" % server, 2)
|
|
|
|
self.logMsg("Username found: %s" % username, 2)
|
|
|
|
self.auth = True
|
|
|
|
|
2016-01-27 01:13:03 +11:00
|
|
|
self.doUtils.stopSession()
|
2015-12-25 07:07:00 +11:00
|
|
|
self.logMsg("##===---- UserClient Stopped ----===##", 0)
|