PlexKodiConnect/resources/lib/initialsetup.py

205 lines
8.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#################################################################################################
import xbmc
import xbmcgui
import xbmcaddon
import utils
import clientinfo
import downloadutils
import userclient
2015-12-27 22:14:06 +11:00
import PlexAPI
#################################################################################################
2016-01-27 03:20:13 +11:00
@utils.logging
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()
self.userClient = userclient.UserClient()
2015-12-27 22:14:06 +11:00
self.plx = PlexAPI.PlexAPI()
def setup(self):
2016-01-15 00:47:34 +11:00
"""
Initial setup. Run once upon startup.
Check server, user, direct paths, music, direct stream if not direct
path.
"""
##### SERVER INFO #####
2016-01-14 20:20:19 +11:00
self.logMsg("Initial setup called.", 0)
server = self.userClient.getServer()
2015-12-27 22:14:06 +11:00
clientId = self.clientInfo.getDeviceId()
serverid = self.userClient.getServerId()
2016-01-15 00:47:34 +11:00
myplexlogin, plexhome, plexLogin, plexToken = self.plx.GetPlexLoginFromSettings()
2015-12-27 22:14:06 +11:00
# Optionally sign into plex.tv. Will not be called on very first run
2016-01-15 00:47:34 +11:00
# as plexToken will be ''
2015-12-27 22:14:06 +11:00
if plexToken and myplexlogin == 'true':
chk = self.plx.CheckConnection('plex.tv', plexToken)
# HTTP Error: unauthorized
if chk == 401:
dialog = xbmcgui.Dialog()
dialog.ok(
self.addonName,
'Could not login to plex.tv.',
'Please try signing in again.'
)
2016-01-14 20:20:19 +11:00
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
plexToken = result['token']
2016-01-15 00:47:34 +11:00
elif chk is False or chk >= 400:
2015-12-27 22:14:06 +11:00
dialog = xbmcgui.Dialog()
dialog.ok(
self.addonName,
'Problems connecting to plex.tv.',
'Network or internet issue?'
)
# If a Plex server IP has already been set, return.
if server:
2016-01-14 20:20:19 +11:00
self.logMsg("Server is already set.", 0)
2016-01-15 00:47:34 +11:00
self.logMsg("url: %s, Plex machineIdentifier: %s"
% (server, serverid), 0)
return
2015-12-27 22:14:06 +11:00
# If not already retrieved myplex info, optionally let user sign in
# to plex.tv.
if not plexToken and myplexlogin == 'true':
2016-01-14 20:20:19 +11:00
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
plexToken = result['token']
2015-12-27 22:14:06 +11:00
# Get g_PMS list of servers (saved to plx.g_PMS)
serverNum = 1
while serverNum > 0:
if plexToken:
tokenDict = {'MyPlexToken': plexToken}
else:
tokenDict = {}
# Populate g_PMS variable with the found Plex servers
2016-01-15 00:47:34 +11:00
self.plx.discoverPMS(clientId,
None,
xbmc.getIPAddress(),
tokenDict=tokenDict)
self.logMsg("Result of setting g_PMS variable: %s"
% self.plx.g_PMS, 2)
2015-12-27 22:14:06 +11:00
isconnected = False
serverlist = self.plx.returnServerList(clientId, self.plx.g_PMS)
# Let user pick server from a list
# Get a nicer list
dialoglist = []
# Exit if no servers found
serverNum = len(serverlist)
if serverNum == 0:
break
2015-12-27 22:14:06 +11:00
for server in serverlist:
2016-01-14 20:20:19 +11:00
if server['local'] == '1':
# server is in the same network as client
dialoglist.append(str(server['name']) + ' (nearby)')
else:
dialoglist.append(str(server['name']))
2015-12-27 22:14:06 +11:00
dialog = xbmcgui.Dialog()
resp = dialog.select(
2016-01-15 00:47:34 +11:00
'Choose your Plex server',
2015-12-27 22:14:06 +11:00
dialoglist)
server = serverlist[resp]
activeServer = server['machineIdentifier']
url = server['scheme'] + '://' + server['ip'] + ':' + \
server['port']
# Deactive SSL verification if the server is local!
if server['local'] == '1':
self.addon.setSetting('sslverify', 'false')
2016-01-15 00:47:34 +11:00
self.logMsg("Setting SSL verify to false, because server is "
"local", 1)
else:
self.addon.setSetting('sslverify', 'true')
2016-01-15 00:47:34 +11:00
self.logMsg("Setting SSL verify to true, because server is "
"not local", 1)
2015-12-27 22:14:06 +11:00
chk = self.plx.CheckConnection(url, server['accesstoken'])
# Unauthorized
if chk == 401:
2016-01-15 00:47:34 +11:00
dialog.ok(self.addonName,
'Not yet authorized for Plex server %s'
% str(server['name']),
'Please sign in to plex.tv.')
2016-01-14 20:20:19 +11:00
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
plexToken = result['token']
else:
# Exit while loop if user cancels
2015-12-27 22:14:06 +11:00
break
# Problems connecting
2016-01-15 00:47:34 +11:00
elif chk >= 400 or chk is False:
2015-12-27 22:14:06 +11:00
dialog = xbmcgui.Dialog()
2016-01-15 00:47:34 +11:00
resp = dialog.yesno(self.addonName,
'Problems connecting to server.',
'Pick another server?')
2015-12-27 22:14:06 +11:00
# Exit while loop if user chooses No
if not resp:
break
# Otherwise: connection worked!
else:
isconnected = True
break
if not isconnected:
# Enter Kodi settings instead
2016-01-15 00:47:34 +11:00
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
return
2015-12-27 22:14:06 +11:00
# Write to Kodi settings file
2016-01-15 00:47:34 +11:00
self.addon.setSetting('plex_machineIdentifier', activeServer)
2015-12-27 22:14:06 +11:00
self.addon.setSetting('ipaddress', server['ip'])
self.addon.setSetting('port', server['port'])
if server['scheme'] == 'https':
self.addon.setSetting('https', 'true')
else:
2015-12-27 22:14:06 +11:00
self.addon.setSetting('https', 'false')
2016-01-15 00:47:34 +11:00
self.logMsg("Wrote to Kodi user settings file:", 0)
self.logMsg("PMS machineIdentifier: %s, ip: %s, port: %s, https: %s "
% (activeServer, server['ip'], server['port'],
server['scheme']), 0)
##### ADDITIONAL PROMPTS #####
dialog = xbmcgui.Dialog()
directPaths = dialog.yesno(
2015-12-28 00:42:54 +11:00
heading="%s: Playback Mode" % self.addonName,
line1=(
"Caution! If you choose Native mode, you "
2015-12-28 00:42:54 +11:00
"will probably lose access to certain Plex "
"features."),
nolabel="Addon (Default)",
yeslabel="Native (Direct Paths)")
if directPaths:
self.logMsg("User opted to use direct paths.", 1)
utils.settings('useDirectPaths', value="1")
musicDisabled = dialog.yesno(
2015-12-28 00:42:54 +11:00
heading="%s: Music Library" % self.addonName,
line1="Disable Plex music library?")
if musicDisabled:
2015-12-28 00:42:54 +11:00
self.logMsg("User opted to disable Plex music library.", 1)
utils.settings('enableMusic', value="false")
else:
# Only prompt if the user didn't select direct paths for videos
if not directPaths:
musicAccess = dialog.yesno(
2015-12-28 00:42:54 +11:00
heading="%s: Music Library" % self.addonName,
line1=(
"Direct stream the music library? Select "
"this option only if you plan on listening "
"to music outside of your network."))
if musicAccess:
self.logMsg("User opted to direct stream music.", 1)
utils.settings('streamMusic', value="true")