2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
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-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2015-12-25 06:51:47 +11:00
|
|
|
class InitialSetup():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
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()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-04 23:34:30 +11:00
|
|
|
def setup(self, forcePlexTV=False):
|
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.
|
|
|
|
"""
|
2016-03-07 23:01:45 +11:00
|
|
|
string = xbmcaddon.Addon().getLocalizedString
|
2016-02-20 06:03:06 +11:00
|
|
|
# SERVER INFO #####
|
2016-01-14 20:20:19 +11:00
|
|
|
self.logMsg("Initial setup called.", 0)
|
2015-12-25 06:51:47 +11:00
|
|
|
server = self.userClient.getServer()
|
2015-12-27 22:14:06 +11:00
|
|
|
clientId = self.clientInfo.getDeviceId()
|
2016-02-20 06:03:06 +11:00
|
|
|
serverid = utils.settings('plex_machineIdentifier')
|
2016-03-04 23:34:30 +11:00
|
|
|
# Get Plex credentials from settings file, if they exist
|
|
|
|
plexdict = self.plx.GetPlexLoginFromSettings()
|
|
|
|
myplexlogin = plexdict['myplexlogin']
|
|
|
|
plexLogin = plexdict['plexLogin']
|
|
|
|
plexToken = plexdict['plexToken']
|
|
|
|
plexid = plexdict['plexid']
|
|
|
|
self.logMsg('Plex info retrieved from settings: %s' % plexdict, 1)
|
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
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 ''
|
2016-03-04 23:34:30 +11:00
|
|
|
if (plexToken and myplexlogin == 'true' and forcePlexTV is False):
|
2015-12-27 22:14:06 +11:00
|
|
|
chk = self.plx.CheckConnection('plex.tv', plexToken)
|
2016-03-04 23:34:30 +11:00
|
|
|
# HTTP Error: unauthorized. Token is no longer valid
|
2015-12-27 22:14:06 +11:00
|
|
|
if chk == 401:
|
2016-03-04 23:34:30 +11:00
|
|
|
# Delete token in the settings
|
|
|
|
utils.settings('plexToken', value='')
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not login, please try again
|
2015-12-27 22:14:06 +11:00
|
|
|
dialog.ok(
|
|
|
|
self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
string(39009).encode('utf-8')
|
2015-12-27 22:14:06 +11:00
|
|
|
)
|
2016-01-14 20:20:19 +11:00
|
|
|
result = self.plx.PlexTvSignInWithPin()
|
|
|
|
if result:
|
|
|
|
plexLogin = result['username']
|
|
|
|
plexToken = result['token']
|
2016-03-04 23:34:30 +11:00
|
|
|
plexid = result['plexid']
|
2016-01-15 00:47:34 +11:00
|
|
|
elif chk is False or chk >= 400:
|
2016-03-04 23:34:30 +11:00
|
|
|
# Problems connecting to plex.tv. Network or internet issue?
|
2015-12-27 22:14:06 +11:00
|
|
|
dialog.ok(
|
|
|
|
self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
string(39010).encode('utf-8')
|
2015-12-27 22:14:06 +11:00
|
|
|
)
|
|
|
|
# If a Plex server IP has already been set, return.
|
2016-03-04 23:34:30 +11:00
|
|
|
if server and forcePlexTV is False:
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
return
|
2015-12-27 22:14:06 +11:00
|
|
|
|
|
|
|
# If not already retrieved myplex info, optionally let user sign in
|
2016-03-04 23:34:30 +11:00
|
|
|
# to plex.tv. This DOES get called on very first install run
|
|
|
|
if ((not plexToken and myplexlogin == 'true') or forcePlexTV):
|
2016-01-14 20:20:19 +11:00
|
|
|
result = self.plx.PlexTvSignInWithPin()
|
|
|
|
if result:
|
|
|
|
plexLogin = result['username']
|
|
|
|
plexToken = result['token']
|
2016-03-04 23:34:30 +11:00
|
|
|
plexid = result['plexid']
|
2015-12-27 22:14:06 +11:00
|
|
|
# Get g_PMS list of servers (saved to plx.g_PMS)
|
2016-03-08 03:11:54 +11:00
|
|
|
httpsUpdated = False
|
2016-01-30 06:07:21 +11:00
|
|
|
while True:
|
2016-03-08 03:11:54 +11:00
|
|
|
if httpsUpdated is False:
|
|
|
|
tokenDict = {'MyPlexToken': plexToken} if plexToken else {}
|
|
|
|
# Populate g_PMS variable with the found Plex servers
|
|
|
|
self.plx.discoverPMS(clientId,
|
|
|
|
None,
|
|
|
|
xbmc.getIPAddress(),
|
|
|
|
tokenDict=tokenDict)
|
|
|
|
self.logMsg("Result of setting g_PMS variable: %s"
|
|
|
|
% self.plx.g_PMS, 1)
|
|
|
|
isconnected = False
|
|
|
|
serverlist = self.plx.returnServerList(
|
|
|
|
clientId, self.plx.g_PMS)
|
|
|
|
self.logMsg('PMS serverlist: %s' % serverlist)
|
|
|
|
# Let user pick server from a list
|
|
|
|
# Get a nicer list
|
|
|
|
dialoglist = []
|
|
|
|
# Exit if no servers found
|
|
|
|
if len(serverlist) == 0:
|
|
|
|
dialog.ok(
|
|
|
|
self.addonName,
|
|
|
|
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(
|
|
|
|
server['name'].encode('utf-8')
|
|
|
|
+ string(39022).encode('utf-8'))
|
|
|
|
else:
|
|
|
|
dialoglist.append(server['name'].encode('utf-8'))
|
|
|
|
resp = dialog.select(string(39012).encode('utf-8'), dialoglist)
|
2015-12-27 22:14:06 +11:00
|
|
|
server = serverlist[resp]
|
|
|
|
activeServer = server['machineIdentifier']
|
|
|
|
url = server['scheme'] + '://' + server['ip'] + ':' + \
|
|
|
|
server['port']
|
2015-12-27 23:20:53 +11:00
|
|
|
# Deactive SSL verification if the server is local!
|
|
|
|
if server['local'] == '1':
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('sslverify', 'false')
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("Setting SSL verify to false, because server is "
|
|
|
|
"local", 1)
|
2015-12-27 23:20:53 +11:00
|
|
|
else:
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('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'])
|
2016-03-08 03:11:54 +11:00
|
|
|
if chk == 504 and httpsUpdated is False:
|
|
|
|
# Not able to use HTTP, try HTTPs for now
|
|
|
|
serverlist[resp]['scheme'] = 'https'
|
|
|
|
httpsUpdated = True
|
|
|
|
continue
|
|
|
|
httpsUpdated = False
|
2015-12-27 22:14:06 +11:00
|
|
|
if chk == 401:
|
2016-03-04 23:34:30 +11:00
|
|
|
# Not yet authorized for Plex server
|
|
|
|
# Please sign in to plex.tv
|
2016-01-15 00:47:34 +11:00
|
|
|
dialog.ok(self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
string(39013).encode('utf-8')
|
|
|
|
+ server['name'].encode('utf-8'),
|
|
|
|
string(39014).encode('utf-8'))
|
2016-01-14 20:20:19 +11:00
|
|
|
result = self.plx.PlexTvSignInWithPin()
|
|
|
|
if result:
|
|
|
|
plexLogin = result['username']
|
|
|
|
plexToken = result['token']
|
2016-03-04 23:34:30 +11:00
|
|
|
plexid = result['plexid']
|
2016-01-14 20:20:19 +11:00
|
|
|
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:
|
2016-03-04 23:34:30 +11:00
|
|
|
# Problems connecting to server. Pick another server?
|
2016-03-07 23:01:45 +11:00
|
|
|
resp = dialog.yesno(self.addonName,
|
|
|
|
string(39015).encode('utf-8'))
|
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-03-04 23:34:30 +11:00
|
|
|
if dialog.yesno(
|
|
|
|
heading=self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
line1=string(39016).encode('utf-8')):
|
2016-03-04 23:34:30 +11:00
|
|
|
self.logMsg("User opted to disable Plex music library.", 1)
|
|
|
|
utils.settings('enableMusic', value="false")
|
2016-01-15 00:47:34 +11:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
|
2015-12-25 06:51:47 +11:00
|
|
|
return
|
2015-12-27 22:14:06 +11:00
|
|
|
# Write to Kodi settings file
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('plex_machineIdentifier', activeServer)
|
|
|
|
utils.settings('ipaddress', server['ip'])
|
|
|
|
utils.settings('port', server['port'])
|
2015-12-27 22:14:06 +11:00
|
|
|
if server['scheme'] == 'https':
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('https', 'true')
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('https', 'false')
|
2016-03-04 23:34:30 +11:00
|
|
|
self.logMsg("Writing to Kodi user settings file", 0)
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("PMS machineIdentifier: %s, ip: %s, port: %s, https: %s "
|
|
|
|
% (activeServer, server['ip'], server['port'],
|
|
|
|
server['scheme']), 0)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
# ADDITIONAL PROMPTS #####
|
2016-03-04 00:00:48 +11:00
|
|
|
# directPaths = dialog.yesno(
|
|
|
|
# heading="%s: Playback Mode" % self.addonName,
|
|
|
|
# line1=(
|
|
|
|
# "Caution! If you choose Native mode, you "
|
|
|
|
# "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")
|
|
|
|
|
2016-03-04 23:34:30 +11:00
|
|
|
if forcePlexTV:
|
|
|
|
return
|
|
|
|
|
2016-03-04 00:00:48 +11:00
|
|
|
if dialog.yesno(
|
|
|
|
heading=self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
line1=string(39016).encode('utf-8')):
|
2015-12-28 00:42:54 +11:00
|
|
|
self.logMsg("User opted to disable Plex music library.", 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
utils.settings('enableMusic', value="false")
|
2016-03-04 00:00:48 +11:00
|
|
|
|
|
|
|
if dialog.yesno(
|
|
|
|
heading=self.addonName,
|
2016-03-07 23:01:45 +11:00
|
|
|
line1=string(39017).encode('utf-8')):
|
2016-03-04 00:00:48 +11:00
|
|
|
xbmc.executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|