PlexKodiConnect/resources/lib/userclient.py

357 lines
12 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2015-12-25 07:07:00 +11:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
2017-12-10 00:35:08 +11:00
from logging import getLogger
from threading import Thread
2015-12-25 07:07:00 +11:00
from xbmc import sleep, executebuiltin
2018-09-11 04:53:46 +10:00
from .windows import userselect
2018-06-22 03:24:37 +10:00
from .downloadutils import DownloadUtils as DU
from . import utils
from . import path_ops
2018-06-22 03:24:37 +10:00
from . import plex_functions as PF
from . import variables as v
2018-06-22 03:24:37 +10:00
from . import state
2015-12-28 03:12:46 +11:00
2016-02-20 06:03:06 +11:00
###############################################################################
2015-12-25 07:07:00 +11:00
2018-06-22 03:24:37 +10:00
LOG = getLogger('PLEX.userclient')
2016-09-03 01:40:36 +10:00
###############################################################################
2015-12-25 07:07:00 +11:00
2018-06-22 03:24:37 +10:00
@utils.thread_methods(add_suspends=['SUSPEND_USER_CLIENT'])
2017-12-10 00:35:08 +11:00
class UserClient(Thread):
2018-04-18 16:39:41 +10:00
"""
Manage Plex users
"""
2015-12-25 07:07:00 +11:00
# Borg - multiple instances, shared state
2016-01-25 20:36:24 +11:00
__shared_state = {}
2015-12-25 07:07:00 +11:00
def __init__(self):
self.__dict__ = self.__shared_state
2015-12-25 07:07:00 +11:00
self.auth = True
self.retry = 0
self.aborted = False
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
self.user = None
self.has_access = True
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
self.server = None
self.server_name = None
self.machine_identifier = None
self.token = None
self.ssl = None
self.sslcert = None
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
self.do_utils = None
2016-01-13 03:23:55 +11:00
2017-12-10 00:35:08 +11:00
Thread.__init__(self)
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
def get_server(self):
"""
2018-04-18 16:39:41 +10:00
Get the current PMS' URL
"""
2016-03-04 23:34:30 +11:00
# Original host
2018-06-22 03:24:37 +10:00
self.server_name = utils.settings('plex_servername')
https = utils.settings('https') == "true"
host = utils.settings('ipaddress')
port = utils.settings('port')
self.machine_identifier = utils.settings('plex_machineIdentifier')
2015-12-25 07:07:00 +11:00
if not host:
2018-02-11 03:59:20 +11:00
LOG.debug("No server information saved.")
2015-12-25 07:07:00 +11:00
return False
2018-04-18 16:39:41 +10:00
server = host + ":" + port
2015-12-25 07:07:00 +11:00
# If https is true
2018-04-18 16:39:41 +10:00
if https:
2015-12-25 07:07:00 +11:00
server = "https://%s" % server
# If https is false
2018-04-18 16:39:41 +10:00
else:
2015-12-25 07:07:00 +11:00
server = "http://%s" % server
# User entered IP; we need to get the machineIdentifier
2018-04-18 16:39:41 +10:00
if not self.machine_identifier:
self.machine_identifier = PF.GetMachineIdentifier(server)
if not self.machine_identifier:
self.machine_identifier = ''
2018-06-22 03:24:37 +10:00
utils.settings('plex_machineIdentifier',
value=self.machine_identifier)
2018-02-11 03:59:20 +11:00
LOG.debug('Returning active server: %s', server)
return server
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
@staticmethod
def get_ssl_verify():
"""
Do we need to verify the SSL certificate? Return None if that is the
case, else False
"""
2018-06-22 03:24:37 +10:00
return None if utils.settings('sslverify') == 'true' else False
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
@staticmethod
def get_ssl_certificate():
"""
Client side certificate
"""
2018-06-22 03:24:37 +10:00
return None if utils.settings('sslcert') == 'None' \
else utils.settings('sslcert')
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
def set_user_prefs(self):
"""
Load a user's profile picture
"""
2018-02-11 03:59:20 +11:00
LOG.debug('Setting user preferences')
2016-03-10 01:37:27 +11:00
# Only try to get user avatar if there is a token
2018-04-18 16:39:41 +10:00
if self.token:
url = PF.GetUserArtworkURL(self.user)
2016-03-10 01:37:27 +11:00
if url:
2018-06-22 03:24:37 +10:00
utils.window('PlexUserImage', value=url)
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
@staticmethod
def check_access():
2016-02-20 06:03:06 +11:00
# Plex: always return True for now
return True
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
def load_user(self, username, user_id, usertoken, authenticated=False):
"""
Load the current user's details for PKC
"""
2018-02-11 03:59:20 +11:00
LOG.debug('Loading current user')
2018-04-18 16:39:41 +10:00
self.token = usertoken
self.server = self.get_server()
self.ssl = self.get_ssl_verify()
self.sslcert = self.get_ssl_certificate()
2015-12-25 07:07:00 +11:00
2016-02-20 06:03:06 +11:00
if authenticated is False:
2018-04-18 16:39:41 +10:00
if self.server is None:
2017-05-21 02:52:51 +10:00
return False
2018-02-11 03:59:20 +11:00
LOG.debug('Testing validity of current token')
2018-04-18 16:39:41 +10:00
res = PF.check_connection(self.server,
token=self.token,
2018-02-11 03:59:20 +11:00
verifySSL=self.ssl)
2016-03-04 23:34:30 +11:00
if res is False:
2016-12-21 02:13:19 +11:00
# PMS probably offline
2016-03-04 23:34:30 +11:00
return False
elif res == 401:
2018-02-11 03:59:20 +11:00
LOG.error('Token is no longer valid')
2016-12-21 02:13:19 +11:00
return 401
2016-03-04 23:34:30 +11:00
elif res >= 400:
2018-02-11 03:59:20 +11:00
LOG.error('Answer from PMS is not as expected. Retrying')
2016-03-04 23:34:30 +11:00
return False
2015-12-25 07:07:00 +11:00
# Set to windows property
2018-04-18 16:39:41 +10:00
state.PLEX_USER_ID = user_id or None
2017-05-18 00:14:17 +10:00
state.PLEX_USERNAME = username
2016-03-11 02:02:46 +11:00
# This is the token for the current PMS (might also be '')
2018-06-22 03:24:37 +10:00
utils.window('pms_token', value=usertoken)
state.PMS_TOKEN = usertoken
2016-03-11 02:02:46 +11:00
# This is the token for plex.tv for the current user
# Is only '' if user is not signed in to plex.tv
2018-06-22 03:24:37 +10:00
utils.window('plex_token', value=utils.settings('plexToken'))
state.PLEX_TOKEN = utils.settings('plexToken') or None
utils.window('plex_restricteduser',
value=utils.settings('plex_restricteduser'))
state.RESTRICTED_USER = True \
2018-06-22 03:24:37 +10:00
if utils.settings('plex_restricteduser') == 'true' else False
utils.window('pms_server', value=self.server)
2018-11-07 22:08:35 +11:00
state.PMS_SERVER = self.server
2018-06-22 03:24:37 +10:00
utils.window('plex_machineIdentifier', value=self.machine_identifier)
utils.window('plex_servername', value=self.server_name)
utils.window('plex_authenticated', value='true')
state.AUTHENTICATED = True
2018-06-22 03:24:37 +10:00
utils.window('useDirectPaths',
value='true' if utils.settings('useDirectPaths') == "1"
else 'false')
state.DIRECT_PATHS = True if utils.settings('useDirectPaths') == "1" \
2017-05-17 23:42:12 +10:00
else False
state.INDICATE_MEDIA_VERSIONS = True \
2018-06-22 03:24:37 +10:00
if utils.settings('indicate_media_versions') == "true" else False
utils.window('plex_force_transcode_pix',
value='true' if utils.settings('force_transcode_pix') == "1"
else 'false')
2016-03-04 23:34:30 +11:00
2015-12-25 07:07:00 +11:00
# Start DownloadUtils session
2018-04-18 16:39:41 +10:00
self.do_utils = DU()
self.do_utils.startSession(reset=True)
2015-12-25 07:07:00 +11:00
# Set user preferences in settings
2018-04-18 16:39:41 +10:00
self.user = username
self.set_user_prefs()
2016-03-10 01:37:27 +11:00
# Writing values to settings file
2018-06-22 03:24:37 +10:00
utils.settings('username', value=username)
utils.settings('userid', value=user_id)
utils.settings('accessToken', value=usertoken)
2015-12-28 21:04:28 +11:00
return True
2015-12-25 07:07:00 +11:00
def authenticate(self):
2018-04-18 16:39:41 +10:00
"""
Authenticate the current user
"""
2018-02-11 03:59:20 +11:00
LOG.debug('Authenticating user')
2016-02-17 19:13:37 +11:00
2016-03-10 01:37:27 +11:00
# Give attempts at entering password / selecting user
2018-09-15 23:15:28 +10:00
if self.retry > 0:
if not self.aborted:
LOG.error("Too many retries to login.")
state.PMS_STATUS = 'Stop'
# Failed to authenticate. Did you login to plex.tv?
2018-09-16 00:12:10 +10:00
utils.messageDialog(utils.lang(29999),utils.lang(39023))
executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
return False
2016-03-10 01:37:27 +11:00
2015-12-25 07:07:00 +11:00
# If there's no settings.xml
if not path_ops.exists("%ssettings.xml" % v.ADDON_PROFILE):
2018-02-11 03:59:20 +11:00
LOG.error("Error, no settings.xml found.")
2015-12-25 07:07:00 +11:00
self.auth = False
2016-03-10 01:37:27 +11:00
return False
2018-04-18 16:39:41 +10:00
server = self.get_server()
2016-03-10 01:37:27 +11:00
# If there is no server we can connect to
2016-03-04 23:34:30 +11:00
if not server:
2018-02-11 03:59:20 +11:00
LOG.info("Missing server information.")
2015-12-25 07:07:00 +11:00
self.auth = False
2016-03-10 01:37:27 +11:00
return False
# If there is a username in the settings, try authenticating
2018-06-22 03:24:37 +10:00
username = utils.settings('username')
userId = utils.settings('userid')
usertoken = utils.settings('accessToken')
enforceLogin = utils.settings('enforceUserLogin')
2016-03-10 01:37:27 +11:00
# Found a user in the settings, try to authenticate
if username and enforceLogin == 'false':
2018-02-11 03:59:20 +11:00
LOG.debug('Trying to authenticate with old settings')
2018-04-18 16:39:41 +10:00
answ = self.load_user(username,
userId,
usertoken,
authenticated=False)
2016-12-21 02:13:19 +11:00
if answ is True:
2016-03-10 01:37:27 +11:00
# SUCCESS: loaded a user from the settings
return True
2016-12-21 02:13:19 +11:00
elif answ == 401:
2018-02-11 03:59:20 +11:00
LOG.error("User token no longer valid. Sign user out")
2018-06-22 03:24:37 +10:00
utils.settings('username', value='')
utils.settings('userid', value='')
utils.settings('accessToken', value='')
2016-12-21 02:13:19 +11:00
else:
2018-02-11 03:59:20 +11:00
LOG.debug("Could not yet authenticate user")
2016-12-21 02:13:19 +11:00
return False
2015-12-25 07:07:00 +11:00
2016-03-10 01:37:27 +11:00
# Could not use settings - try to get Plex user list from plex.tv
2018-06-22 03:24:37 +10:00
plextoken = utils.settings('plexToken')
2016-03-10 01:37:27 +11:00
if plextoken:
2018-02-11 03:59:20 +11:00
LOG.info("Trying to connect to plex.tv to get a user list")
user, self.aborted = userselect.start()
2018-09-11 04:53:46 +10:00
if not user:
2016-03-10 01:37:27 +11:00
# FAILURE: Something went wrong, try again
2016-03-04 23:34:30 +11:00
self.auth = True
2016-03-10 01:37:27 +11:00
self.retry += 1
return False
2018-09-11 04:53:46 +10:00
username = user.title
user_id = user.id
usertoken = user.authToken
2015-12-25 07:07:00 +11:00
else:
2018-02-11 03:59:20 +11:00
LOG.info("Trying to authenticate without a token")
2016-03-10 01:37:27 +11:00
username = ''
2018-09-11 04:53:46 +10:00
user_id = ''
2016-03-10 01:37:27 +11:00
usertoken = ''
2018-09-11 04:53:46 +10:00
if self.load_user(username, user_id, usertoken, authenticated=False):
2016-03-10 01:37:27 +11:00
# SUCCESS: loaded a user from the settings
return True
2018-02-11 03:59:20 +11:00
# Something went wrong, try again
self.auth = True
self.retry += 1
return False
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
def reset_client(self):
"""
Reset all user settings
"""
2018-02-11 03:59:20 +11:00
LOG.debug("Reset UserClient authentication.")
2018-05-30 21:10:54 +10:00
try:
self.do_utils.stopSession()
except AttributeError:
pass
2018-06-22 03:24:37 +10:00
utils.window('plex_authenticated', clear=True)
state.AUTHENTICATED = False
2018-06-22 03:24:37 +10:00
utils.window('pms_token', clear=True)
state.PLEX_TOKEN = None
2018-02-15 06:11:32 +11:00
state.PLEX_TRANSIENT_TOKEN = None
state.PMS_TOKEN = None
2018-06-22 03:24:37 +10:00
utils.window('plex_token', clear=True)
utils.window('pms_server', clear=True)
2018-11-07 22:08:35 +11:00
state.PMS_SERVER = None
2018-06-22 03:24:37 +10:00
utils.window('plex_machineIdentifier', clear=True)
utils.window('plex_servername', clear=True)
2017-05-18 00:15:16 +10:00
state.PLEX_USER_ID = None
2017-05-18 00:14:17 +10:00
state.PLEX_USERNAME = None
2018-06-22 03:24:37 +10:00
utils.window('plex_restricteduser', clear=True)
state.RESTRICTED_USER = False
2016-01-13 03:23:55 +11:00
2018-06-22 03:24:37 +10:00
utils.settings('username', value='')
utils.settings('userid', value='')
utils.settings('accessToken', value='')
2016-03-10 01:37:27 +11:00
2018-04-18 16:39:41 +10:00
self.token = None
2015-12-25 07:07:00 +11:00
self.auth = True
2018-04-18 16:39:41 +10:00
self.user = None
2016-01-13 03:23:55 +11:00
2016-03-10 01:37:27 +11:00
self.retry = 0
2015-12-25 07:07:00 +11:00
def run(self):
2018-04-18 16:39:41 +10:00
"""
Do the work
"""
2018-02-11 03:59:20 +11:00
LOG.info("----===## Starting UserClient ##===----")
2018-02-12 00:57:39 +11:00
stopped = self.stopped
suspended = self.suspended
while not stopped():
while suspended():
if stopped():
2016-01-27 01:13:03 +11:00
break
2018-02-11 03:59:20 +11:00
sleep(1000)
2015-12-25 07:07:00 +11:00
if state.PMS_STATUS == "Stop":
2018-02-11 03:59:20 +11:00
sleep(500)
2016-03-11 02:02:46 +11:00
continue
elif state.PMS_STATUS == "401":
2016-03-11 02:02:46 +11:00
# Unauthorized access, revoke token
state.PMS_STATUS = 'Auth'
2018-06-22 03:24:37 +10:00
utils.window('plex_serverStatus', value='Auth')
2018-04-18 16:39:41 +10:00
self.reset_client()
2018-02-11 03:59:20 +11:00
sleep(3000)
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
if self.auth and (self.user is None):
2015-12-25 07:07:00 +11:00
# Try to authenticate user
if not state.PMS_STATUS or state.PMS_STATUS == "Auth":
2015-12-25 07:07:00 +11:00
# Set auth flag because we no longer need
# to authenticate the user
self.auth = False
2016-03-10 01:37:27 +11:00
if self.authenticate():
2016-03-10 01:53:46 +11:00
# Successfully authenticated and loaded a user
2018-02-11 03:59:20 +11:00
LOG.info("Successfully authenticated!")
2018-04-18 16:39:41 +10:00
LOG.info("Current user: %s", self.user)
2018-02-11 03:59:20 +11:00
LOG.info("Current userId: %s", state.PLEX_USER_ID)
2016-03-10 01:37:27 +11:00
self.retry = 0
state.SUSPEND_LIBRARY_THREAD = False
2018-06-22 03:24:37 +10:00
utils.window('plex_serverStatus', clear=True)
state.PMS_STATUS = False
2015-12-25 07:07:00 +11:00
2018-04-18 16:39:41 +10:00
if not self.auth and (self.user is None):
2016-03-04 23:34:30 +11:00
# Loop if no server found
2018-04-18 16:39:41 +10:00
server = self.get_server()
2016-02-20 06:03:06 +11:00
2015-12-25 07:07:00 +11:00
# The status Stop is for when user cancelled password dialog.
2016-03-11 02:02:46 +11:00
# Or retried too many times
if server and state.PMS_STATUS != "Stop":
2015-12-25 07:07:00 +11:00
# Only if there's information found to login
2018-02-11 03:59:20 +11:00
LOG.debug("Server found: %s", server)
2015-12-25 07:07:00 +11:00
self.auth = True
# Minimize CPU load
2018-02-11 03:59:20 +11:00
sleep(100)
2018-02-11 03:59:20 +11:00
LOG.info("##===---- UserClient Stopped ----===##")