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']
|
2016-03-11 02:02:46 +11:00
|
|
|
if plexToken:
|
|
|
|
self.logMsg('Found plex.tv token in settings', 0)
|
2016-03-04 23:34:30 +11:00
|
|
|
|
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
|
2016-03-17 22:45:38 +11:00
|
|
|
if chk == 401 or chk == 403:
|
|
|
|
self.logMsg('plex.tv connection returned HTTP %s' % chk, 0)
|
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
|
2016-03-08 23:00:03 +11:00
|
|
|
dialog.ok(self.addonName,
|
|
|
|
string(39009))
|
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?
|
2016-03-11 02:02:46 +11:00
|
|
|
self.logMsg('plex.tv connection returned HTTP %s'
|
|
|
|
% str(chk), 0)
|
2016-03-08 23:00:03 +11:00
|
|
|
dialog.ok(self.addonName,
|
|
|
|
string(39010))
|
2016-03-10 01:37:27 +11:00
|
|
|
else:
|
2016-03-11 02:02:46 +11:00
|
|
|
self.logMsg('plex.tv connection with token successful', 0)
|
2016-03-10 01:37:27 +11:00
|
|
|
# Refresh the info from Plex.tv
|
|
|
|
url = 'https://plex.tv/'
|
|
|
|
path = 'users/account'
|
|
|
|
xml = self.plx.getXMLFromPMS(url, path, authtoken=plexToken)
|
|
|
|
if xml:
|
|
|
|
xml = xml.getroot()
|
|
|
|
plexLogin = xml.attrib.get('title')
|
|
|
|
utils.settings('plexLogin', value=plexLogin)
|
|
|
|
home = 'true' if xml.attrib.get('home') == '1' else 'false'
|
|
|
|
utils.settings('plexhome', value=home)
|
|
|
|
utils.settings('plexAvatar', value=xml.attrib.get('thumb'))
|
|
|
|
utils.settings(
|
|
|
|
'plexHomeSize', value=xml.attrib.get('homeSize', '1'))
|
|
|
|
self.logMsg('Updated Plex info from plex.tv', 0)
|
|
|
|
else:
|
|
|
|
self.logMsg('Failed to update Plex info from plex.tv', -1)
|
2016-03-11 02:02:46 +11:00
|
|
|
|
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
|
2016-03-08 23:00:03 +11:00
|
|
|
serverlist = self.plx.returnServerList(clientId,
|
|
|
|
self.plx.g_PMS)
|
2016-03-08 03:11:54 +11:00
|
|
|
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,
|
2016-03-08 23:00:03 +11:00
|
|
|
string(39011)
|
2016-03-08 03:11:54 +11:00
|
|
|
)
|
|
|
|
break
|
|
|
|
for server in serverlist:
|
|
|
|
if server['local'] == '1':
|
|
|
|
# server is in the same network as client. Add "local"
|
|
|
|
dialoglist.append(
|
2016-03-08 23:00:03 +11:00
|
|
|
server['name']
|
|
|
|
+ string(39022))
|
2016-03-08 03:11:54 +11:00
|
|
|
else:
|
2016-03-08 23:00:03 +11:00
|
|
|
dialoglist.append(server['name'])
|
|
|
|
resp = dialog.select(string(39012), dialoglist)
|
2015-12-27 22:14:06 +11:00
|
|
|
server = serverlist[resp]
|
|
|
|
activeServer = server['machineIdentifier']
|
2016-03-09 00:50:43 +11:00
|
|
|
# Re-direct via plex if remote - will lead to the correct SSL
|
|
|
|
# certificate
|
|
|
|
if server['local'] == '1':
|
|
|
|
url = server['scheme'] + '://' + server['ip'] + ':' \
|
|
|
|
+ server['port']
|
|
|
|
else:
|
|
|
|
url = server['baseURL']
|
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-08 23:00:03 +11:00
|
|
|
string(39013) + server['name'],
|
|
|
|
string(39014))
|
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,
|
2016-03-08 23:00:03 +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)
|
2016-03-10 01:37:27 +11:00
|
|
|
utils.settings('plex_servername', server['name'])
|
2016-03-09 00:50:43 +11:00
|
|
|
if server['local'] == '1':
|
|
|
|
scheme = server['scheme']
|
|
|
|
utils.settings('ipaddress', server['ip'])
|
|
|
|
utils.settings('port', server['port'])
|
|
|
|
else:
|
|
|
|
baseURL = server['baseURL'].split(':')
|
|
|
|
scheme = baseURL[0]
|
|
|
|
utils.settings('ipaddress', baseURL[1].replace('//', ''))
|
|
|
|
utils.settings('port', baseURL[2])
|
|
|
|
|
|
|
|
if 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-16 22:26:31 +11:00
|
|
|
goToSettings = False
|
2016-03-16 06:26:45 +11:00
|
|
|
# Direct paths (\\NAS\mymovie.mkv) or addon (http)?
|
|
|
|
if dialog.yesno(heading=self.addonName,
|
|
|
|
line1=string(39027),
|
|
|
|
line2=string(39028),
|
|
|
|
nolabel="Addon (Default)",
|
|
|
|
yeslabel="Native (Direct Paths)"):
|
|
|
|
self.logMsg("User opted to use direct paths.", 1)
|
|
|
|
utils.settings('useDirectPaths', value="1")
|
|
|
|
# Are you on a system where you would like to replace paths
|
|
|
|
# \\NAS\mymovie.mkv with smb://NAS/mymovie.mkv? (e.g. Windows)
|
|
|
|
if dialog.yesno(heading=self.addonName,
|
|
|
|
line1=string(39033)):
|
|
|
|
self.logMsg("User chose to replace paths with smb", 1)
|
2016-03-16 22:26:31 +11:00
|
|
|
else:
|
|
|
|
utils.settings('replaceSMB', value="false")
|
|
|
|
|
|
|
|
# complete replace all original Plex library paths with custom SMB
|
|
|
|
if dialog.yesno(heading=self.addonName,
|
|
|
|
line1=string(39043)):
|
|
|
|
self.logMsg("User chose custom smb paths", 1)
|
|
|
|
utils.settings('remapSMB', value="true")
|
|
|
|
# Please enter your custom smb paths in the settings under
|
|
|
|
# "Sync Options" and then restart Kodi
|
|
|
|
dialog.ok(heading=self.addonName,
|
|
|
|
line1=string(39044))
|
|
|
|
goToSettings = True
|
2016-03-16 06:26:45 +11:00
|
|
|
|
|
|
|
# Go to network credentials?
|
|
|
|
if dialog.yesno(heading=self.addonName,
|
|
|
|
line1=string(39029),
|
|
|
|
line2=string(39030)):
|
|
|
|
self.logMsg("Presenting network credentials dialog.", 1)
|
|
|
|
utils.passwordsXML()
|
2016-03-08 23:00:03 +11:00
|
|
|
# Disable Plex music?
|
|
|
|
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
|
|
|
|
2016-03-16 22:26:31 +11:00
|
|
|
if goToSettings:
|
2016-03-04 00:00:48 +11:00
|
|
|
xbmc.executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|
2016-03-16 22:26:31 +11:00
|
|
|
else:
|
2016-03-23 20:30:49 +11:00
|
|
|
# Open Settings page now? You will need to restart!
|
2016-03-16 22:26:31 +11:00
|
|
|
if dialog.yesno(heading=self.addonName,
|
|
|
|
line1=string(39017)):
|
2016-03-23 20:30:49 +11:00
|
|
|
utils.window('emby_serverStatus', value="Stop")
|
2016-03-16 22:26:31 +11:00
|
|
|
xbmc.executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|