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.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()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2016-03-04 00:00:48 +11:00
|
|
|
string = self.__language__
|
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')
|
|
|
|
myplexlogin, plexhome, plexLogin, plexToken = \
|
|
|
|
self.plx.GetPlexLoginFromSettings()
|
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 ''
|
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:
|
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-04 00:00:48 +11:00
|
|
|
string(39009)
|
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-01-15 00:47:34 +11:00
|
|
|
elif chk is False or chk >= 400:
|
2015-12-27 22:14:06 +11:00
|
|
|
dialog.ok(
|
|
|
|
self.addonName,
|
2016-03-04 00:00:48 +11:00
|
|
|
string(39010)
|
2015-12-27 22:14:06 +11:00
|
|
|
)
|
|
|
|
# If a Plex server IP has already been set, return.
|
2015-12-25 06:51:47 +11:00
|
|
|
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)
|
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
|
|
|
|
# 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)
|
2016-01-30 06:07:21 +11:00
|
|
|
while True:
|
|
|
|
tokenDict = {'MyPlexToken': plexToken} if plexToken else {}
|
2015-12-27 22:14:06 +11:00
|
|
|
# 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 = []
|
2015-12-27 23:20:53 +11:00
|
|
|
# Exit if no servers found
|
2016-01-30 06:07:21 +11:00
|
|
|
if len(serverlist) == 0:
|
|
|
|
dialog.ok(
|
|
|
|
self.addonName,
|
2016-03-04 00:00:48 +11:00
|
|
|
string(39011)
|
2016-01-30 06:07:21 +11:00
|
|
|
)
|
2015-12-27 23:20:53 +11:00
|
|
|
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
|
|
|
resp = dialog.select(
|
2016-03-04 00:00:48 +11:00
|
|
|
string(39012),
|
2015-12-27 22:14:06 +11:00
|
|
|
dialoglist)
|
|
|
|
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'])
|
|
|
|
# Unauthorized
|
|
|
|
if chk == 401:
|
2016-01-15 00:47:34 +11:00
|
|
|
dialog.ok(self.addonName,
|
2016-03-04 00:00:48 +11:00
|
|
|
string(39013) + str(server['name']),
|
|
|
|
string(39014))
|
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:
|
|
|
|
resp = dialog.yesno(self.addonName,
|
2016-03-04 00:00:48 +11:00
|
|
|
string(39015))
|
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)
|
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-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)
|
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")
|
|
|
|
|
|
|
|
if dialog.yesno(
|
|
|
|
heading=self.addonName,
|
|
|
|
line1=string(39016)):
|
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,
|
|
|
|
line1=string(39017)):
|
|
|
|
xbmc.executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|