2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2017-12-10 00:35:08 +11:00
|
|
|
from logging import getLogger
|
2018-01-07 01:19:12 +11:00
|
|
|
from Queue import Queue
|
2018-01-08 01:44:20 +11:00
|
|
|
import xml.etree.ElementTree as etree
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
|
2017-05-02 04:29:28 +10:00
|
|
|
from utils import settings, window, language as lang, tryEncode, \
|
2018-01-08 01:16:53 +11:00
|
|
|
XmlKodiSetting, reboot_kodi
|
2018-01-08 01:20:25 +11:00
|
|
|
from downloadutils import DownloadUtils as DU
|
2016-12-28 03:33:52 +11:00
|
|
|
from userclient import UserClient
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-25 02:53:50 +11:00
|
|
|
from PlexAPI import PlexAPI
|
2016-10-24 04:38:21 +11:00
|
|
|
from PlexFunctions import GetMachineIdentifier, get_PMS_settings
|
2017-05-17 18:09:50 +10:00
|
|
|
import state
|
2017-05-30 01:29:29 +10:00
|
|
|
from migration import check_migration
|
2018-01-07 01:19:12 +11:00
|
|
|
import playqueue as PQ
|
2015-12-27 22:14:06 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2016-08-31 00:13:13 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
class InitialSetup():
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug('Entering initialsetup class')
|
2017-01-25 02:53:50 +11:00
|
|
|
self.plx = PlexAPI()
|
2016-05-25 04:43:52 +10:00
|
|
|
self.dialog = xbmcgui.Dialog()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-12-28 03:33:52 +11:00
|
|
|
self.server = UserClient().getServer()
|
2016-08-31 00:13:13 +10:00
|
|
|
self.serverid = settings('plex_machineIdentifier')
|
2016-05-25 04:43:52 +10:00
|
|
|
# Get Plex credentials from settings file, if they exist
|
|
|
|
plexdict = self.plx.GetPlexLoginFromSettings()
|
|
|
|
self.myplexlogin = plexdict['myplexlogin'] == 'true'
|
|
|
|
self.plexLogin = plexdict['plexLogin']
|
|
|
|
self.plexToken = plexdict['plexToken']
|
|
|
|
self.plexid = plexdict['plexid']
|
2016-10-24 04:38:21 +11:00
|
|
|
# Token for the PMS, not plex.tv
|
|
|
|
self.pms_token = settings('accessToken')
|
2016-05-25 04:43:52 +10:00
|
|
|
if self.plexToken:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug('Found a plex.tv token in the settings')
|
2016-05-25 04:43:52 +10:00
|
|
|
|
|
|
|
def PlexTVSignIn(self):
|
2016-05-25 03:00:39 +10:00
|
|
|
"""
|
2016-05-25 04:43:52 +10:00
|
|
|
Signs (freshly) in to plex.tv (will be saved to file settings)
|
|
|
|
|
|
|
|
Returns True if successful, or False if not
|
2016-05-25 03:00:39 +10:00
|
|
|
"""
|
2016-05-25 04:43:52 +10:00
|
|
|
result = self.plx.PlexTvSignInWithPin()
|
|
|
|
if result:
|
|
|
|
self.plexLogin = result['username']
|
|
|
|
self.plexToken = result['token']
|
|
|
|
self.plexid = result['plexid']
|
|
|
|
return True
|
|
|
|
return False
|
2016-05-25 03:00:39 +10:00
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
def CheckPlexTVSignIn(self):
|
2016-01-15 00:47:34 +11:00
|
|
|
"""
|
2016-05-25 04:43:52 +10:00
|
|
|
Checks existing connection to plex.tv. If not, triggers sign in
|
2016-05-30 00:52:00 +10:00
|
|
|
|
|
|
|
Returns True if signed in, False otherwise
|
2016-01-15 00:47:34 +11:00
|
|
|
"""
|
2016-05-30 00:52:00 +10:00
|
|
|
answer = True
|
|
|
|
chk = self.plx.CheckConnection('plex.tv', token=self.plexToken)
|
2016-05-25 04:43:52 +10:00
|
|
|
if chk in (401, 403):
|
|
|
|
# HTTP Error: unauthorized. Token is no longer valid
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('plex.tv connection returned HTTP %s', str(chk))
|
2016-05-25 04:43:52 +10:00
|
|
|
# Delete token in the settings
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('plexToken', value='')
|
|
|
|
settings('plexLogin', value='')
|
2016-05-25 04:43:52 +10:00
|
|
|
# Could not login, please try again
|
2017-01-25 02:04:42 +11:00
|
|
|
self.dialog.ok(lang(29999), lang(39009))
|
2016-05-30 00:52:00 +10:00
|
|
|
answer = self.PlexTVSignIn()
|
2016-05-25 04:43:52 +10:00
|
|
|
elif chk is False or chk >= 400:
|
|
|
|
# Problems connecting to plex.tv. Network or internet issue?
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('Problems connecting to plex.tv; connection returned '
|
|
|
|
'HTTP %s', str(chk))
|
2017-01-25 02:04:42 +11:00
|
|
|
self.dialog.ok(lang(29999), lang(39010))
|
2016-05-30 00:52:00 +10:00
|
|
|
answer = False
|
2016-05-25 04:43:52 +10:00
|
|
|
else:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('plex.tv connection with token successful')
|
2017-05-29 18:51:26 +10:00
|
|
|
settings('plex_status', value=lang(39227))
|
2016-05-25 04:43:52 +10:00
|
|
|
# Refresh the info from Plex.tv
|
2018-01-08 01:20:25 +11:00
|
|
|
xml = DU().downloadUrl('https://plex.tv/users/account',
|
|
|
|
authenticate=False,
|
|
|
|
headerOptions={'X-Plex-Token': self.plexToken})
|
2016-05-25 04:43:52 +10:00
|
|
|
try:
|
|
|
|
self.plexLogin = xml.attrib['title']
|
|
|
|
except (AttributeError, KeyError):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.error('Failed to update Plex info from plex.tv')
|
2016-05-25 04:43:52 +10:00
|
|
|
else:
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('plexLogin', value=self.plexLogin)
|
2016-05-25 04:43:52 +10:00
|
|
|
home = 'true' if xml.attrib.get('home') == '1' else 'false'
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('plexhome', value=home)
|
|
|
|
settings('plexAvatar', value=xml.attrib.get('thumb'))
|
|
|
|
settings('plexHomeSize', value=xml.attrib.get('homeSize', '1'))
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('Updated Plex info from plex.tv')
|
2016-05-30 00:52:00 +10:00
|
|
|
return answer
|
2016-03-04 23:34:30 +11:00
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
def CheckPMS(self):
|
|
|
|
"""
|
|
|
|
Check the PMS that was set in file settings.
|
|
|
|
Will return False if we need to reconnect, because:
|
|
|
|
PMS could not be reached (no matter the authorization)
|
|
|
|
machineIdentifier did not match
|
2015-12-27 22:14:06 +11:00
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
Will also set the PMS machineIdentifier in the file settings if it was
|
|
|
|
not set before
|
|
|
|
"""
|
|
|
|
answer = True
|
2016-08-31 00:13:13 +10:00
|
|
|
chk = self.plx.CheckConnection(self.server, verifySSL=False)
|
2016-05-25 04:43:52 +10:00
|
|
|
if chk is False:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Could not reach PMS %s', self.server)
|
2016-05-25 04:43:52 +10:00
|
|
|
answer = False
|
|
|
|
if answer is True and not self.serverid:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('No PMS machineIdentifier found for %s. Trying to '
|
|
|
|
'get the PMS unique ID', self.server)
|
2016-05-25 04:43:52 +10:00
|
|
|
self.serverid = GetMachineIdentifier(self.server)
|
|
|
|
if self.serverid is None:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Could not retrieve machineIdentifier')
|
2016-05-25 04:43:52 +10:00
|
|
|
answer = False
|
2016-03-10 01:37:27 +11:00
|
|
|
else:
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('plex_machineIdentifier', value=self.serverid)
|
2016-05-25 04:43:52 +10:00
|
|
|
elif answer is True:
|
|
|
|
tempServerid = GetMachineIdentifier(self.server)
|
|
|
|
if tempServerid != self.serverid:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('The current PMS %s was expected to have a '
|
2016-08-31 00:13:13 +10:00
|
|
|
'unique machineIdentifier of %s. But we got '
|
2018-01-07 01:19:12 +11:00
|
|
|
'%s. Pick a new server to be sure',
|
|
|
|
self.server, self.serverid, tempServerid)
|
2016-05-25 04:43:52 +10:00
|
|
|
answer = False
|
|
|
|
return answer
|
2016-03-11 02:02:46 +11:00
|
|
|
|
2016-08-19 01:28:29 +10:00
|
|
|
def _getServerList(self):
|
2016-05-30 00:52:00 +10:00
|
|
|
"""
|
|
|
|
Returns a list of servers from GDM and possibly plex.tv
|
2016-05-25 04:43:52 +10:00
|
|
|
"""
|
2016-05-30 00:52:00 +10:00
|
|
|
self.plx.discoverPMS(xbmc.getIPAddress(),
|
|
|
|
plexToken=self.plexToken)
|
|
|
|
serverlist = self.plx.returnServerList(self.plx.g_PMS)
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug('PMS serverlist: %s', serverlist)
|
2016-05-30 00:52:00 +10:00
|
|
|
return serverlist
|
|
|
|
|
2016-08-19 01:28:29 +10:00
|
|
|
def _checkServerCon(self, server):
|
2016-05-30 00:52:00 +10:00
|
|
|
"""
|
|
|
|
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']
|
2017-05-21 02:46:31 +10:00
|
|
|
verifySSL = True
|
2016-05-30 00:52:00 +10:00
|
|
|
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
|
2015-12-27 22:14:06 +11:00
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
Returns the picked PMS' detail as a dict:
|
|
|
|
{
|
|
|
|
'name': friendlyName, the Plex server's name
|
|
|
|
'address': ip:port
|
|
|
|
'ip': ip, without http/https
|
|
|
|
'port': port
|
|
|
|
'scheme': 'http'/'https', nice for checking for secure connections
|
|
|
|
'local': '1'/'0', Is the server a local server?
|
|
|
|
'owned': '1'/'0', Is the server owned by the user?
|
|
|
|
'machineIdentifier': id, Plex server machine identifier
|
|
|
|
'accesstoken': token Access token to this server
|
|
|
|
'baseURL': baseURL scheme://ip:port
|
|
|
|
'ownername' Plex username of PMS owner
|
|
|
|
}
|
|
|
|
|
|
|
|
or None if unsuccessful
|
|
|
|
"""
|
2016-05-30 00:52:00 +10:00
|
|
|
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:
|
2016-08-19 01:28:29 +10:00
|
|
|
server = self._UserPickPMS()
|
2016-05-30 00:52:00 +10:00
|
|
|
else:
|
2016-08-19 01:28:29 +10:00
|
|
|
server = self._AutoPickPMS()
|
2016-10-24 04:38:21 +11:00
|
|
|
if server is not None:
|
|
|
|
self._write_PMS_settings(server['baseURL'], server['accesstoken'])
|
2016-05-30 00:52:00 +10:00
|
|
|
return server
|
|
|
|
|
2016-10-24 04:38:21 +11:00
|
|
|
def _write_PMS_settings(self, url, token):
|
|
|
|
"""
|
|
|
|
Sets certain settings for server by asking for the PMS' settings
|
|
|
|
Call with url: scheme://ip:port
|
|
|
|
"""
|
|
|
|
xml = get_PMS_settings(url, token)
|
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except AttributeError:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.error('Could not get PMS settings for %s', url)
|
2016-10-24 04:38:21 +11:00
|
|
|
return
|
|
|
|
for entry in xml:
|
|
|
|
if entry.attrib.get('id', '') == 'allowMediaDeletion':
|
|
|
|
settings('plex_allows_mediaDeletion',
|
|
|
|
value=entry.attrib.get('value', 'true'))
|
|
|
|
window('plex_allows_mediaDeletion',
|
|
|
|
value=entry.attrib.get('value', 'true'))
|
|
|
|
|
2016-08-19 01:28:29 +10:00
|
|
|
def _AutoPickPMS(self):
|
2016-05-30 00:52:00 +10:00
|
|
|
"""
|
|
|
|
Will try to pick PMS based on machineIdentifier saved in file settings
|
|
|
|
but only once
|
|
|
|
|
|
|
|
Returns server or None if unsuccessful
|
|
|
|
"""
|
2016-03-08 03:11:54 +11:00
|
|
|
httpsUpdated = False
|
2016-05-30 00:52:00 +10:00
|
|
|
checkedPlexTV = False
|
|
|
|
server = None
|
2016-01-30 06:07:21 +11:00
|
|
|
while True:
|
2016-03-08 03:11:54 +11:00
|
|
|
if httpsUpdated is False:
|
2016-08-19 01:28:29 +10:00
|
|
|
serverlist = self._getServerList()
|
2016-05-30 00:52:00 +10:00
|
|
|
for item in serverlist:
|
|
|
|
if item.get('machineIdentifier') == self.serverid:
|
|
|
|
server = item
|
|
|
|
if server is None:
|
2016-08-31 00:13:13 +10:00
|
|
|
name = settings('plex_servername')
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('The PMS you have used before with a unique '
|
2016-08-31 00:13:13 +10:00
|
|
|
'machineIdentifier of %s and name %s is '
|
2018-01-07 01:19:12 +11:00
|
|
|
'offline', self.serverid, name)
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2016-08-19 01:28:29 +10:00
|
|
|
chk = self._checkServerCon(server)
|
2016-05-30 00:52:00 +10:00
|
|
|
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:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Not yet authorized for Plex server %s',
|
|
|
|
server['name'])
|
2016-05-30 00:52:00 +10:00
|
|
|
if self.CheckPlexTVSignIn() is True:
|
|
|
|
if checkedPlexTV is False:
|
|
|
|
# Try again
|
|
|
|
checkedPlexTV = True
|
|
|
|
httpsUpdated = False
|
|
|
|
continue
|
|
|
|
else:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Not authorized even though we are signed '
|
2016-08-31 00:13:13 +10:00
|
|
|
' in to plex.tv correctly')
|
2017-01-25 02:04:42 +11:00
|
|
|
self.dialog.ok(lang(29999), '%s %s'
|
2017-02-27 01:21:55 +11:00
|
|
|
% (lang(39214),
|
|
|
|
tryEncode(server['name'])))
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
# Problems connecting
|
|
|
|
elif chk >= 400 or chk is False:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Problems connecting to server %s. chk is %s',
|
|
|
|
server['name'], chk)
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('We found a server to automatically connect to: %s',
|
|
|
|
server['name'])
|
2016-05-30 00:52:00 +10:00
|
|
|
return server
|
|
|
|
|
2016-08-19 01:28:29 +10:00
|
|
|
def _UserPickPMS(self):
|
2016-05-30 00:52:00 +10:00
|
|
|
"""
|
|
|
|
Lets user pick his/her PMS from a list
|
|
|
|
|
|
|
|
Returns server or None if unsuccessful
|
|
|
|
"""
|
|
|
|
httpsUpdated = False
|
|
|
|
while True:
|
|
|
|
if httpsUpdated is False:
|
2016-08-19 01:28:29 +10:00
|
|
|
serverlist = self._getServerList()
|
2016-03-08 03:11:54 +11:00
|
|
|
# Exit if no servers found
|
|
|
|
if len(serverlist) == 0:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('No plex media servers found!')
|
2017-01-25 02:04:42 +11:00
|
|
|
self.dialog.ok(lang(29999), lang(39011))
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2016-05-25 04:43:52 +10:00
|
|
|
# Get a nicer list
|
|
|
|
dialoglist = []
|
2016-03-08 03:11:54 +11:00
|
|
|
for server in serverlist:
|
|
|
|
if server['local'] == '1':
|
2016-05-30 00:52:00 +10:00
|
|
|
# server is in the same network as client.
|
|
|
|
# Add"local"
|
2016-08-31 00:13:13 +10:00
|
|
|
msg = lang(39022)
|
2016-04-07 00:24:03 +10:00
|
|
|
else:
|
|
|
|
# Add 'remote'
|
2016-08-31 00:13:13 +10:00
|
|
|
msg = lang(39054)
|
2016-04-07 00:24:03 +10:00
|
|
|
if server.get('ownername'):
|
|
|
|
# Display username if its not our PMS
|
|
|
|
dialoglist.append('%s (%s, %s)'
|
|
|
|
% (server['name'],
|
|
|
|
server['ownername'],
|
|
|
|
msg))
|
2016-03-08 03:11:54 +11:00
|
|
|
else:
|
2016-04-07 00:24:03 +10:00
|
|
|
dialoglist.append('%s (%s)'
|
2016-05-25 04:43:52 +10:00
|
|
|
% (server['name'], msg))
|
|
|
|
# Let user pick server from a list
|
2016-08-31 00:13:13 +10:00
|
|
|
resp = self.dialog.select(lang(39012), dialoglist)
|
2017-02-01 19:35:39 +11:00
|
|
|
if resp == -1:
|
|
|
|
# User cancelled
|
|
|
|
return
|
2016-05-30 00:52:00 +10:00
|
|
|
|
2015-12-27 22:14:06 +11:00
|
|
|
server = serverlist[resp]
|
2016-08-19 01:28:29 +10:00
|
|
|
chk = self._checkServerCon(server)
|
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:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.warn('Not yet authorized for Plex server %s',
|
|
|
|
server['name'])
|
2016-03-04 23:34:30 +11:00
|
|
|
# Please sign in to plex.tv
|
2017-01-25 02:04:42 +11:00
|
|
|
self.dialog.ok(lang(29999),
|
2016-08-31 00:13:13 +10:00
|
|
|
lang(39013) + server['name'],
|
|
|
|
lang(39014))
|
2016-05-25 04:43:52 +10:00
|
|
|
if self.PlexTVSignIn() is False:
|
2016-01-14 20:20:19 +11:00
|
|
|
# Exit while loop if user cancels
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2015-12-27 22:14:06 +11:00
|
|
|
# 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?
|
2017-01-25 02:04:42 +11:00
|
|
|
answ = self.dialog.yesno(lang(29999),
|
2016-08-31 00:13:13 +10:00
|
|
|
lang(39015))
|
2015-12-27 22:14:06 +11:00
|
|
|
# Exit while loop if user chooses No
|
2016-05-25 04:43:52 +10:00
|
|
|
if not answ:
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2015-12-27 22:14:06 +11:00
|
|
|
# Otherwise: connection worked!
|
|
|
|
else:
|
2016-05-30 00:52:00 +10:00
|
|
|
return server
|
2016-05-25 04:43:52 +10:00
|
|
|
|
|
|
|
def WritePMStoSettings(self, server):
|
|
|
|
"""
|
|
|
|
Saves server to file settings. server is a dict of the form:
|
|
|
|
{
|
|
|
|
'name': friendlyName, the Plex server's name
|
|
|
|
'address': ip:port
|
|
|
|
'ip': ip, without http/https
|
|
|
|
'port': port
|
|
|
|
'scheme': 'http'/'https', nice for checking for secure connections
|
|
|
|
'local': '1'/'0', Is the server a local server?
|
|
|
|
'owned': '1'/'0', Is the server owned by the user?
|
|
|
|
'machineIdentifier': id, Plex server machine identifier
|
|
|
|
'accesstoken': token Access token to this server
|
|
|
|
'baseURL': baseURL scheme://ip:port
|
|
|
|
'ownername' Plex username of PMS owner
|
|
|
|
}
|
|
|
|
"""
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('plex_machineIdentifier', server['machineIdentifier'])
|
|
|
|
settings('plex_servername', server['name'])
|
|
|
|
settings('plex_serverowned',
|
|
|
|
'true' if server['owned'] == '1'
|
|
|
|
else 'false')
|
2016-05-25 04:43:52 +10:00
|
|
|
# Careful to distinguish local from remote PMS
|
2016-03-09 00:50:43 +11:00
|
|
|
if server['local'] == '1':
|
|
|
|
scheme = server['scheme']
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('ipaddress', server['ip'])
|
|
|
|
settings('port', server['port'])
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("Setting SSL verify to false, because server is "
|
2016-08-31 00:13:13 +10:00
|
|
|
"local")
|
|
|
|
settings('sslverify', 'false')
|
2016-03-09 00:50:43 +11:00
|
|
|
else:
|
|
|
|
baseURL = server['baseURL'].split(':')
|
|
|
|
scheme = baseURL[0]
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('ipaddress', baseURL[1].replace('//', ''))
|
|
|
|
settings('port', baseURL[2])
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("Setting SSL verify to true, because server is not "
|
2016-08-31 00:13:13 +10:00
|
|
|
"local")
|
|
|
|
settings('sslverify', 'true')
|
2016-03-09 00:50:43 +11:00
|
|
|
|
|
|
|
if scheme == 'https':
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('https', 'true')
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('https', 'false')
|
2016-05-25 04:43:52 +10:00
|
|
|
# And finally do some logging
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("Writing to Kodi user settings file")
|
|
|
|
LOG.debug("PMS machineIdentifier: %s, ip: %s, port: %s, https: %s ",
|
|
|
|
server['machineIdentifier'], server['ip'], server['port'],
|
|
|
|
server['scheme'])
|
2016-05-25 04:43:52 +10:00
|
|
|
|
2016-05-30 00:52:00 +10:00
|
|
|
def setup(self):
|
2016-05-25 04:43:52 +10:00
|
|
|
"""
|
|
|
|
Initial setup. Run once upon startup.
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
Check server, user, direct paths, music, direct stream if not direct
|
|
|
|
path.
|
|
|
|
"""
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info("Initial setup called.")
|
2016-05-25 04:43:52 +10:00
|
|
|
dialog = self.dialog
|
2018-01-08 01:16:53 +11:00
|
|
|
try:
|
|
|
|
with XmlKodiSetting('advancedsettings.xml',
|
|
|
|
force_create=True,
|
|
|
|
top_element='advancedsettings') as xml:
|
|
|
|
# Get current Kodi video cache setting
|
|
|
|
cache = xml.get_setting(['cache', 'memorysize'])
|
|
|
|
# Disable foreground "Loading media information from files"
|
|
|
|
# (still used by Kodi, even though the Wiki says otherwise)
|
|
|
|
xml.set_setting(['musiclibrary', 'backgroundupdate'],
|
|
|
|
value='true')
|
|
|
|
# Disable cleaning of library - not compatible with PKC
|
|
|
|
xml.set_setting(['videolibrary', 'cleanonupdate'],
|
|
|
|
value='false')
|
|
|
|
reboot = xml.write_xml
|
2018-01-08 01:44:20 +11:00
|
|
|
except etree.ParseError:
|
2018-01-08 01:16:53 +11:00
|
|
|
cache = None
|
|
|
|
reboot = False
|
2017-12-29 01:24:36 +11:00
|
|
|
# Kodi default cache if no setting is set
|
|
|
|
cache = str(cache.text) if cache is not None else '20971520'
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info('Current Kodi video memory cache in bytes: %s', cache)
|
2017-05-02 04:29:28 +10:00
|
|
|
settings('kodi_video_cache', value=cache)
|
2018-01-08 01:16:53 +11:00
|
|
|
|
2018-01-08 01:44:20 +11:00
|
|
|
# Hack to make PKC Kodi master lock compatible
|
|
|
|
try:
|
|
|
|
with XmlKodiSetting('sources.xml',
|
|
|
|
force_create=True,
|
|
|
|
top_element='sources') as xml:
|
|
|
|
root = xml.set_setting(['video'])
|
|
|
|
count = 2
|
|
|
|
for source in root.findall('.//path'):
|
|
|
|
if source.text == "smb://":
|
|
|
|
count -= 1
|
|
|
|
if count == 0:
|
|
|
|
# sources already set
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Missing smb:// occurences, re-add.
|
|
|
|
for _ in range(0, count):
|
|
|
|
source = etree.SubElement(root, 'source')
|
|
|
|
etree.SubElement(source,
|
|
|
|
'name').text = "PlexKodiConnect Masterlock Hack"
|
|
|
|
etree.SubElement(source,
|
|
|
|
'path',
|
|
|
|
attrib={'pathversion': "1"}).text = "smb://"
|
|
|
|
etree.SubElement(source, 'allowsharing').text = "true"
|
|
|
|
if reboot is False:
|
|
|
|
reboot = xml.write_xml
|
|
|
|
except etree.ParseError:
|
|
|
|
pass
|
|
|
|
|
2017-05-30 01:29:29 +10:00
|
|
|
# Do we need to migrate stuff?
|
|
|
|
check_migration()
|
2016-05-25 04:43:52 +10:00
|
|
|
|
2018-01-07 01:19:12 +11:00
|
|
|
# Initialize the PKC playqueues
|
|
|
|
PQ.init_playqueues()
|
|
|
|
# Init some Queues()
|
|
|
|
state.COMMAND_PIPELINE_QUEUE = Queue()
|
|
|
|
state.COMPANION_QUEUE = Queue(maxsize=100)
|
|
|
|
state.WEBSOCKET_QUEUE = Queue()
|
|
|
|
|
2016-05-25 04:43:52 +10:00
|
|
|
# If a Plex server IP has already been set
|
|
|
|
# return only if the right machine identifier is found
|
|
|
|
if self.server:
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info("PMS is already set: %s. Checking now...", self.server)
|
2017-05-01 01:35:51 +10:00
|
|
|
if self.CheckPMS():
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.info("Using PMS %s with machineIdentifier %s",
|
|
|
|
self.server, self.serverid)
|
2016-10-24 04:38:21 +11:00
|
|
|
self._write_PMS_settings(self.server, self.pms_token)
|
2018-01-08 01:16:53 +11:00
|
|
|
if reboot is True:
|
|
|
|
reboot_kodi()
|
2016-05-25 04:43:52 +10:00
|
|
|
return
|
|
|
|
|
|
|
|
# If not already retrieved myplex info, optionally let user sign in
|
|
|
|
# to plex.tv. This DOES get called on very first install run
|
|
|
|
if not self.plexToken and self.myplexlogin:
|
|
|
|
self.PlexTVSignIn()
|
|
|
|
|
|
|
|
server = self.PickPMS()
|
2016-05-30 00:52:00 +10:00
|
|
|
if server is not None:
|
|
|
|
# Write our chosen server to Kodi settings file
|
|
|
|
self.WritePMStoSettings(server)
|
2016-03-04 23:34:30 +11:00
|
|
|
|
2016-05-30 00:52:00 +10:00
|
|
|
# User already answered the installation questions
|
2016-08-31 00:13:13 +10:00
|
|
|
if settings('InstallQuestionsAnswered') == 'true':
|
2018-01-08 01:16:53 +11:00
|
|
|
if reboot is True:
|
|
|
|
reboot_kodi()
|
2016-05-30 00:52:00 +10:00
|
|
|
return
|
2016-05-25 04:43:52 +10:00
|
|
|
|
|
|
|
# Additional settings where the user needs to choose
|
2016-03-16 06:26:45 +11:00
|
|
|
# Direct paths (\\NAS\mymovie.mkv) or addon (http)?
|
2016-09-05 01:10:38 +10:00
|
|
|
goToSettings = False
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(lang(29999),
|
2016-08-31 00:13:13 +10:00
|
|
|
lang(39027),
|
|
|
|
lang(39028),
|
2016-03-16 06:26:45 +11:00
|
|
|
nolabel="Addon (Default)",
|
|
|
|
yeslabel="Native (Direct Paths)"):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User opted to use direct paths.")
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('useDirectPaths', value="1")
|
2017-05-17 23:42:12 +10:00
|
|
|
state.DIRECT_PATHS = True
|
2016-03-16 06:26:45 +11:00
|
|
|
# Are you on a system where you would like to replace paths
|
|
|
|
# \\NAS\mymovie.mkv with smb://NAS/mymovie.mkv? (e.g. Windows)
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(heading=lang(29999), line1=lang(39033)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User chose to replace paths with smb")
|
2016-03-16 22:26:31 +11:00
|
|
|
else:
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('replaceSMB', value="false")
|
2016-03-16 22:26:31 +11:00
|
|
|
|
|
|
|
# complete replace all original Plex library paths with custom SMB
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(heading=lang(29999), line1=lang(39043)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User chose custom smb paths")
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('remapSMB', value="true")
|
2016-03-16 22:26:31 +11:00
|
|
|
# Please enter your custom smb paths in the settings under
|
|
|
|
# "Sync Options" and then restart Kodi
|
2017-01-25 02:04:42 +11:00
|
|
|
dialog.ok(heading=lang(29999), line1=lang(39044))
|
2016-03-16 22:26:31 +11:00
|
|
|
goToSettings = True
|
2016-03-16 06:26:45 +11:00
|
|
|
|
|
|
|
# Go to network credentials?
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(heading=lang(29999),
|
2016-08-31 00:13:13 +10:00
|
|
|
line1=lang(39029),
|
|
|
|
line2=lang(39030)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("Presenting network credentials dialog.")
|
2016-08-31 00:13:13 +10:00
|
|
|
from utils import passwordsXML
|
|
|
|
passwordsXML()
|
2016-03-08 23:00:03 +11:00
|
|
|
# Disable Plex music?
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(heading=lang(29999), line1=lang(39016)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User opted to disable Plex music library.")
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('enableMusic', value="false")
|
2016-03-04 00:00:48 +11:00
|
|
|
|
2016-04-29 20:44:56 +10:00
|
|
|
# Download additional art from FanArtTV
|
2017-01-25 02:04:42 +11:00
|
|
|
if dialog.yesno(heading=lang(29999), line1=lang(39061)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User opted to use FanArtTV")
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('FanartTV', value="true")
|
2017-05-31 22:13:45 +10:00
|
|
|
# Do you want to replace your custom user ratings with an indicator of
|
|
|
|
# how many versions of a media item you posses?
|
|
|
|
if dialog.yesno(heading=lang(29999), line1=lang(39718)):
|
2018-01-07 01:19:12 +11:00
|
|
|
LOG.debug("User opted to replace user ratings with version number")
|
2017-05-31 22:13:45 +10:00
|
|
|
settings('indicate_media_versions', value="true")
|
2016-04-29 20:44:56 +10:00
|
|
|
|
2016-12-21 02:13:19 +11:00
|
|
|
# If you use several Plex libraries of one kind, e.g. "Kids Movies" and
|
|
|
|
# "Parents Movies", be sure to check https://goo.gl/JFtQV9
|
2017-08-11 05:08:37 +10:00
|
|
|
# dialog.ok(heading=lang(29999), line1=lang(39076))
|
2017-05-31 22:13:45 +10:00
|
|
|
|
2017-05-30 00:33:44 +10:00
|
|
|
# Need to tell about our image source for collections: themoviedb.org
|
2017-08-11 05:08:37 +10:00
|
|
|
# dialog.ok(heading=lang(29999), line1=lang(39717))
|
2016-05-30 00:52:00 +10:00
|
|
|
# Make sure that we only ask these questions upon first installation
|
2016-08-31 00:13:13 +10:00
|
|
|
settings('InstallQuestionsAnswered', value='true')
|
2016-05-30 00:52:00 +10:00
|
|
|
|
2016-04-22 20:34:35 +10:00
|
|
|
if goToSettings is False:
|
|
|
|
# Open Settings page now? You will need to restart!
|
2017-01-25 02:04:42 +11:00
|
|
|
goToSettings = dialog.yesno(heading=lang(29999), line1=lang(39017))
|
2016-03-16 22:26:31 +11:00
|
|
|
if goToSettings:
|
2017-05-17 18:09:50 +10:00
|
|
|
state.PMS_STATUS = 'Stop'
|
2016-03-04 00:00:48 +11:00
|
|
|
xbmc.executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|
2018-01-08 01:16:53 +11:00
|
|
|
elif reboot is True:
|
|
|
|
reboot_kodi()
|