PlexKodiConnect/resources/lib/app/connection.py

112 lines
4.5 KiB
Python
Raw Permalink Normal View History

2018-11-19 00:59:17 +11:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from logging import getLogger
import secrets
2018-11-19 00:59:17 +11:00
2020-12-28 03:29:01 +11:00
from .. import utils, json_rpc as js
2018-11-19 00:59:17 +11:00
LOG = getLogger('PLEX.connection')
class Connection(object):
def __init__(self, entrypoint=False):
self.verify_ssl_cert = None
self.ssl_cert_path = None
self.machine_identifier = None
self.server_name = None
self.https = None
self.host = None
self.port = None
self.server = None
self.online = False
self.webserver_host = None
self.webserver_port = None
self.webserver_username = None
self.webserver_password = None
if entrypoint:
self.load_entrypoint()
else:
self.load_webserver()
self.load()
# Token passed along, e.g. if playback initiated by Plex Companion. Might be
# another user playing something! Token identifies user
self.plex_transient_token = None
2018-11-19 00:59:17 +11:00
def load_webserver(self):
"""
PKC needs Kodi webserver to work correctly
"""
LOG.debug('Loading Kodi webserver details')
if not utils.settings('enableTextureCache') == 'true':
LOG.info('Artwork caching disabled')
return
self.webserver_password = js.get_setting('services.webserverpassword')
if not self.webserver_password:
LOG.warn('No password set for the Kodi web server. Generating a '
'new random password')
self.webserver_password = secrets.token_urlsafe(16)
js.set_setting('services.webserverpassword', self.webserver_password)
if not js.get_setting('services.webserver'):
# The Kodi webserver is needed for artwork caching. PKC already set
# a strong, random password automatically if you haven't done so
# already. Please confirm the next dialog that you want to enable
# the webserver now with Yes.
utils.messageDialog(utils.lang(29999), utils.lang(30004))
# Enable the webserver, it is disabled. Will force a Kodi pop-up
2018-11-19 00:59:17 +11:00
js.set_setting('services.webserver', True)
if not js.get_setting('services.webserver'):
LOG.warn('User chose to not enable Kodi webserver')
utils.settings('enableTextureCache', value='false')
2018-11-19 00:59:17 +11:00
self.webserver_host = 'localhost'
self.webserver_port = js.get_setting('services.webserverport')
self.webserver_username = js.get_setting('services.webserverusername')
def load(self):
LOG.debug('Loading connection settings')
# Shall we verify SSL certificates? "None" will leave SSL enabled
# Ignore this setting for Kodi >= 18 as Kodi 18 is much stricter
# with checking SSL certs
self.verify_ssl_cert = None
2018-11-19 00:59:17 +11:00
# Do we have an ssl certificate for PKC we need to use?
self.ssl_cert_path = utils.settings('sslcert') \
if utils.settings('sslcert') != 'None' else None
self.machine_identifier = utils.settings('plex_machineIdentifier') or None
self.server_name = utils.settings('plex_servername') or None
self.https = utils.settings('https') == 'true'
self.host = utils.settings('ipaddress') or None
self.port = int(utils.settings('port')) if utils.settings('port') else None
if not self.host:
self.server = None
elif self.https:
self.server = 'https://%s:%s' % (self.host, self.port)
else:
self.server = 'http://%s:%s' % (self.host, self.port)
self.online = False
2018-11-19 00:59:17 +11:00
LOG.debug('Set server %s (%s) to %s',
self.server_name, self.machine_identifier, self.server)
def load_entrypoint(self):
self.verify_ssl_cert = None
self.ssl_cert_path = utils.settings('sslcert') \
if utils.settings('sslcert') != 'None' else None
self.https = utils.settings('https') == 'true'
self.host = utils.settings('ipaddress') or None
self.port = int(utils.settings('port')) if utils.settings('port') else None
if not self.host:
self.server = None
elif self.https:
self.server = 'https://%s:%s' % (self.host, self.port)
else:
self.server = 'http://%s:%s' % (self.host, self.port)
2018-11-19 00:59:17 +11:00
def clear(self):
LOG.debug('Clearing connection settings')
self.machine_identifier = None
self.server_name = None
2019-02-08 23:52:33 +11:00
self.https = None
2018-11-19 00:59:17 +11:00
self.host = None
self.port = None
self.server = None