Enable non-ASCI PMS server name and username

This commit is contained in:
tomkat83 2016-03-07 13:01:45 +01:00
parent 75a2fac4cc
commit c6503f8a8e
5 changed files with 92 additions and 86 deletions

View file

@ -79,7 +79,6 @@ class PlexAPI():
self.deviceName = client.getDeviceName()
self.plexversion = client.getVersion()
self.platform = client.getPlatform()
self.user = utils.window('plex_username')
self.userId = utils.window('emby_currUser')
self.token = utils.window('emby_accessToken%s' % self.userId)
self.server = utils.window('emby_server%s' % self.userId)
@ -95,13 +94,15 @@ class PlexAPI():
'plexid': utils.settings('plexid'),
'myplexlogin': utils.settings('myplexlogin')
plexLogin is unicode or empty unicode string u''
Returns empty strings '' for a setting if not found.
myplexlogin is 'true' if user opted to log into plex.tv (the default)
plexhome is 'true' if plex home is used (the default)
"""
return {
'plexLogin': utils.settings('plexLogin'),
'plexLogin': utils.settings('plexLogin').decode('utf-8'),
'plexToken': utils.settings('plexToken'),
'plexhome': utils.settings('plexhome'),
'plexid': utils.settings('plexid'),
@ -127,32 +128,31 @@ class PlexAPI():
retrievedPlexLogin = ''
plexLogin = 'dummy'
authtoken = ''
dialog = xbmcgui.Dialog()
while retrievedPlexLogin == '' and plexLogin != '':
dialog = xbmcgui.Dialog()
# Enter plex.tv username. Or nothing to cancel.
plexLogin = dialog.input(
self.addonName + string(39300),
self.addonName.encode('utf-8') + string(39300).encode('utf-8'),
type=xbmcgui.INPUT_ALPHANUM,
)
if plexLogin != "":
dialog = xbmcgui.Dialog()
# Enter password for plex.tv user
plexPassword = dialog.input(
string(39301) + plexLogin,
string(39301).encode('utf-8') + plexLogin.encode('utf-8'),
type=xbmcgui.INPUT_ALPHANUM,
option=xbmcgui.ALPHANUM_HIDE_INPUT
)
retrievedPlexLogin, authtoken = self.MyPlexSignIn(
plexLogin,
plexPassword,
{'X-Plex-Client-Identifier': self.clientId}
)
{'X-Plex-Client-Identifier': self.clientId})
self.logMsg("plex.tv username and token: %s, %s"
% (plexLogin, authtoken), 1)
if plexLogin == '':
dialog = xbmcgui.Dialog()
# Could not sign in user
dialog.ok(self.addonName, string(39302) + plexLogin)
dialog.ok(self.addonName,
string(39302).encode('utf-8')
+ plexLogin.encode('utf-8'))
# Write to Kodi settings file
utils.settings('plexLogin', value=retrievedPlexLogin)
utils.settings('plexToken', value=authtoken)
@ -177,12 +177,12 @@ class PlexAPI():
dialog = xbmcgui.Dialog()
if not code:
# Problems trying to contact plex.tv. Try again later
dialog.ok(self.addonName, string(39303))
dialog.ok(self.addonName, string(39303).encode('utf-8'))
return False
# Go to https://plex.tv/pin and enter the code:
answer = dialog.yesno(self.addonName,
string(39304) + "\n\n",
code)
(string(39304) + "\n\n").encode('utf-8'),
code.encode('utf-8'))
if not answer:
return False
count = 0
@ -196,7 +196,7 @@ class PlexAPI():
count += 1
if not xml:
# Could not sign in to plex.tv Try again later
dialog.ok(self.addonName, string(39305))
dialog.ok(self.addonName, string(39305).encode('utf-8'))
return False
# Parse xml
userid = xml.attrib.get('id')
@ -259,11 +259,11 @@ class PlexAPI():
try:
code = xml.find('code').text
identifier = xml.find('id').text
self.logMsg('Successfully retrieved code and id from plex.tv', 1)
return code, identifier
except:
self.logMsg("Error, no PIN from plex.tv provided", -1)
self.logMsg("plex.tv/pin: Code is: %s" % code, 2)
self.logMsg("plex.tv/pin: Identifier is: %s" % identifier, 2)
return code, identifier
return None, None
def TalkToPlexServer(self, url, talkType="GET", verify=True, token=None):
"""
@ -814,7 +814,6 @@ class PlexAPI():
'X-Plex-Version': self.plexversion,
'X-Plex-Client-Identifier': self.clientId,
'X-Plex-Provides': 'player',
'X-Plex-Username': self.user,
'X-Plex-Client-Capabilities': 'protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:1080&level:51};audioDecoders=mp3,aac,dts{bitrate:800000&channels:8},ac3{bitrate:800000&channels:8}',
'X-Plex-Client-Profile-Extra': 'add-transcode-target-audio-codec(type=videoProfile&context=streaming&protocol=*&audioCodec=dca,ac3)',
}
@ -1042,6 +1041,8 @@ class PlexAPI():
self.logMsg("No URL for user avatar.", 1)
return False
for user in users:
self.logMsg('type user: %s, type username: %s'
% (type(user['title']), type(username)))
if username in user['title']:
url = user['thumb']
self.logMsg("Avatar url for user %s is: %s" % (username, url), 1)
@ -1060,6 +1061,7 @@ class PlexAPI():
Will return empty strings if failed.
"""
string = self.__language__
dialog = xbmcgui.Dialog()
plexLogin = utils.settings('plexLogin')
plexToken = utils.settings('plexToken')
@ -1074,19 +1076,21 @@ class PlexAPI():
return ('', '', '')
userlist = []
userlistCoded = []
for user in users:
username = user['title']
userlist.append(username)
userlistCoded.append(username.encode('utf-8'))
usernumber = len(userlist)
usertoken = ''
# Plex home not in use: only 1 user returned
trials = 0
while trials < 3:
if usernumber > 1:
dialog = xbmcgui.Dialog()
# Select user
user_select = dialog.select(self.addonName + string(39306),
userlist)
user_select = dialog.select(
(self.addonName + string(39306)).encode('utf-8'),
userlistCoded)
if user_select == -1:
self.logMsg("No user selected.", 1)
xbmc.executebuiltin('Addon.OpenSettings(%s)'
@ -1101,11 +1105,10 @@ class PlexAPI():
user = users[user_select]
# Ask for PIN, if protected:
if user['protected'] == '1':
dialog = xbmcgui.Dialog()
# Please enter pin for user
self.logMsg('Asking for users PIN', 1)
pin = dialog.input(
string(39307) + selected_user,
(string(39307) + selected_user).encode('utf-8'),
type=xbmcgui.INPUT_NUMERIC,
option=xbmcgui.ALPHANUM_HIDE_INPUT)
# User chose to cancel
@ -1122,12 +1125,11 @@ class PlexAPI():
)
# Couldn't get user auth
if not username:
dialog = xbmcgui.Dialog()
# Could not login user, please try again
if not dialog.yesno(
self.addonName,
string(39308) + selected_user,
string(39309)
(string(39308) + selected_user).encode('utf-8'),
string(39309).encode('utf-8')
):
# User chose to cancel
break
@ -1188,7 +1190,7 @@ class PlexAPI():
self.logMsg("username: %s, token: xxxx. "
"Saving to window and file settings" % username, 0)
utils.window('emby_currUser', value=userId)
utils.settings('userId%s' % username, value=userId)
utils.settings('userId', value=userId)
utils.settings('username', value=username)
utils.window('emby_accessToken%s' % userId, value=token)
return (username, token)
@ -1411,7 +1413,6 @@ class API():
self.part = 0
self.clientinfo = clientinfo.ClientInfo()
self.clientId = self.clientinfo.getDeviceId()
self.user = utils.window('plex_username')
self.userId = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userId)
self.token = utils.window('emby_accessToken%s' % self.userId)

View file

@ -20,10 +20,6 @@ import PlexAPI
class InitialSetup():
def __init__(self):
self.addon = xbmcaddon.Addon()
self.__language__ = self.addon.getLocalizedString
self.clientInfo = clientinfo.ClientInfo()
self.addonId = self.clientInfo.getAddonId()
self.doUtils = downloadutils.DownloadUtils()
@ -36,7 +32,7 @@ class InitialSetup():
Check server, user, direct paths, music, direct stream if not direct
path.
"""
string = self.__language__
string = xbmcaddon.Addon().getLocalizedString
# SERVER INFO #####
self.logMsg("Initial setup called.", 0)
server = self.userClient.getServer()
@ -63,7 +59,7 @@ class InitialSetup():
# Could not login, please try again
dialog.ok(
self.addonName,
string(39009)
string(39009).encode('utf-8')
)
result = self.plx.PlexTvSignInWithPin()
if result:
@ -74,7 +70,7 @@ class InitialSetup():
# Problems connecting to plex.tv. Network or internet issue?
dialog.ok(
self.addonName,
string(39010)
string(39010).encode('utf-8')
)
# If a Plex server IP has already been set, return.
if server and forcePlexTV is False:
@ -110,18 +106,18 @@ class InitialSetup():
if len(serverlist) == 0:
dialog.ok(
self.addonName,
string(39011)
string(39011).encode('utf-8')
)
break
for server in serverlist:
if server['local'] == '1':
# server is in the same network as client. Add "local"
dialoglist.append(str(server['name']) + string(39022))
dialoglist.append(
server['name'].encode('utf-8')
+ string(39022).encode('utf-8'))
else:
dialoglist.append(str(server['name']))
resp = dialog.select(
string(39012),
dialoglist)
dialoglist.append(server['name'].encode('utf-8'))
resp = dialog.select(string(39012).encode('utf-8'), dialoglist)
server = serverlist[resp]
activeServer = server['machineIdentifier']
url = server['scheme'] + '://' + server['ip'] + ':' + \
@ -141,8 +137,9 @@ class InitialSetup():
# Not yet authorized for Plex server
# Please sign in to plex.tv
dialog.ok(self.addonName,
string(39013) + str(server['name']),
string(39014))
string(39013).encode('utf-8')
+ server['name'].encode('utf-8'),
string(39014).encode('utf-8'))
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
@ -154,7 +151,8 @@ class InitialSetup():
# Problems connecting
elif chk >= 400 or chk is False:
# Problems connecting to server. Pick another server?
resp = dialog.yesno(self.addonName, string(39015))
resp = dialog.yesno(self.addonName,
string(39015).encode('utf-8'))
# Exit while loop if user chooses No
if not resp:
break
@ -166,7 +164,7 @@ class InitialSetup():
# Enter Kodi settings instead
if dialog.yesno(
heading=self.addonName,
line1=string(39016)):
line1=string(39016).encode('utf-8')):
self.logMsg("User opted to disable Plex music library.", 1)
utils.settings('enableMusic', value="false")
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
@ -202,12 +200,12 @@ class InitialSetup():
if dialog.yesno(
heading=self.addonName,
line1=string(39016)):
line1=string(39016).encode('utf-8')):
self.logMsg("User opted to disable Plex music library.", 1)
utils.settings('enableMusic', value="false")
if dialog.yesno(
heading=self.addonName,
line1=string(39017)):
line1=string(39017).encode('utf-8')):
xbmc.executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')

View file

@ -54,12 +54,15 @@ class UserClient(threading.Thread):
self.AdditionalUser = additionalUsers.split(',')
def getUsername(self):
"""
Returns username as unicode
"""
username = utils.settings('username')
username = utils.settings('username').decode('utf-8')
if not username:
self.logMsg("No username saved, trying to get Plex username", 0)
username = utils.settings('plexLogin')
username = utils.settings('plexLogin').decode('utf-8')
if not username:
self.logMsg("Also no Plex username found", 0)
return ""
@ -84,33 +87,31 @@ class UserClient(threading.Thread):
if username is None:
username = self.getUsername()
w_userId = window('emby_currUser')
s_userId = settings('userId%s' % username)
s_userId = settings('userId')
# Verify the window property
if w_userId:
if not s_userId:
# Save access token if it's missing from settings
settings('userId%s' % username, value=w_userId)
log("Returning userId from WINDOW for username: %s UserId: %s"
% (username, w_userId), 1)
settings('userId', value=w_userId)
log("Returning userId %s from WINDOW for username %s"
% (w_userId, username), 0)
return w_userId
# Verify the settings
elif s_userId:
log("Returning userId from SETTINGS for username: %s userId: %s"
% (username, s_userId), 1)
log("Returning userId %s from SETTINGS for username %s"
% (w_userId, username), 0)
return s_userId
# No userId found
else:
log("No userId saved for username: %s. Trying to get Plex ID"
% username, 0)
plexId = settings('plexid')
if not plexId:
log('Also no Plex ID found in settings', 0)
return ''
log('Using Plex ID %s as userid for username: %s'
% (plexId, username))
settings('userId%s' % username, value=plexId)
return plexId
log("No userId saved. Trying to get Plex to use instead", 0)
plexId = settings('plexid')
if not plexId:
log('Also no Plex ID found in settings', 0)
return ''
log('Using Plex ID %s as userid for username %s'
% (plexId, username), 0)
settings('userId', value=plexId)
return plexId
def getServer(self, prefix=True):
@ -157,14 +158,14 @@ class UserClient(threading.Thread):
if not s_token:
# Save access token if it's missing from settings
settings('accessToken', value=w_token)
log("Returning accessToken from WINDOW for username: %s accessToken: %s"
% (username, w_token), 2)
log("Returning accessToken from WINDOW for username: %s "
"accessToken: xxxx" % username, 2)
return w_token
# Verify the settings
elif s_token:
log("Returning accessToken from SETTINGS for username: %s accessToken: %s"
% (username, s_token), 2)
window('emby_accessToken%s' % username, value=s_token)
log("Returning accessToken from SETTINGS for username: %s "
"accessToken: xxxx" % username, 2)
window('emby_accessToken%s' % userId, value=s_token)
return s_token
else:
log("No token found.", 1)
@ -244,7 +245,9 @@ class UserClient(threading.Thread):
log("Access is granted.", 1)
self.HasAccess = True
window('emby_serverStatus', clear=True)
xbmcgui.Dialog().notification(self.addonName, utils.language(33007))
xbmcgui.Dialog().notification(
self.addonName,
utils.language(33007).encode('utf-8'))
def loadCurrUser(self, authenticated=False):
self.logMsg('Loading current user', 0)
@ -284,7 +287,6 @@ class UserClient(threading.Thread):
window('plex_username', value=username)
window('emby_accessToken%s' % userId, value=self.currToken)
window('emby_server%s' % userId, value=self.currServer)
window('emby_server_%s' % userId, value=self.getServer(prefix=False))
window('plex_machineIdentifier', value=self.machineIdentifier)
window('emby_serverStatus', clear=True)
@ -359,9 +361,8 @@ class UserClient(threading.Thread):
# Check connection
if plx.CheckConnection(server, accessToken) == 200:
self.currUser = username
dialog = xbmcgui.Dialog()
settings('accessToken', value=accessToken)
settings('userId%s' % username, value=userId)
settings('userId', value=userId)
log("User authenticated with an access token", 1)
if self.loadCurrUser(authenticated=True) is False:
# Something went really wrong, return and try again
@ -372,7 +373,7 @@ class UserClient(threading.Thread):
if username:
dialog.notification(
heading=self.addonName,
message="Welcome %s" % username.decode('utf-8'),
message=("Welcome " + username).encode('utf-8'),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png")
else:
dialog.notification(
@ -384,13 +385,14 @@ class UserClient(threading.Thread):
else:
self.logMsg("Error: user authentication failed.", -1)
settings('accessToken', value="")
settings('userId%s' % username, value="")
settings('userId', value="")
# Give attempts at entering password / selecting user
if self.retry >= 5:
log("Too many retries.", 1)
window('emby_serverStatus', value="Stop")
dialog.ok(lang(33001), lang(39023))
dialog.ok(lang(33001).encode('utf-8'),
lang(39023).encode('utf-8'))
xbmc.executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')

View file

@ -210,12 +210,17 @@ def window(property, value=None, clear=False, windowid=10000):
# Takes unicode or string by default!
WINDOW.setProperty(property, value)
else: #getproperty returns string so convert to unicode
return unicode(WINDOW.getProperty(property))
return WINDOW.getProperty(property)
def settings(setting, value=None):
# Get or add addon setting
"""
Get or add addon setting.
Settings needs to be string
Value can either be unicode or string
"""
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
if value is not None:
# Takes string or unicode by default!
addon.setSetting(setting, value)

View file

@ -172,8 +172,8 @@ class Service():
xbmcgui.Dialog().notification(
heading=self.addonName,
message=("%s %s"
% (lang(33000),
user.currUser.decode('utf-8'))),
% (lang(33000).encode('utf-8'),
user.currUser.encode('utf-8'))),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
time=2000,
sound=False)
@ -226,8 +226,8 @@ class Service():
window('emby_online', value="false")
xbmcgui.Dialog().notification(
heading=lang(33001),
message="%s %s" % (self.addonName, lang(33002)),
heading=lang(33001).encode('utf-8'),
message="%s %s" % (self.addonName, lang(33002).encode('utf-8')),
icon="special://home/addons/plugin.video."
"plexkodiconnect/icon.png",
sound=False)
@ -245,7 +245,7 @@ class Service():
# Alert the user that server is online.
xbmcgui.Dialog().notification(
heading=self.addonName,
message=lang(33003),
message=lang(33003).encode('utf-8'),
icon="special://home/addons/plugin.video."
"plexkodiconnect/icon.png",
time=2000,