From 1f8eb7f2ab7a4a18e5b4a2425bc991e6443373d2 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Wed, 13 Jan 2016 14:56:44 +0100 Subject: [PATCH] Trying to change Plex.tv sign in process --- default.py | 2 +- resources/lib/PlexAPI.py | 24 ++++++++++++++-------- resources/lib/userclient.py | 41 ++++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/default.py b/default.py index 3a07760c..1737721d 100644 --- a/default.py +++ b/default.py @@ -95,7 +95,7 @@ class Main: xbmc.executebuiltin('Addon.OpenSettings(plugin.video.plexkodiconnect)') if mode == "switchuser": xbmc.log('Requesting user switch') - userclient.UserClient().signUserOut() + utils.window('emby_serverStatus', value="401") elif mode in ("manualsync", "repair"): if utils.window('emby_dbScan') != "true": import librarysync diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index a38f49ac..66498d41 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -181,14 +181,21 @@ class PlexAPI(): if 'plex.tv' in url: url = 'https://plex.tv/api/home/users' else: - url = url + '/clients' - self.logMsg("CheckConnection called for url %s with a token" % url, 2) - - r = self.doUtils.downloadUrl( - url, - authenticate=False, - headerOptions={'X-Plex-Token': token} - ) + url = url + '/library/onDeck' + + if token: + self.logMsg("CheckConnection for %s with a token" % url, 0) + r = self.doUtils.downloadUrl( + url, + authenticate=False, + headerOptions={'X-Plex-Token': token, + 'Accept': 'application/json'}) + else: + self.logMsg("CheckConnection for %s without a token" % url, 0) + r = self.doUtils.downloadUrl( + url, + authenticate=False, + headerOptions={'Accept': 'application/json'}) self.logMsg("Response was: %s" % r, 2) # List of exception returns, when connection failed exceptionlist = [ @@ -886,7 +893,6 @@ class PlexAPI(): Will return empty strings if failed. """ - string = self.__language__ plexLogin = self.plexLogin plexToken = self.plexToken self.logMsg("Getting user list.", 1) diff --git a/resources/lib/userclient.py b/resources/lib/userclient.py index 8911626c..e893700a 100644 --- a/resources/lib/userclient.py +++ b/resources/lib/userclient.py @@ -47,16 +47,8 @@ class UserClient(threading.Thread): self.addonName = clientinfo.ClientInfo().getAddonName() self.doUtils = downloadutils.DownloadUtils() - self.signoutUser = threading.Event() - threading.Thread.__init__(self) - def signUserOut(self): - self.signoutUser.set() - - def userSignedOut(self): - return self.signoutUser.isSet() - def logMsg(self, msg, lvl=1): className = self.__class__.__name__ @@ -283,6 +275,7 @@ class UserClient(threading.Thread): def authenticate(self): # Get /profile/addon_data + plx = PlexAPI.PlexAPI() addondir = xbmc.translatePath(self.addon.getAddonInfo('profile')).decode('utf-8') hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir) @@ -315,9 +308,15 @@ class UserClient(threading.Thread): ##### AUTHENTICATE USER ##### # Choose Plex user login - username, userId, accessToken = PlexAPI.PlexAPI().ChoosePlexHomeUser() - - if accessToken is not "": + accessToken = "" + myplexlogin = utils.settings('myplexlogin') + if myplexlogin == "true": + username, userId, accessToken = plx.ChoosePlexHomeUser() + else: + # Try connecting without credentials + pass + # Check connection + if plx.CheckConnection(server, accessToken) == 200: self.currUser = username xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % username) utils.settings('accessToken', value=accessToken) @@ -330,7 +329,18 @@ class UserClient(threading.Thread): self.logMsg("User authentication failed.", 1) utils.settings('accessToken', value="") utils.settings('userId%s' % username, value="") - self.retry = 0 + + # Give 3 attempts at entering password / selecting user + if self.retry == 3: + self.logMsg("""Too many retries. You can retry by resetting + attempts in the addon settings.""", 1) + utils.window('emby_serverStatus', value="Stop") + xbmcgui.Dialog().ok( + heading=self.addonName, + line1="Failed to authenticate too many times.", + line2="You can retry by resetting attempts in the addon " + "settings.") + self.retry += 1 self.auth = False def resetClient(self): @@ -365,13 +375,6 @@ class UserClient(threading.Thread): utils.window('emby_serverStatus', value="Auth") self.resetClient() - if self.userSignedOut(): - # Check whether another thread wanted to sign out user - self.logMsg("User sign out requested", 0) - self.resetClient() - # Reset threading event - self.signoutUser.clear() - if self.auth and (self.currUser is None): # Try to authenticate user status = utils.window('emby_serverStatus')