diff --git a/default.py b/default.py
index 30271bec..2b33a244 100644
--- a/default.py
+++ b/default.py
@@ -48,7 +48,6 @@ class Main:
xbmc.log("PlexKodiConnect - Full sys.argv received: %s" % sys.argv)
base_url = sys.argv[0]
params = urlparse.parse_qs(sys.argv[2][1:])
- xbmc.log("PlexKodiConnect - Parameter string: %s" % sys.argv[2])
try:
mode = params['mode'][0]
itemid = params.get('id', '')
@@ -80,12 +79,13 @@ class Main:
'companion': entrypoint.plexCompanion,
'switchuser': entrypoint.switchPlexUser,
'deviceid': entrypoint.resetDeviceId,
- 'reConnect': entrypoint.reConnect,
'delete': entrypoint.deleteItem,
'browseplex': entrypoint.BrowsePlexContent,
'ondeck': entrypoint.getOnDeck,
'chooseServer': entrypoint.chooseServer,
- 'watchlater': entrypoint.watchlater
+ 'watchlater': entrypoint.watchlater,
+ 'enterPMS': entrypoint.enterPMS,
+ 'togglePlexTV': entrypoint.togglePlexTV
}
if "/extrafanart" in sys.argv[0]:
diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml
index 38255833..dc4e6f3f 100644
--- a/resources/language/English/strings.xml
+++ b/resources/language/English/strings.xml
@@ -367,7 +367,8 @@
[COLOR yellow]Sync Emby Theme Media to Kodi[/COLOR]
local
Failed to authenticate. Did you login to plex.tv?
- [COLOR yellow]Reset PMS and plex.tv connections to re-login[/COLOR]
+
+
Automatically log into plex.tv on startup
Enable constant background sync
Playback Mode
@@ -411,6 +412,11 @@
Recently Added: Also show already watched episodes
Force Transcode HEVC
Recently Added: Also show already watched movies (Refresh Plex playlist/nodes!)
+ Your current Plex Media Server:
+ [COLOR yellow]Manually enter Plex Media Server address[/COLOR]
+ Current address:
+ Current port:
+ Current plex.tv status:
Log-out Plex Home User
@@ -421,10 +427,19 @@
Unable to run the sync, the add-on is not connected to a Plex server.
Plex might lock your account if you fail to log in too many times. Proceed anyway?
Resetting PMS connections, please wait
- Failed to reset PMS and plex.tv connects. Try to restart Kodi.
- [COLOR yellow]Log-in to plex.tv[/COLOR]
+ Failed to reset PKC. Try to restart Kodi.
+ [COLOR yellow]Toggle plex.tv login (sign in or sign out)[/COLOR]
Not yet connected to Plex Server
Watch later
+ is offline
+ Even though we signed in to plex.tv, we could not authorize for PMS
+ Enter your Plex Media Server's IP or URL, Examples are:
+
+ Does your Plex Media Server support SSL connections? (https instead of http)?
+ Error contacting PMS
+ Abort (Yes) or save address anyway (No)?
+ connected
+ plex.tv toggle successful
diff --git a/resources/language/German/strings.xml b/resources/language/German/strings.xml
index 12aaeb68..c9c33580 100644
--- a/resources/language/German/strings.xml
+++ b/resources/language/German/strings.xml
@@ -306,7 +306,8 @@
[COLOR yellow]Plex Themes zu Kodi synchronisieren[/COLOR]
lokal
Plex Media Server Authentifizierung fehlgeschlagen. Haben Sie sich bei plex.tv eingeloggt?
- [COLOR yellow]PMS und plex.tv Verbindungen zurücksetzen für erneuten Login[/COLOR]
+
+
Automatisch beim Starten bei plex.tv einloggen
Laufende Synchronisierung im Hintergrund aktivieren
Playback Modus
@@ -350,6 +351,10 @@
"Zuletzt hinzugefügt": gesehene Folgen anzeigen
HEVC Codec Transkodierung erzwingen
"Zuletzt hinzugefügt": gesehene Filme anzeigen (Plex Playlisten und Nodes zurücksetzen!)
+ Aktueller Plex Media Server:
+ [COLOR yellow]Plex Media Server Adresse manuell eingeben[/COLOR]
+ Aktuelle Adresse:
+ Aktueller Port:
Plex Home Benutzer abmelden:
@@ -360,10 +365,19 @@
Plex Bibliothek kann nicht gescannt werden, da keine Verbindung mit einem Plex Server besteht.
Plex könnte möglicherweise Ihren Account sperren, wenn Sie zu oft versuchen, sich erfolglos anzumelden. Trotzdem fortfahren?
PMS Verbindungen werden zurückgesetzt
- PMS und plex.tv Verbindungen konnten nicht zurückgesetzt werden. Bitte versuchen Sie, Kodi neu zu starten, um das Problem zu beheben.
- [COLOR yellow]Bei plex.tv einloggen[/COLOR]
+ PKC konnte nicht zurückgesetzt werden. Bitte versuchen Sie, Kodi neu zu starten, um das Problem zu beheben.
+ [COLOR yellow]plex.tv Login wechseln (ein- resp. ausloggen)[/COLOR]
Noch nicht mit Plex Server verbunden
Später ansehen
+ ist offline
+ Obwohl mit plex.tv verbunden, konnte keine Verbindung hergestellt werden mit
+ Plex Media Server IP oder URL eingeben. Zum Beispiel:
+
+ Unterstützt der Plex Media Server sichere SSL Verbindungen (https anstelle von http)?
+ Error beim Verbinden mit PMS
+ Abbrechen (Ja) oder PMS Adresse trotzdem speichern (Nein)?
+ verbunden
+ plex.tv wechsel OK
Alle Plex Bilder in Kodi zwischenzuspeichern kann sehr lange dauern. Möchten Sie wirklich fortfahren?
diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py
index 581848ed..4a05b2b5 100644
--- a/resources/lib/PlexAPI.py
+++ b/resources/lib/PlexAPI.py
@@ -206,6 +206,7 @@ class PlexAPI():
utils.settings('plexHomeSize', homeSize)
# Let Kodi log into plex.tv on startup from now on
utils.settings('myplexlogin', 'true')
+ utils.settings('plex_status', value='Logged in to plex.tv')
return result
def CheckPlexTvSignin(self, identifier):
@@ -290,7 +291,8 @@ class PlexAPI():
answer = self.doUtils(url,
authenticate=False,
headerOptions=headerOptions,
- verifySSL=verifySSL)
+ verifySSL=verifySSL,
+ timeout=4)
if answer is None:
self.logMsg("Could not connect to %s" % url, 0)
count += 1
@@ -459,8 +461,6 @@ class PlexAPI():
while True:
try:
data, server = GDM.recvfrom(1024)
- # dprint(__name__, 1, "Received data from {0}", server)
- # dprint(__name__, 1, "Data received:\n {0}", data)
returnData.append({'from': server,
'data': data})
except socket.timeout:
@@ -469,31 +469,31 @@ class PlexAPI():
GDM.close()
pmsList = {}
-
for response in returnData:
update = {'ip': response.get('from')[0]}
# Check if we had a positive HTTP response
- if "200 OK" in response.get('data'):
- for each in response.get('data').split('\n'):
- # decode response data
- update['discovery'] = "auto"
- # update['owned']='1'
- # update['master']= 1
- # update['role']='master'
+ if "200 OK" not in response.get('data'):
+ continue
+ for each in response.get('data').split('\n'):
+ # decode response data
+ update['discovery'] = "auto"
+ # update['owned']='1'
+ # update['master']= 1
+ # update['role']='master'
- if "Content-Type:" in each:
- update['content-type'] = each.split(':')[1].strip()
- elif "Resource-Identifier:" in each:
- update['uuid'] = each.split(':')[1].strip()
- elif "Name:" in each:
- update['serverName'] = utils.tryDecode(each.split(
- ':')[1].strip())
- elif "Port:" in each:
- update['port'] = each.split(':')[1].strip()
- elif "Updated-At:" in each:
- update['updated'] = each.split(':')[1].strip()
- elif "Version:" in each:
- update['version'] = each.split(':')[1].strip()
+ if "Content-Type:" in each:
+ update['content-type'] = each.split(':')[1].strip()
+ elif "Resource-Identifier:" in each:
+ update['uuid'] = each.split(':')[1].strip()
+ elif "Name:" in each:
+ update['serverName'] = utils.tryDecode(each.split(
+ ':')[1].strip())
+ elif "Port:" in each:
+ update['port'] = each.split(':')[1].strip()
+ elif "Updated-At:" in each:
+ update['updated'] = each.split(':')[1].strip()
+ elif "Version:" in each:
+ update['version'] = each.split(':')[1].strip()
pmsList[update['uuid']] = update
return pmsList
@@ -530,6 +530,8 @@ class PlexAPI():
for uuid in pmsList:
PMS = pmsList[uuid]
if PMS['uuid'] in self.g_PMS:
+ self.logMsg('We already know of PMS %s from plex.tv'
+ % PMS['serverName'], 1)
continue
self.declarePMS(PMS['uuid'], PMS['serverName'], 'http',
PMS['ip'], PMS['port'])
@@ -574,7 +576,7 @@ class PlexAPI():
import Queue
queue = Queue.Queue()
- threads = []
+ threadQueue = []
maxAgeSeconds = 2*60*60*24
for Dir in xml.findall('Device'):
@@ -613,11 +615,30 @@ class PlexAPI():
if Con.get('local') != '1':
PMS['connections'].append(Con)
- # poke PMS, own thread for each poke
t = Thread(target=self.pokePMS,
args=(PMS, queue))
- t.start()
- threads.append(t)
+ threadQueue.append(t)
+
+ maxThreads = int(utils.settings('imageCacheLimit'))
+ threads = []
+ # poke PMS, own thread for each PMS
+ while(True):
+ # Remove finished threads
+ for t in threads:
+ if not t.isAlive():
+ threads.remove(t)
+ if len(threads) < maxThreads:
+ try:
+ t = threadQueue.pop()
+ except IndexError:
+ # We have done our work
+ break
+ else:
+ t.start()
+ threads.append(t)
+ else:
+ self.logMsg('Waiting for queue spot to poke PMS', 1)
+ xbmc.sleep(50)
# wait for requests being answered
for t in threads:
@@ -628,7 +649,6 @@ class PlexAPI():
PMS = queue.get()
self.declarePMS(PMS['uuid'], PMS['name'],
PMS['protocol'], PMS['ip'], PMS['port'])
- # dflt: token='', local, owned - updated later
self.updatePMSProperty(
PMS['uuid'], 'accesstoken', PMS['token'])
self.updatePMSProperty(
@@ -643,13 +663,14 @@ class PlexAPI():
queue.task_done()
def pokePMS(self, PMS, queue):
- if PMS['connections'][0].get('local') == '1':
- protocol = PMS['connections'][0].get('protocol')
- address = PMS['connections'][0].get('address')
- port = PMS['connections'][0].get('port')
+ data = PMS['connections'][0].attrib
+ if data['local'] == '1':
+ protocol = data['protocol']
+ address = data['address']
+ port = data['port']
url = '%s://%s:%s' % (protocol, address, port)
else:
- url = PMS['connections'][0].get('uri')
+ url = data['uri']
protocol, address, port = url.split(':')
address = address.replace('/', '')
@@ -659,8 +680,8 @@ class PlexAPI():
verifySSL=False,
timeout=3)
try:
- xml.attrib
- except AttributeError:
+ xml.attrib['machineIdentifier']
+ except (AttributeError, KeyError):
# No connection, delete the one we just tested
del PMS['connections'][0]
if len(PMS['connections']) > 0:
diff --git a/resources/lib/PlexFunctions.py b/resources/lib/PlexFunctions.py
index 95af2655..a22115bb 100644
--- a/resources/lib/PlexFunctions.py
+++ b/resources/lib/PlexFunctions.py
@@ -450,7 +450,8 @@ def GetMachineIdentifier(url):
"""
xml = downloadutils.DownloadUtils().downloadUrl('%s/identity' % url,
authenticate=False,
- verifySSL=False)
+ verifySSL=False,
+ timeout=4)
try:
machineIdentifier = xml.attrib['machineIdentifier']
except (AttributeError, KeyError):
diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py
index 5dff1bff..005de1b2 100644
--- a/resources/lib/entrypoint.py
+++ b/resources/lib/entrypoint.py
@@ -74,52 +74,13 @@ def plexCompanion(fullurl, params):
def chooseServer():
"""
- Lets user choose from list of PMS (signs out & signs in)
+ Lets user choose from list of PMS
"""
- string = xbmcaddon.Addon().getLocalizedString
utils.logMsg(title, "Choosing PMS server requested, starting", 1)
- dialog = xbmcgui.Dialog()
- # Resetting, please wait
- dialog.notification(
- heading=addonName,
- message=string(39207),
- icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
- time=3000,
- sound=False)
- # Pause library sync thread - user needs to be auth in order to sync
- utils.window('suspend_LibraryThread', value='true')
- # Wait max for 5 seconds for all lib scans to shutdown
- counter = 0
- while utils.window('emby_dbScan') == 'true':
- if counter > 100:
- # Failed to reset PMS and plex.tv connects. Try to restart Kodi.
- dialog.ok(addonName, string(39208))
- # Resuming threads, just in case
- utils.window('suspend_LibraryThread', clear=True)
- utils.logMsg(title, "Could not stop library sync, aborting", -1)
- return
- counter += 1
- xbmc.sleep(50)
- utils.logMsg(title, "Successfully stopped library sync", 1)
-
- # Log out currently signed in user:
- utils.window('emby_serverStatus', value="401")
- # Above method needs to have run its course! Hence wait
- counter = 0
- while utils.window('emby_serverStatus') == "401":
- if counter > 100:
- dialog.ok(addonName,
- string(39208))
- utils.logMsg(title, "Could not sign out, aborting PMS", -1)
- return
- counter += 1
- xbmc.sleep(50)
- # Suspend the user client during procedure
- utils.window('suspend_Userclient', value='true')
import initialsetup
setup = initialsetup.InitialSetup()
- server = setup.PickPMS()
+ server = setup.PickPMS(showDialog=True)
if server is None:
utils.logMsg('We did not connect to a new PMS, aborting', -1)
utils.window('suspend_Userclient', clear=True)
@@ -127,85 +88,45 @@ def chooseServer():
return
setup.WritePMStoSettings(server)
- # Request lib sync to get user view data (e.g. watched/unwatched)
- utils.window('plex_runLibScan', value='full')
- # Restart user client
- utils.window('suspend_Userclient', clear=True)
- utils.logMsg(title, "Choosing new PMS complete", 0)
- # And do NOT clear suspend_LibraryThread flag, that needs to be done in
- # service.py
+ if not __LogOut():
+ return
-def reConnect():
- """
- Triggers login to plex.tv and re-authorization
- """
- string = xbmcaddon.Addon().getLocalizedString
- utils.logMsg(title, "Connection resets requested", 0)
- dialog = xbmcgui.Dialog()
- # Resetting, please wait
- dialog.notification(
+ # Log in again
+ __LogIn()
+ utils.logMsg(title, "Choosing new PMS complete", 1)
+ # ' connected'
+ xbmcgui.Dialog().notification(
heading=addonName,
- message=string(39207),
+ message='%s %s' % (server['name'],
+ xbmcaddon.Addon().getLocalizedString(39220)),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
time=3000,
sound=False)
- # Pause library sync thread - user needs to be auth in order to sync
- utils.window('suspend_LibraryThread', value='true')
- # Wait max for 25 seconds for all lib scans to finish
- counter = 0
- while utils.window('emby_dbScan') == 'true':
- if counter > 500:
- # Failed to reset PMS and plex.tv connects. Try to restart Kodi.
- dialog.ok(addonName,
- string(39208))
- # Resuming threads, just in case
- utils.window('suspend_LibraryThread', clear=True)
- utils.logMsg(title, "Could not stop library sync, aborting", -1)
- return
- counter += 1
- xbmc.sleep(50)
- utils.logMsg(title, "Successfully stopped library sync", 0)
- # Delete plex credentials in settings
- utils.settings('myplexlogin', value="true")
- utils.settings('plexLogin', value="")
- utils.settings('plexToken', value=""),
- utils.settings('plexid', value="")
- utils.settings('plexHomeSize', value="1")
- utils.settings('plexAvatar', value="")
+def togglePlexTV():
+ if utils.settings('plexToken'):
+ utils.logMsg(title, 'Reseting plex.tv credentials in settings', 1)
+ utils.settings('plexLogin', value="")
+ utils.settings('plexToken', value=""),
+ utils.settings('plexid', value="")
+ utils.settings('plexHomeSize', value="1")
+ utils.settings('plexAvatar', value="")
+ utils.settings('plex_status', value="Not logged in to plex.tv")
- # Reset connection details
- utils.settings('plex_machineIdentifier', value="")
- utils.settings('plex_servername', value="")
- utils.settings('https', value="")
- utils.settings('ipaddress', value="")
- utils.settings('port', value="")
-
- # Log out currently signed in user:
- utils.window('emby_serverStatus', value="401")
-
- # Above method needs to have run its course! Hence wait
- counter = 0
- while utils.window('emby_serverStatus') == "401":
- if counter > 100:
- dialog.ok(addonName,
- string(39208))
- utils.logMsg(title, "Could not sign out, aborting", -1)
- return
- counter += 1
- xbmc.sleep(50)
- # Suspend the user client during procedure
- utils.window('suspend_Userclient', value='true')
-
- import initialsetup
- initialsetup.InitialSetup().setup(forcePlexTV=True)
- # Request lib sync to get user view data (e.g. watched/unwatched)
- utils.window('plex_runLibScan', value='full')
- # Restart user client
- utils.window('suspend_Userclient', clear=True)
- utils.logMsg(title, "Complete reconnection to plex.tv and PMS complete", 0)
+ utils.window('plex_token', clear=True)
+ utils.window('plex_username', clear=True)
+ else:
+ utils.logMsg(title, 'Login to plex.tv', 1)
+ import initialsetup
+ initialsetup.InitialSetup().PlexTVSignIn()
+ xbmcgui.Dialog().notification(
+ heading=addonName,
+ message=xbmcaddon.Addon().getLocalizedString(39221),
+ icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
+ time=3000,
+ sound=False)
def PassPlaylist(xml, resume=None):
@@ -565,30 +486,15 @@ def switchPlexUser():
# Delete any userimages. Since there's always only 1 user: position = 0
# position = 0
# utils.window('EmbyAdditionalUserImage.%s' % position, clear=True)
- utils.logMsg("entrypoint switchPlexUser",
- "Plex home user switch requested", 0)
- # Pause library sync thread - user needs to be auth in order to sync
- utils.window('suspend_LibraryThread', value='true')
- # Wait to ensure that any sync already going on has finished
- counter = 0
- while utils.window('emby_dbScan') == 'true':
- if counter > 100:
- # Something went wrong, aborting
- # Resuming threads, just in case
- utils.window('suspend_LibraryThread', clear=True)
- # Abort reConnection
- return
- counter += 1
- xbmc.sleep(50)
+ utils.logMsg(title, "Plex home user switch requested", 0)
+ if not __LogOut(user=True):
+ return
- # First remove playlists
+ # First remove playlists of old user
utils.deletePlaylists()
# Remove video nodes
utils.deleteNodes()
- # Log out currently signed in user:
- utils.window('emby_serverStatus', value="401")
- # Request lib sync to get user view data (e.g. watched/unwatched)
- utils.window('plex_runLibScan', value='full')
+ __LogIn()
##### THEME MUSIC/VIDEOS #####
@@ -1707,3 +1613,122 @@ def watchlater():
handle=int(sys.argv[1]),
cacheToDisc=True if utils.settings('enableTextureCache') == 'true'
else False)
+
+
+def enterPMS():
+ """
+ Opens dialogs for the user the plug in the PMS details
+ """
+ dialog = xbmcgui.Dialog()
+ string = xbmcaddon.Addon().getLocalizedString
+ # "Enter your Plex Media Server's IP or URL. Examples are:"
+ dialog.ok(addonName,
+ string(39215),
+ '192.168.1.2',
+ 'plex.myServer.org')
+ ip = dialog.input("Enter PMS IP or URL")
+ if ip == '':
+ return
+ port = dialog.input("Enter PMS port", '32400', xbmcgui.INPUT_NUMERIC)
+ if port == '':
+ return
+ url = '%s:%s' % (ip, port)
+ # "Does your Plex Media Server support SSL connections?
+ # (https instead of http)"
+ https = dialog.yesno(addonName, string(39217))
+ if https:
+ url = 'https://%s' % url
+ else:
+ url = 'http://%s' % url
+ https = 'true' if https else 'false'
+
+ machineIdentifier = PlexFunctions.GetMachineIdentifier(url)
+ if machineIdentifier is None:
+ # "Error contacting url
+ # Abort (Yes) or save address anyway (No)"
+ if dialog.yesno(addonName, '%s %s. %s'
+ % (string(39218), url, string(39219))):
+ return
+ else:
+ utils.settings('plex_machineIdentifier', '')
+ else:
+ utils.settings('plex_machineIdentifier', machineIdentifier)
+ utils.logMsg(title, 'Setting new PMS to: https %s, ip %s, port %s, '
+ 'machineIdentifier: %s'
+ % (https, ip, port, machineIdentifier), 1)
+ utils.settings('https', value=https)
+ utils.settings('ipaddress', value=ip)
+ utils.settings('port', value=port)
+
+ # Sign out to trigger new login
+ if __LogOut():
+ # Only login again if logout was successful
+ __LogIn()
+
+
+def __LogIn(user=False):
+ """
+ Resets (clears) window properties to enable (re-)login:
+ suspend_Userclient
+ plex_runLibScan: set to 'full' to trigger lib sync
+
+ user=False: user has NOT been signed out before
+ suspend_LibraryThread is cleared in service.py if user was signed out!
+ """
+ utils.window('plex_runLibScan', value='full')
+ # Restart user client
+ utils.window('suspend_Userclient', clear=True)
+ if user is False:
+ utils.window('suspend_LibraryThread', clear=True)
+
+
+def __LogOut(user=False):
+ """
+ Finishes lib scans, logs out user. The following window attributes are set:
+ suspend_LibraryThread: 'true'
+ If user=True, then the user will also be logged out
+ suspend_Userclient: 'true'
+
+ Returns True if successfully signed out, False otherwise
+ """
+ string = xbmcaddon.Addon().getLocalizedString
+ dialog = xbmcgui.Dialog()
+ # Resetting, please wait
+ dialog.notification(
+ heading=addonName,
+ message=string(39207),
+ icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
+ time=3000,
+ sound=False)
+ # Pause library sync thread
+ utils.window('suspend_LibraryThread', value='true')
+ # Wait max for 10 seconds for all lib scans to shutdown
+ counter = 0
+ while utils.window('emby_dbScan') == 'true':
+ if counter > 200:
+ # Failed to reset PMS and plex.tv connects. Try to restart Kodi.
+ dialog.ok(addonName, string(39208))
+ # Resuming threads, just in case
+ utils.window('suspend_LibraryThread', clear=True)
+ utils.logMsg(title, "Could not stop library sync, aborting", -1)
+ return False
+ counter += 1
+ xbmc.sleep(50)
+ utils.logMsg(title, "Successfully stopped library sync", 1)
+
+ if user:
+ # Log out currently signed in user:
+ utils.window('emby_serverStatus', value="401")
+ # Above method needs to have run its course! Hence wait
+ counter = 0
+ while utils.window('emby_serverStatus') == "401":
+ if counter > 100:
+ # 'Failed to reset PKC. Try to restart Kodi.'
+ dialog.ok(addonName, string(39208))
+ utils.logMsg(title, "Could not sign out user, aborting", -1)
+ return False
+ counter += 1
+ xbmc.sleep(50)
+ # Suspend the user client during procedure
+ utils.window('suspend_Userclient', value='true')
+ return True
diff --git a/resources/lib/initialsetup.py b/resources/lib/initialsetup.py
index f6638da5..0648bfbb 100644
--- a/resources/lib/initialsetup.py
+++ b/resources/lib/initialsetup.py
@@ -59,25 +59,31 @@ class InitialSetup():
def CheckPlexTVSignIn(self):
"""
Checks existing connection to plex.tv. If not, triggers sign in
+
+ Returns True if signed in, False otherwise
"""
- chk = self.plx.CheckConnection('plex.tv', self.plexToken)
+ answer = True
+ chk = self.plx.CheckConnection('plex.tv', token=self.plexToken)
if chk in (401, 403):
# HTTP Error: unauthorized. Token is no longer valid
- self.logMsg('plex.tv connection returned HTTP %s' % chk, 1)
+ self.logMsg('plex.tv connection returned HTTP %s' % str(chk), 1)
# Delete token in the settings
utils.settings('plexToken', value='')
+ utils.settings('plexLogin', value='')
# Could not login, please try again
self.dialog.ok(self.addonName,
self.string(39009))
- self.PlexTVSignIn()
+ answer = self.PlexTVSignIn()
elif chk is False or chk >= 400:
# Problems connecting to plex.tv. Network or internet issue?
- self.logMsg('plex.tv connection returned HTTP %s'
- % str(chk), 1)
+ self.logMsg('Problems connecting to plex.tv; connection returned '
+ 'HTTP %s' % str(chk), 1)
self.dialog.ok(self.addonName,
self.string(39010))
+ answer = False
else:
self.logMsg('plex.tv connection with token successful', 1)
+ utils.settings('plex_status', value='Logged in to plex.tv')
# Refresh the info from Plex.tv
xml = self.doUtils('https://plex.tv/users/account',
authenticate=False,
@@ -94,6 +100,7 @@ class InitialSetup():
utils.settings(
'plexHomeSize', value=xml.attrib.get('homeSize', '1'))
self.logMsg('Updated Plex info from plex.tv', 1)
+ return answer
def CheckPMS(self):
"""
@@ -106,7 +113,8 @@ class InitialSetup():
not set before
"""
answer = True
- chk = self.plx.CheckConnection(self.server, verifySSL=False)
+ chk = self.plx.CheckConnection(self.server,
+ verifySSL=False)
if chk is False:
self.logMsg('Could not reach PMS %s' % self.server, -1)
answer = False
@@ -129,9 +137,41 @@ class InitialSetup():
answer = False
return answer
- def PickPMS(self):
+ def __getServerList(self):
"""
- Searches for PMS and lets user pick one
+ Returns a list of servers from GDM and possibly plex.tv
+ """
+ self.plx.discoverPMS(xbmc.getIPAddress(),
+ plexToken=self.plexToken)
+ serverlist = self.plx.returnServerList(self.plx.g_PMS)
+ self.logMsg('PMS serverlist: %s' % serverlist, 2)
+ return serverlist
+
+ def __checkServerCon(self, server):
+ """
+ Checks for server's connectivity. Returns CheckConnection result
+ """
+ # Re-direct via plex if remote - will lead to the correct SSL
+ # certificate
+ if server['local'] == '1':
+ url = '%s://%s:%s' \
+ % (server['scheme'], server['ip'], server['port'])
+ # Deactive SSL verification if the server is local!
+ verifySSL = False
+ else:
+ url = server['baseURL']
+ verifySSL = None
+ chk = self.plx.CheckConnection(url,
+ token=server['accesstoken'],
+ verifySSL=verifySSL)
+ return chk
+
+ def PickPMS(self, showDialog=False):
+ """
+ Searches for PMS in local Lan and optionally (if self.plexToken set)
+ also on plex.tv
+ showDialog=True: let the user pick one
+ showDialog=False: automatically pick PMS based on machineIdentifier
Returns the picked PMS' detail as a dict:
{
@@ -150,24 +190,98 @@ class InitialSetup():
or None if unsuccessful
"""
+ server = None
+ # If no server is set, let user choose one
+ if not self.server or not self.serverid:
+ showDialog = True
+ if showDialog is True:
+ server = self.__UserPickPMS()
+ else:
+ server = self.__AutoPickPMS()
+ return server
+
+ def __AutoPickPMS(self):
+ """
+ Will try to pick PMS based on machineIdentifier saved in file settings
+ but only once
+
+ Returns server or None if unsuccessful
+ """
+ httpsUpdated = False
+ checkedPlexTV = False
+ server = None
+ while True:
+ if httpsUpdated is False:
+ serverlist = self.__getServerList()
+ for item in serverlist:
+ if item.get('machineIdentifier') == self.serverid:
+ server = item
+ if server is None:
+ name = utils.settings('plex_servername')
+ self.logMsg('The PMS you have used before with a unique '
+ 'machineIdentifier of %s and name %s is '
+ 'offline' % (self.serverid, name), -1)
+ # "PMS xyz offline"
+ self.dialog.notification(self.addonName,
+ '%s %s'
+ % (name, self.string(39213)),
+ xbmcgui.NOTIFICATION_ERROR,
+ 7000,
+ False)
+ return
+ chk = self.__checkServerCon(server)
+ if chk == 504 and httpsUpdated is False:
+ # Not able to use HTTP, try HTTPs for now
+ server['scheme'] = 'https'
+ httpsUpdated = True
+ continue
+ if chk == 401:
+ self.logMsg('Not yet authorized for Plex server %s'
+ % server['name'], -1)
+ if self.CheckPlexTVSignIn() is True:
+ if checkedPlexTV is False:
+ # Try again
+ checkedPlexTV = True
+ httpsUpdated = False
+ continue
+ else:
+ self.logMsg('Not authorized even though we are signed '
+ ' in to plex.tv correctly', -1)
+ self.dialog.ok(self.addonName, '%s %s'
+ % self.string(39214) + server['name'])
+ return
+ else:
+ return
+ # Problems connecting
+ elif chk >= 400 or chk is False:
+ self.logMsg('Problems connecting to server %s. chk is %s'
+ % (server['name'], chk), -1)
+ return
+ self.logMsg('We found a server to automatically connect to: %s'
+ % server['name'], 1)
+ return server
+
+ def __UserPickPMS(self):
+ """
+ Lets user pick his/her PMS from a list
+
+ Returns server or None if unsuccessful
+ """
httpsUpdated = False
while True:
if httpsUpdated is False:
- # Populate g_PMS variable with the found Plex servers
- self.plx.discoverPMS(xbmc.getIPAddress(),
- plexToken=self.plexToken)
- serverlist = self.plx.returnServerList(self.plx.g_PMS)
- self.logMsg('PMS serverlist: %s' % serverlist, 2)
+ serverlist = self.__getServerList()
# Exit if no servers found
if len(serverlist) == 0:
+ self.logMsg('No plex media servers found!', -1)
self.dialog.ok(self.addonName, self.string(39011))
- server = None
- break
+ return
# Get a nicer list
dialoglist = []
for server in serverlist:
if server['local'] == '1':
- # server is in the same network as client. Add "local"
+ # server is in the same network as client.
+ # Add"local"
msg = self.string(39022)
else:
# Add 'remote'
@@ -183,22 +297,9 @@ class InitialSetup():
% (server['name'], msg))
# Let user pick server from a list
resp = self.dialog.select(self.string(39012), dialoglist)
+
server = serverlist[resp]
- # Re-direct via plex if remote - will lead to the correct SSL
- # certificate
- if server['local'] == '1':
- url = '%s://%s:%s' \
- % (server['scheme'], server['ip'], server['port'])
- else:
- url = server['baseURL']
- # Deactive SSL verification if the server is local!
- if server['local'] == '1':
- verifySSL = False
- else:
- verifySSL = None
- chk = self.plx.CheckConnection(url,
- server['accesstoken'],
- verifySSL=verifySSL)
+ chk = self.__checkServerCon(server)
if chk == 504 and httpsUpdated is False:
# Not able to use HTTP, try HTTPs for now
serverlist[resp]['scheme'] = 'https'
@@ -206,15 +307,15 @@ class InitialSetup():
continue
httpsUpdated = False
if chk == 401:
- # Not yet authorized for Plex server
+ self.logMsg('Not yet authorized for Plex server %s'
+ % server['name'], -1)
# Please sign in to plex.tv
self.dialog.ok(self.addonName,
self.string(39013) + server['name'],
self.string(39014))
if self.PlexTVSignIn() is False:
# Exit while loop if user cancels
- server = None
- break
+ return
# Problems connecting
elif chk >= 400 or chk is False:
# Problems connecting to server. Pick another server?
@@ -222,12 +323,10 @@ class InitialSetup():
self.string(39015))
# Exit while loop if user chooses No
if not answ:
- server = None
- break
+ return
# Otherwise: connection worked!
else:
- break
- return server
+ return server
def WritePMStoSettings(self, server):
"""
@@ -278,7 +377,7 @@ class InitialSetup():
% (server['machineIdentifier'], server['ip'],
server['port'], server['scheme']), 0)
- def setup(self, forcePlexTV=False, chooseServer=False):
+ def setup(self):
"""
Initial setup. Run once upon startup.
@@ -291,17 +390,18 @@ class InitialSetup():
# Optionally sign into plex.tv. Will not be called on very first run
# as plexToken will be ''
+ utils.settings('plex_status', value='Not logged in to plex.tv')
if self.plexToken and self.myplexlogin:
self.CheckPlexTVSignIn()
# If a Plex server IP has already been set
# return only if the right machine identifier is found
- getNewPMS = False
+ getNewIP = False
if self.server:
self.logMsg("PMS is already set: %s. Checking now..."
% self.server, 0)
- getNewPMS = not self.CheckPMS()
- if getNewPMS is False:
+ getNewIP = not self.CheckPMS()
+ if getNewIP is False:
self.logMsg("Using PMS %s with machineIdentifier %s"
% (self.server, self.serverid), 0)
return
@@ -312,16 +412,21 @@ class InitialSetup():
self.PlexTVSignIn()
server = self.PickPMS()
- if server is None:
- # Enter Kodi settings instead
- xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
+ if server is not None:
+ goToSettings = False
+ # Write our chosen server to Kodi settings file
+ self.WritePMStoSettings(server)
+ else:
+ goToSettings = True
+
+ # User already answered the installation questions
+ if utils.settings('InstallQuestionsAnswered') == 'true':
+ if goToSettings:
+ xbmc.executebuiltin(
+ 'Addon.OpenSettings(plugin.video.plexkodiconnect)')
return
- # Write our chosen server to Kodi settings file
- self.WritePMStoSettings(server)
-
# Additional settings where the user needs to choose
- goToSettings = False
# Direct paths (\\NAS\mymovie.mkv) or addon (http)?
if dialog.yesno(self.addonName,
string(39027),
@@ -365,10 +470,13 @@ class InitialSetup():
# Download additional art from FanArtTV
if dialog.yesno(heading=self.addonName,
- line1=string(39016)):
+ line1=string(39061)):
self.logMsg("User opted to use FanArtTV", 1)
utils.settings('FanartTV', value="true")
+ # Make sure that we only ask these questions upon first installation
+ utils.settings('InstallQuestionsAnswered', value='true')
+
if goToSettings is False:
# Open Settings page now? You will need to restart!
goToSettings = dialog.yesno(heading=self.addonName,
diff --git a/resources/settings.xml b/resources/settings.xml
index bddb070b..9900d0ef 100644
--- a/resources/settings.xml
+++ b/resources/settings.xml
@@ -1,37 +1,30 @@
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
-
+
+
+
-
+