2018-11-19 00:59:17 +11:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
from .. import utils
|
|
|
|
|
|
|
|
LOG = getLogger('PLEX.account')
|
|
|
|
|
|
|
|
|
|
|
|
class Account(object):
|
2018-11-26 17:19:34 +11:00
|
|
|
def __init__(self, entrypoint=False):
|
2019-01-31 06:36:52 +11:00
|
|
|
self.plex_login = None
|
|
|
|
self.plex_login_id = None
|
|
|
|
self.plex_username = None
|
|
|
|
self.plex_user_id = None
|
|
|
|
self.plex_token = None
|
2021-01-04 03:17:47 +11:00
|
|
|
# Personal access token per specific user and PMS
|
|
|
|
# As a rule of thumb, always use this token!
|
2019-01-31 06:36:52 +11:00
|
|
|
self.pms_token = None
|
|
|
|
self.avatar = None
|
|
|
|
self.myplexlogin = None
|
|
|
|
self.restricted_user = None
|
|
|
|
self.force_login = None
|
|
|
|
self._session = None
|
|
|
|
self.authenticated = False
|
2018-11-26 17:19:34 +11:00
|
|
|
if entrypoint:
|
|
|
|
self.load_entrypoint()
|
|
|
|
else:
|
|
|
|
utils.window('plex_authenticated', clear=True)
|
|
|
|
self.load()
|
2018-11-19 00:59:17 +11:00
|
|
|
|
|
|
|
def set_authenticated(self):
|
|
|
|
self.authenticated = True
|
|
|
|
utils.window('plex_authenticated', value='true')
|
|
|
|
|
|
|
|
def set_unauthenticated(self):
|
|
|
|
self.authenticated = False
|
|
|
|
utils.window('plex_authenticated', clear=True)
|
|
|
|
|
2018-11-27 02:56:39 +11:00
|
|
|
def reset_session(self):
|
|
|
|
try:
|
|
|
|
self._session.stopSession()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
from .. import downloadutils
|
|
|
|
self._session = downloadutils.DownloadUtils()
|
|
|
|
self._session.startSession(reset=True)
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
def load(self):
|
|
|
|
LOG.debug('Loading account settings')
|
2018-11-26 03:03:19 +11:00
|
|
|
# User name we used to sign in to plex.tv
|
|
|
|
self.plex_login = utils.settings('plexLogin') or None
|
|
|
|
self.plex_login_id = utils.settings('plexid') or None
|
2018-11-19 00:59:17 +11:00
|
|
|
# plex.tv username
|
|
|
|
self.plex_username = utils.settings('username') or None
|
|
|
|
# Plex ID of that user (e.g. for plex.tv) as a STRING
|
|
|
|
self.plex_user_id = utils.settings('userid') or None
|
|
|
|
# Token for that user for plex.tv
|
|
|
|
self.plex_token = utils.settings('plexToken') or None
|
|
|
|
# Plex token for the active PMS for the active user
|
|
|
|
# (might be diffent to plex_token)
|
|
|
|
self.pms_token = utils.settings('accessToken') or None
|
|
|
|
self.avatar = utils.settings('plexAvatar') or None
|
|
|
|
self.myplexlogin = utils.settings('myplexlogin') == 'true'
|
|
|
|
|
|
|
|
# Plex home user? Then "False"
|
2018-11-26 17:19:34 +11:00
|
|
|
self.restricted_user = utils.settings('plex_restricteduser') == 'true'
|
2018-11-19 00:59:17 +11:00
|
|
|
# Force user to enter Pin if set?
|
|
|
|
self.force_login = utils.settings('enforceUserLogin') == 'true'
|
|
|
|
|
|
|
|
# Also load these settings to Kodi window variables - they'll be
|
|
|
|
# available for other PKC Python instances
|
|
|
|
utils.window('plex_restricteduser',
|
|
|
|
value='true' if self.restricted_user else 'false')
|
|
|
|
utils.window('plex_token', value=self.plex_token or '')
|
|
|
|
utils.window('pms_token', value=self.pms_token or '')
|
|
|
|
utils.window('plexAvatar', value=self.avatar or '')
|
2018-11-27 02:56:39 +11:00
|
|
|
|
|
|
|
# Start download session
|
|
|
|
self.reset_session()
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.debug('Loaded user %s, %s with plex token %s... and pms token %s...',
|
|
|
|
self.plex_username, self.plex_user_id,
|
|
|
|
self.plex_token[:5] if self.plex_token else None,
|
|
|
|
self.pms_token[:5] if self.pms_token else None)
|
|
|
|
LOG.debug('User is restricted Home user: %s', self.restricted_user)
|
|
|
|
|
2018-11-26 17:19:34 +11:00
|
|
|
def load_entrypoint(self):
|
|
|
|
self.pms_token = utils.settings('accessToken') or None
|
|
|
|
|
2018-11-19 18:17:49 +11:00
|
|
|
def log_out(self):
|
2018-11-26 03:03:19 +11:00
|
|
|
LOG.debug('Logging-out user %s', self.plex_username)
|
2018-11-19 18:17:49 +11:00
|
|
|
self.plex_username = None
|
|
|
|
self.plex_user_id = None
|
|
|
|
self.pms_token = None
|
|
|
|
self.avatar = None
|
|
|
|
self.restricted_user = None
|
|
|
|
self.authenticated = False
|
2018-11-26 03:03:19 +11:00
|
|
|
try:
|
2018-11-27 02:56:39 +11:00
|
|
|
self._session.stopSession()
|
2018-11-26 03:03:19 +11:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-11-19 18:17:49 +11:00
|
|
|
self._session = None
|
|
|
|
|
|
|
|
utils.settings('username', value='')
|
|
|
|
utils.settings('userid', value='')
|
|
|
|
utils.settings('plex_restricteduser', value='')
|
|
|
|
utils.settings('accessToken', value='')
|
|
|
|
utils.settings('plexAvatar', value='')
|
|
|
|
|
|
|
|
utils.window('plex_restricteduser', clear=True)
|
|
|
|
utils.window('pms_token', clear=True)
|
|
|
|
utils.window('plexAvatar', clear=True)
|
|
|
|
utils.window('plex_authenticated', clear=True)
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
def clear(self):
|
|
|
|
LOG.debug('Clearing account settings')
|
|
|
|
self.plex_username = None
|
|
|
|
self.plex_user_id = None
|
|
|
|
self.plex_token = None
|
|
|
|
self.pms_token = None
|
|
|
|
self.avatar = None
|
|
|
|
self.restricted_user = None
|
|
|
|
self.authenticated = False
|
2018-11-26 03:03:19 +11:00
|
|
|
self.plex_login = None
|
|
|
|
self.plex_login_id = None
|
|
|
|
try:
|
2018-11-27 02:56:39 +11:00
|
|
|
self._session.stopSession()
|
2018-11-26 03:03:19 +11:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-11-19 00:59:17 +11:00
|
|
|
self._session = None
|
|
|
|
|
|
|
|
utils.settings('username', value='')
|
|
|
|
utils.settings('userid', value='')
|
|
|
|
utils.settings('plex_restricteduser', value='')
|
|
|
|
utils.settings('plexToken', value='')
|
|
|
|
utils.settings('accessToken', value='')
|
|
|
|
utils.settings('plexAvatar', value='')
|
2018-11-26 03:03:19 +11:00
|
|
|
utils.settings('plexLogin', value='')
|
|
|
|
utils.settings('plexid', value='')
|
2018-11-19 00:59:17 +11:00
|
|
|
|
|
|
|
utils.window('plex_restricteduser', clear=True)
|
|
|
|
utils.window('plex_token', clear=True)
|
|
|
|
utils.window('pms_token', clear=True)
|
|
|
|
utils.window('plexAvatar', clear=True)
|
|
|
|
utils.window('plex_authenticated', clear=True)
|