2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2018-06-22 03:24:37 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2018-06-22 03:24:37 +10:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import xbmc
|
2018-11-26 17:31:36 +11:00
|
|
|
import xbmcgui
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import utils, clientinfo, timing
|
|
|
|
from . import initialsetup, artwork
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import kodimonitor
|
2018-10-25 02:19:36 +11:00
|
|
|
from . import sync
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import websocket_client
|
|
|
|
from . import plex_companion
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import plex_functions as PF, playqueue as PQ
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import playback_starter
|
|
|
|
from . import playqueue
|
|
|
|
from . import variables as v
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import app
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import loghandler
|
2018-11-26 03:03:19 +11:00
|
|
|
from . import backgroundthread
|
2018-11-19 00:59:17 +11:00
|
|
|
from .windows import userselect
|
2018-06-22 03:24:37 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
loghandler.config()
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG = logging.getLogger("PLEX.service")
|
2018-06-22 03:24:37 +10:00
|
|
|
###############################################################################
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
WINDOW_PROPERTIES = (
|
2018-11-26 05:46:34 +11:00
|
|
|
"plex_dbScan", "pms_token", "plex_token", "pms_server",
|
|
|
|
"plex_authenticated", "plex_restricteduser", "plex_allows_mediaDeletion",
|
|
|
|
"plex_command", "plex_result")
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-26 17:31:36 +11:00
|
|
|
# "Start from beginning", "Play from beginning"
|
|
|
|
STRINGS = (utils.try_encode(utils.lang(12021)),
|
|
|
|
utils.try_encode(utils.lang(12023)))
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
class Service():
|
2018-06-22 03:24:37 +10:00
|
|
|
ws = None
|
2018-10-25 02:19:36 +11:00
|
|
|
sync = None
|
2018-06-22 03:24:37 +10:00
|
|
|
plexcompanion = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-01-13 23:41:22 +11:00
|
|
|
self._init_done = False
|
|
|
|
# Kodi Version supported by PKC?
|
|
|
|
try:
|
|
|
|
v.database_paths()
|
|
|
|
except RuntimeError as err:
|
|
|
|
|
|
|
|
# Database does not exists
|
|
|
|
LOG.error('The current Kodi version is incompatible')
|
|
|
|
LOG.error('Error: %s', err)
|
|
|
|
# "The current Kodi version is not supported by PKC. Please consult
|
|
|
|
# the Plex forum."
|
|
|
|
utils.messageDialog(utils.lang(29999), utils.lang(39403))
|
|
|
|
return
|
2018-06-22 03:24:37 +10:00
|
|
|
# Initial logging
|
|
|
|
LOG.info("======== START %s ========", v.ADDON_NAME)
|
|
|
|
LOG.info("Platform: %s", v.PLATFORM)
|
|
|
|
LOG.info("KODI Version: %s", v.KODILONGVERSION)
|
|
|
|
LOG.info("%s Version: %s", v.ADDON_NAME, v.ADDON_VERSION)
|
|
|
|
LOG.info("PKC Direct Paths: %s",
|
|
|
|
utils.settings('useDirectPaths') == '1')
|
|
|
|
LOG.info("Number of sync threads: %s",
|
|
|
|
utils.settings('syncThreadNumber'))
|
2018-06-24 02:25:18 +10:00
|
|
|
LOG.info('Playlist m3u encoding: %s', v.M3U_ENCODING)
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG.info("Full sys.argv received: %s", sys.argv)
|
2018-08-06 02:13:16 +10:00
|
|
|
LOG.info('Sync playlists: %s', utils.settings('enablePlaylistSync'))
|
2018-07-03 04:23:16 +10:00
|
|
|
LOG.info('Synching only specific Kodi playlists: %s',
|
|
|
|
utils.settings('syncSpecificKodiPlaylists') == 'true')
|
|
|
|
LOG.info('Kodi playlist prefix: %s',
|
|
|
|
utils.settings('syncSpecificKodiPlaylistsPrefix'))
|
|
|
|
LOG.info('Synching only specific Plex playlists: %s',
|
|
|
|
utils.settings('syncSpecificPlexPlaylistsPrefix') == 'true')
|
|
|
|
LOG.info('Play playlist prefix: %s',
|
|
|
|
utils.settings('syncSpecificPlexPlaylistsPrefix'))
|
2018-11-03 20:36:37 +11:00
|
|
|
LOG.info("Db version: %s", utils.settings('dbCreatedWithVersion'))
|
2019-01-13 23:41:22 +11:00
|
|
|
LOG.info('Kodi video database version: %s', v.DB_VIDEO_VERSION)
|
|
|
|
LOG.info('Kodi music database version: %s', v.DB_MUSIC_VERSION)
|
|
|
|
LOG.info('Kodi texture database version: %s', v.DB_TEXTURE_VERSION)
|
2018-11-19 00:59:17 +11:00
|
|
|
|
2018-12-09 23:45:09 +11:00
|
|
|
# Reset some status in the PKC settings
|
|
|
|
# toggled to "No"
|
|
|
|
utils.settings('plex_status_fanarttv_lookup', value=utils.lang(106))
|
2018-12-09 23:52:26 +11:00
|
|
|
# toggled to "No"
|
|
|
|
utils.settings('plex_status_image_caching', value=utils.lang(106))
|
2018-12-09 23:45:09 +11:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
# Reset window props
|
|
|
|
for prop in WINDOW_PROPERTIES:
|
|
|
|
utils.window(prop, clear=True)
|
|
|
|
|
|
|
|
# To detect Kodi profile switches
|
|
|
|
utils.window('plex_kodiProfile',
|
|
|
|
value=utils.try_decode(xbmc.translatePath("special://profile")))
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
# Load/Reset PKC entirely - important for user/Kodi profile switch
|
2018-11-19 00:59:17 +11:00
|
|
|
# Clear video nodes properties
|
|
|
|
from .library_sync import videonodes
|
|
|
|
videonodes.VideoNodes().clearProperties()
|
|
|
|
clientinfo.getDeviceId()
|
|
|
|
# Init time-offset between Kodi and Plex
|
2018-11-21 05:14:11 +11:00
|
|
|
timing.KODI_PLEX_TIME_OFFSET = float(utils.settings('kodiplextimeoffset') or 0.0)
|
2018-11-19 00:59:17 +11:00
|
|
|
|
2018-11-26 03:03:19 +11:00
|
|
|
self.startup_completed = False
|
|
|
|
self.server_has_been_online = True
|
|
|
|
self.welcome_msg = True
|
|
|
|
self.connection_check_counter = 0
|
|
|
|
# Flags for other threads
|
|
|
|
self.connection_check_running = False
|
|
|
|
self.auth_running = False
|
2019-01-13 23:41:22 +11:00
|
|
|
self._init_done = True
|
2018-11-26 03:03:19 +11:00
|
|
|
|
2018-12-11 05:51:17 +11:00
|
|
|
def isCanceled(self):
|
|
|
|
return xbmc.abortRequested or app.APP.stop_pkc
|
|
|
|
|
2018-11-26 03:03:19 +11:00
|
|
|
def on_connection_check(self, result):
|
|
|
|
"""
|
|
|
|
Call this method after PF.check_connection()
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if result is False:
|
|
|
|
# Server is offline or cannot be reached
|
|
|
|
# Alert the user and suppress future warning
|
|
|
|
if app.CONN.online:
|
|
|
|
# PMS was online before
|
|
|
|
app.CONN.online = False
|
|
|
|
app.APP.suspend_threads = True
|
|
|
|
LOG.warn("Plex Media Server went offline")
|
|
|
|
if utils.settings('show_pms_offline') == 'true':
|
|
|
|
utils.dialog('notification',
|
|
|
|
utils.lang(33001),
|
|
|
|
"%s %s" % (utils.lang(29999),
|
|
|
|
utils.lang(33002)),
|
|
|
|
icon='{plex}',
|
|
|
|
sound=False)
|
|
|
|
self.connection_check_counter += 1
|
|
|
|
# Periodically check if the IP changed every 15 seconds
|
|
|
|
if self.connection_check_counter > 150:
|
|
|
|
self.connection_check_counter = 0
|
|
|
|
server = self.setup.pick_pms()
|
|
|
|
if server:
|
2018-11-27 02:56:39 +11:00
|
|
|
LOG.debug('Found server: %s', server)
|
|
|
|
self.setup.save_pms_settings(server['baseURL'], server['token'])
|
2018-11-26 03:03:19 +11:00
|
|
|
self.setup.write_pms_to_settings(server)
|
|
|
|
app.CONN.load()
|
2018-11-27 02:56:39 +11:00
|
|
|
app.ACCOUNT.reset_session()
|
2018-11-26 03:03:19 +11:00
|
|
|
else:
|
|
|
|
# Server is online
|
|
|
|
self.connection_check_counter = 0
|
|
|
|
if not app.CONN.online:
|
|
|
|
# Server was offline before
|
|
|
|
if (self.welcome_msg is False and
|
|
|
|
utils.settings('show_pms_offline') == 'true'):
|
|
|
|
# Alert the user that server is online
|
|
|
|
utils.dialog('notification',
|
|
|
|
utils.lang(29999),
|
|
|
|
utils.lang(33003),
|
|
|
|
icon='{plex}',
|
|
|
|
time=5000,
|
|
|
|
sound=False)
|
|
|
|
LOG.info("Server is online and ready")
|
|
|
|
if app.ACCOUNT.authenticated:
|
|
|
|
# Server got offline when we were authenticated.
|
|
|
|
# Hence resume threads
|
|
|
|
app.APP.suspend_threads = False
|
|
|
|
app.CONN.online = True
|
|
|
|
finally:
|
|
|
|
self.connection_check_running = False
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
def log_out(self):
|
|
|
|
"""
|
|
|
|
Ensures that lib sync threads are suspended; signs out user
|
|
|
|
"""
|
|
|
|
LOG.info('Log-out requested')
|
2018-11-26 03:03:19 +11:00
|
|
|
app.APP.suspend_threads = True
|
2018-11-19 00:59:17 +11:00
|
|
|
i = 0
|
|
|
|
while app.SYNC.db_scan:
|
|
|
|
i += 1
|
2018-11-27 03:58:15 +11:00
|
|
|
app.APP.monitor.waitForAbort(0.1)
|
|
|
|
if i > 150:
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.error('Could not stop library sync, aborting log-out')
|
|
|
|
# Failed to reset PMS and plex.tv connects. Try to restart Kodi
|
|
|
|
utils.messageDialog(utils.lang(29999), utils.lang(39208))
|
|
|
|
# Resuming threads, just in case
|
2018-11-26 03:03:19 +11:00
|
|
|
app.APP.suspend_threads = False
|
2018-11-19 00:59:17 +11:00
|
|
|
return False
|
|
|
|
LOG.info('Successfully stopped library sync')
|
2018-11-19 18:17:49 +11:00
|
|
|
app.ACCOUNT.log_out()
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.info('User has been logged out')
|
|
|
|
return True
|
|
|
|
|
|
|
|
def choose_pms_server(self, manual=False):
|
|
|
|
LOG.info("Choosing PMS server requested, starting")
|
|
|
|
if manual:
|
|
|
|
if not self.setup.enter_new_pms_address():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
server = self.setup.pick_pms(showDialog=True)
|
2018-11-27 02:56:39 +11:00
|
|
|
if not server:
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.info('We did not connect to a new PMS, aborting')
|
|
|
|
return False
|
|
|
|
LOG.info("User chose server %s", server['name'])
|
2018-12-11 05:19:46 +11:00
|
|
|
if server['machineIdentifier'] == app.CONN.machine_identifier:
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.info('User chose old PMS to connect to')
|
|
|
|
return False
|
2018-11-27 02:56:39 +11:00
|
|
|
# Save changes to to file
|
|
|
|
self.setup.save_pms_settings(server['baseURL'], server['token'])
|
2018-11-19 00:59:17 +11:00
|
|
|
self.setup.write_pms_to_settings(server)
|
|
|
|
if not self.log_out():
|
|
|
|
return False
|
|
|
|
# Wipe Kodi and Plex database as well as playlists and video nodes
|
|
|
|
utils.wipe_database()
|
|
|
|
app.CONN.load()
|
2018-11-27 02:56:39 +11:00
|
|
|
app.ACCOUNT.reset_session()
|
2018-11-26 03:03:19 +11:00
|
|
|
app.ACCOUNT.set_unauthenticated()
|
|
|
|
self.server_has_been_online = False
|
|
|
|
self.welcome_msg = False
|
2018-11-27 02:58:25 +11:00
|
|
|
# Force a full sync
|
|
|
|
app.SYNC.run_lib_scan = 'full'
|
2018-11-19 00:59:17 +11:00
|
|
|
LOG.info("Choosing new PMS complete")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def switch_plex_user(self):
|
|
|
|
if not self.log_out():
|
|
|
|
return False
|
|
|
|
# First remove playlists of old user
|
|
|
|
utils.delete_playlists()
|
|
|
|
# Remove video nodes
|
|
|
|
utils.delete_nodes()
|
2018-11-26 03:03:19 +11:00
|
|
|
app.ACCOUNT.set_unauthenticated()
|
2018-11-26 05:49:47 +11:00
|
|
|
# Force full sync after login
|
2018-12-10 00:36:13 +11:00
|
|
|
utils.settings('lastfullsync', value='0')
|
2018-11-26 05:49:47 +11:00
|
|
|
app.SYNC.run_lib_scan = 'full'
|
2018-11-19 00:59:17 +11:00
|
|
|
return True
|
|
|
|
|
|
|
|
def toggle_plex_tv(self):
|
2018-11-26 03:03:19 +11:00
|
|
|
if app.ACCOUNT.plex_token:
|
|
|
|
LOG.info('Resetting plex.tv credentials in settings')
|
|
|
|
self.log_out()
|
2018-11-19 00:59:17 +11:00
|
|
|
app.ACCOUNT.clear()
|
|
|
|
else:
|
|
|
|
LOG.info('Login to plex.tv')
|
2018-11-26 03:03:19 +11:00
|
|
|
if self.setup.plex_tv_sign_in():
|
|
|
|
self.setup.write_credentials_to_settings()
|
|
|
|
app.ACCOUNT.load()
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-19 18:43:12 +11:00
|
|
|
def authenticate(self):
|
|
|
|
"""
|
|
|
|
Authenticate the current user or prompt to log-in
|
|
|
|
|
|
|
|
Returns True if successful, False if not. 'aborted' if user chose to
|
|
|
|
abort
|
|
|
|
"""
|
2018-11-26 03:03:19 +11:00
|
|
|
if self._do_auth():
|
|
|
|
if self.welcome_msg is True:
|
|
|
|
# Reset authentication warnings
|
|
|
|
self.welcome_msg = False
|
|
|
|
utils.dialog('notification',
|
|
|
|
utils.lang(29999),
|
|
|
|
"%s %s" % (utils.lang(33000),
|
|
|
|
app.ACCOUNT.plex_username),
|
|
|
|
icon='{plex}',
|
|
|
|
time=2000,
|
|
|
|
sound=False)
|
|
|
|
app.APP.suspend_threads = False
|
|
|
|
self.auth_running = False
|
|
|
|
|
|
|
|
def enter_new_pms_address(self):
|
|
|
|
if self.setup.enter_new_pms_address():
|
|
|
|
app.CONN.load()
|
2018-11-27 02:56:39 +11:00
|
|
|
app.ACCOUNT.reset_session()
|
2018-11-26 03:03:19 +11:00
|
|
|
app.ACCOUNT.set_unauthenticated()
|
|
|
|
self.server_has_been_online = False
|
|
|
|
self.welcome_msg = False
|
2018-11-27 02:59:22 +11:00
|
|
|
# Force a full sync
|
|
|
|
app.SYNC.run_lib_scan = 'full'
|
2018-11-26 03:03:19 +11:00
|
|
|
|
|
|
|
def _do_auth(self):
|
2018-11-19 18:43:12 +11:00
|
|
|
LOG.info('Authenticating user')
|
2018-11-26 03:03:19 +11:00
|
|
|
if app.ACCOUNT.plex_username and not app.ACCOUNT.force_login: # Found a user in the settings, try to authenticate
|
2018-11-19 18:43:12 +11:00
|
|
|
LOG.info('Trying to authenticate with old settings')
|
|
|
|
res = PF.check_connection(app.CONN.server,
|
|
|
|
token=app.ACCOUNT.pms_token,
|
|
|
|
verifySSL=app.CONN.verify_ssl_cert)
|
|
|
|
if res is False:
|
|
|
|
LOG.error('Something went wrong while checking connection')
|
|
|
|
return False
|
|
|
|
elif res == 401:
|
2018-11-26 03:03:19 +11:00
|
|
|
LOG.error('User %s no longer has access - signing user out',
|
|
|
|
app.ACCOUNT.plex_username)
|
|
|
|
self.log_out()
|
2018-11-19 18:43:12 +11:00
|
|
|
return False
|
|
|
|
elif res >= 400:
|
|
|
|
LOG.error('Answer from PMS is not as expected')
|
|
|
|
return False
|
|
|
|
LOG.info('Successfully authenticated using old settings')
|
|
|
|
app.ACCOUNT.set_authenticated()
|
|
|
|
return True
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Could not use settings - try to get Plex user list from plex.tv
|
|
|
|
if app.ACCOUNT.plex_token:
|
|
|
|
LOG.info("Trying to connect to plex.tv to get a user list")
|
|
|
|
user, _ = userselect.start()
|
|
|
|
if not user:
|
|
|
|
LOG.info('No user received')
|
2018-11-26 03:03:19 +11:00
|
|
|
app.APP.suspend = True
|
2018-11-19 18:43:12 +11:00
|
|
|
return False
|
|
|
|
username = user.title
|
|
|
|
user_id = user.id
|
|
|
|
token = user.authToken
|
|
|
|
else:
|
|
|
|
LOG.info("Trying to authenticate without a token")
|
|
|
|
username = ''
|
|
|
|
user_id = ''
|
|
|
|
token = ''
|
|
|
|
res = PF.check_connection(app.CONN.server,
|
|
|
|
token=token,
|
|
|
|
verifySSL=app.CONN.verify_ssl_cert)
|
|
|
|
if res is False:
|
|
|
|
LOG.error('Something went wrong while checking connection')
|
|
|
|
return False
|
|
|
|
elif res == 401:
|
|
|
|
if app.ACCOUNT.plex_token:
|
2018-11-26 03:03:19 +11:00
|
|
|
LOG.error('User %s does not have access to PMS %s on %s',
|
|
|
|
username, app.CONN.server_name, app.CONN.server)
|
|
|
|
# "User is unauthorized for server {0}"
|
|
|
|
utils.messageDialog(utils.lang(29999),
|
|
|
|
utils.lang(33010).format(app.CONN.server_name))
|
|
|
|
self.log_out()
|
2018-11-19 18:43:12 +11:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# "Failed to authenticate. Did you login to plex.tv?"
|
|
|
|
utils.messageDialog(utils.lang(29999), utils.lang(39023))
|
|
|
|
if self.setup.plex_tv_sign_in():
|
|
|
|
self.setup.write_credentials_to_settings()
|
|
|
|
app.ACCOUNT.load()
|
|
|
|
continue
|
|
|
|
else:
|
2018-11-26 03:03:19 +11:00
|
|
|
app.APP.suspend = True
|
2018-11-19 18:43:12 +11:00
|
|
|
return False
|
|
|
|
elif res >= 400:
|
|
|
|
LOG.error('Answer from PMS is not as expected')
|
|
|
|
return False
|
|
|
|
LOG.info('Successfully authenticated')
|
|
|
|
# Got new values that need to be saved
|
|
|
|
utils.settings('username', value=username)
|
|
|
|
utils.settings('userid', value=user_id)
|
|
|
|
utils.settings('accessToken', value=token)
|
|
|
|
app.ACCOUNT.load()
|
|
|
|
app.ACCOUNT.set_authenticated()
|
|
|
|
return True
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
def ServiceEntryPoint(self):
|
2019-01-13 23:41:22 +11:00
|
|
|
if not self._init_done:
|
|
|
|
return
|
2018-06-22 03:24:37 +10:00
|
|
|
# Important: Threads depending on abortRequest will not trigger
|
|
|
|
# if profile switch happens more than once.
|
2018-11-19 00:59:17 +11:00
|
|
|
# Some plumbing
|
2018-11-26 03:03:19 +11:00
|
|
|
app.init()
|
2018-11-21 02:58:25 +11:00
|
|
|
app.APP.monitor = kodimonitor.KodiMonitor()
|
2018-11-23 18:41:05 +11:00
|
|
|
app.APP.player = xbmc.Player()
|
2018-11-19 00:59:17 +11:00
|
|
|
artwork.IMAGE_CACHING_SUSPENDS = [
|
2018-11-26 03:03:19 +11:00
|
|
|
app.APP.suspend_threads,
|
2018-11-19 00:59:17 +11:00
|
|
|
app.SYNC.stop_sync,
|
|
|
|
app.SYNC.db_scan
|
|
|
|
]
|
|
|
|
if not utils.settings('imageSyncDuringPlayback') == 'true':
|
|
|
|
artwork.IMAGE_CACHING_SUSPENDS.append(app.SYNC.suspend_sync)
|
|
|
|
# Initialize the PKC playqueues
|
|
|
|
PQ.init_playqueues()
|
2018-06-22 03:24:37 +10:00
|
|
|
|
|
|
|
# Server auto-detect
|
2018-11-19 00:59:17 +11:00
|
|
|
self.setup = initialsetup.InitialSetup()
|
|
|
|
self.setup.setup()
|
2018-06-22 03:24:37 +10:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
# Initialize important threads
|
2018-06-22 03:24:37 +10:00
|
|
|
self.ws = websocket_client.PMS_Websocket()
|
|
|
|
self.alexa = websocket_client.Alexa_Websocket()
|
2018-10-25 02:19:36 +11:00
|
|
|
self.sync = sync.Sync()
|
2018-06-22 03:24:37 +10:00
|
|
|
self.plexcompanion = plex_companion.PlexCompanion()
|
|
|
|
self.playqueue = playqueue.PlayqueueMonitor()
|
|
|
|
|
2018-11-26 03:03:19 +11:00
|
|
|
# Main PKC program loop
|
2018-12-11 05:51:17 +11:00
|
|
|
while not self.isCanceled():
|
2018-11-26 03:03:19 +11:00
|
|
|
# Check for Kodi profile change
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.window('plex_kodiProfile') != v.KODI_PROFILE:
|
|
|
|
# Profile change happened, terminate this thread and others
|
|
|
|
LOG.info("Kodi profile was: %s and changed to: %s. "
|
|
|
|
"Terminating old PlexKodiConnect thread.",
|
|
|
|
v.KODI_PROFILE, utils.window('plex_kodiProfile'))
|
|
|
|
break
|
|
|
|
|
2018-11-26 03:03:19 +11:00
|
|
|
# Check for PKC commands from other Python instances
|
2018-11-19 00:59:17 +11:00
|
|
|
plex_command = utils.window('plex_command')
|
|
|
|
if plex_command:
|
|
|
|
# Commands/user interaction received from other PKC Python
|
|
|
|
# instances (default.py and context.py instead of service.py)
|
|
|
|
utils.window('plex_command', clear=True)
|
2018-11-26 17:48:45 +11:00
|
|
|
task = None
|
2018-11-19 00:59:17 +11:00
|
|
|
if plex_command.startswith('PLAY-'):
|
|
|
|
# Add-on path playback!
|
2018-11-26 17:48:45 +11:00
|
|
|
task = playback_starter.PlaybackTask(
|
2018-11-19 00:59:17 +11:00
|
|
|
plex_command.replace('PLAY-', ''))
|
|
|
|
elif plex_command.startswith('NAVIGATE-'):
|
2018-11-26 17:48:45 +11:00
|
|
|
task = playback_starter.PlaybackTask(
|
2018-11-19 00:59:17 +11:00
|
|
|
plex_command.replace('NAVIGATE-', ''))
|
|
|
|
elif plex_command.startswith('CONTEXT_menu?'):
|
2018-11-26 17:48:45 +11:00
|
|
|
task = playback_starter.PlaybackTask(
|
2018-11-19 00:59:17 +11:00
|
|
|
'dummy?mode=context_menu&%s'
|
|
|
|
% plex_command.replace('CONTEXT_menu?', ''))
|
|
|
|
elif plex_command == 'choose_pms_server':
|
2018-11-26 03:03:19 +11:00
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
self.choose_pms_server, None)
|
2018-11-19 00:59:17 +11:00
|
|
|
elif plex_command == 'switch_plex_user':
|
2018-11-26 03:03:19 +11:00
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
self.switch_plex_user, None)
|
2018-11-19 00:59:17 +11:00
|
|
|
elif plex_command == 'enter_new_pms_address':
|
2018-11-26 03:03:19 +11:00
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
self.enter_new_pms_address, None)
|
2018-11-19 00:59:17 +11:00
|
|
|
elif plex_command == 'toggle_plex_tv_sign_in':
|
2018-11-26 03:03:19 +11:00
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
self.toggle_plex_tv, None)
|
2018-11-19 00:59:17 +11:00
|
|
|
elif plex_command == 'repair-scan':
|
|
|
|
app.SYNC.run_lib_scan = 'repair'
|
|
|
|
elif plex_command == 'full-scan':
|
|
|
|
app.SYNC.run_lib_scan = 'full'
|
|
|
|
elif plex_command == 'fanart-scan':
|
|
|
|
app.SYNC.run_lib_scan = 'fanart'
|
|
|
|
elif plex_command == 'textures-scan':
|
|
|
|
app.SYNC.run_lib_scan = 'textures'
|
2018-11-26 07:07:25 +11:00
|
|
|
elif plex_command == 'RESET-PKC':
|
|
|
|
utils.reset()
|
2018-11-26 17:48:45 +11:00
|
|
|
if task:
|
|
|
|
backgroundthread.BGThreader.addTasksToFront([task])
|
2018-11-19 00:59:17 +11:00
|
|
|
continue
|
|
|
|
|
2018-11-26 03:03:19 +11:00
|
|
|
if app.APP.suspend:
|
|
|
|
app.APP.monitor.waitForAbort(0.1)
|
|
|
|
continue
|
|
|
|
|
2018-11-26 17:31:36 +11:00
|
|
|
# Detect the resume dialog for widgets. Could also be used to detect
|
|
|
|
# external players (see Emby implementation)
|
|
|
|
if xbmc.getCondVisibility('Window.IsVisible(DialogContextMenu.xml)'):
|
|
|
|
if xbmc.getInfoLabel('Control.GetLabel(1002)') in STRINGS:
|
|
|
|
# Remember that the item IS indeed resumable
|
|
|
|
control = int(xbmcgui.Window(10106).getFocusId())
|
|
|
|
app.PLAYSTATE.resume_playback = True if control == 1001 else False
|
|
|
|
else:
|
|
|
|
# Different context menu is displayed
|
|
|
|
app.PLAYSTATE.resume_playback = False
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
# Before proceeding, need to make sure:
|
|
|
|
# 1. Server is online
|
|
|
|
# 2. User is set
|
|
|
|
# 3. User has access to the server
|
2018-11-26 03:03:19 +11:00
|
|
|
if not app.CONN.online:
|
|
|
|
# Not online
|
2018-11-19 00:59:17 +11:00
|
|
|
server = app.CONN.server
|
|
|
|
if not server:
|
|
|
|
# No server info set in add-on settings
|
|
|
|
pass
|
2018-11-26 03:03:19 +11:00
|
|
|
elif not self.connection_check_running:
|
|
|
|
self.connection_check_running = True
|
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
PF.check_connection,
|
|
|
|
self.on_connection_check,
|
|
|
|
server,
|
|
|
|
verifySSL=True)
|
|
|
|
backgroundthread.BGThreader.addTasksToFront([task])
|
|
|
|
continue
|
|
|
|
elif not app.ACCOUNT.authenticated:
|
|
|
|
# Plex server is online, but we're not yet authenticated
|
|
|
|
if not self.auth_running:
|
|
|
|
self.auth_running = True
|
|
|
|
task = backgroundthread.FunctionAsTask(
|
|
|
|
self.authenticate, None)
|
|
|
|
backgroundthread.BGThreader.addTasksToFront([task])
|
|
|
|
continue
|
|
|
|
elif not self.startup_completed:
|
|
|
|
self.startup_completed = True
|
|
|
|
self.ws.start()
|
|
|
|
self.sync.start()
|
|
|
|
self.plexcompanion.start()
|
|
|
|
self.playqueue.start()
|
|
|
|
if utils.settings('enable_alexa') == 'true':
|
|
|
|
self.alexa.start()
|
|
|
|
|
|
|
|
app.APP.monitor.waitForAbort(0.1)
|
|
|
|
|
|
|
|
# EXITING PKC
|
2018-06-22 03:24:37 +10:00
|
|
|
# Tell all threads to terminate (e.g. several lib sync threads)
|
2018-11-19 00:59:17 +11:00
|
|
|
app.APP.stop_pkc = True
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.window('plex_service_started', clear=True)
|
|
|
|
LOG.info("======== STOP %s ========", v.ADDON_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
# Safety net - Kody starts PKC twice upon first installation!
|
|
|
|
if utils.window('plex_service_started') == 'true':
|
|
|
|
EXIT = True
|
|
|
|
else:
|
|
|
|
utils.window('plex_service_started', value='true')
|
|
|
|
EXIT = False
|
|
|
|
|
|
|
|
# Delay option
|
|
|
|
DELAY = int(utils.settings('startupDelay'))
|
|
|
|
|
|
|
|
LOG.info("Delaying Plex startup by: %s sec...", DELAY)
|
|
|
|
if EXIT:
|
|
|
|
LOG.error('PKC service.py already started - exiting this instance')
|
|
|
|
elif DELAY and xbmc.Monitor().waitForAbort(DELAY):
|
|
|
|
# Start the service
|
|
|
|
LOG.info("Abort requested while waiting. PKC not started.")
|
|
|
|
else:
|
|
|
|
Service().ServiceEntryPoint()
|