PlexKodiConnect/resources/lib/sync.py

347 lines
15 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-08-18 18:38:03 +10:00
from logging import getLogger
2015-12-25 07:07:00 +11:00
import xbmc
2018-10-24 16:08:32 +11:00
from .downloadutils import DownloadUtils as DU
2018-11-06 00:03:19 +11:00
from . import library_sync
from . import backgroundthread, utils, path_ops, artwork, variables as v, state
2018-11-02 01:43:27 +11:00
from . import plex_db, kodidb_functions as kodidb
2015-12-25 07:07:00 +11:00
2018-10-25 02:19:36 +11:00
LOG = getLogger('PLEX.sync')
2015-12-25 07:07:00 +11:00
2018-10-24 16:08:32 +11:00
def set_library_scan_toggle(boolean=True):
"""
Make sure to hit this function before starting large scans
"""
if not boolean:
# Deactivate
state.DB_SCAN = False
utils.window('plex_dbScan', clear=True)
else:
state.DB_SCAN = True
utils.window('plex_dbScan', value="true")
2016-09-02 03:07:28 +10:00
2018-10-25 02:19:36 +11:00
class Sync(backgroundthread.KillableThread):
2016-03-25 04:52:02 +11:00
"""
The one and only library sync thread. Spawn only 1!
2016-03-25 04:52:02 +11:00
"""
def __init__(self):
2018-10-24 16:08:32 +11:00
self.sync_successful = False
self.last_full_sync = 0
2018-11-03 20:36:37 +11:00
self.fanart = None
# Show sync dialog even if user deactivated?
2018-10-24 16:08:32 +11:00
self.force_dialog = False
if utils.settings('enableTextureCache') == "true":
2018-11-06 00:18:52 +11:00
self.image_cache_thread = artwork.ImageCachingThread()
2018-10-24 16:08:32 +11:00
# Lock used to wait on a full sync, e.g. on initial sync
# self.lock = backgroundthread.threading.Lock()
2018-10-25 02:19:36 +11:00
super(Sync, self).__init__()
2018-10-24 16:08:32 +11:00
def isCanceled(self):
2018-11-06 00:03:19 +11:00
return state.STOP_PKC
2018-10-24 16:08:32 +11:00
def isSuspended(self):
2018-11-06 00:10:43 +11:00
return state.SUSPEND_LIBRARY_THREAD
2015-12-25 07:07:00 +11:00
def suspend_item_sync(self):
"""
Returns True if we should not sync new items or artwork to Kodi or even
abort a sync currently running.
Returns False otherwise.
"""
2018-10-24 16:08:32 +11:00
if self.isSuspended() or self.isCanceled():
return True
elif state.SUSPEND_SYNC:
return True
return False
2018-11-03 20:36:37 +11:00
def show_kodi_note(self, message, icon="plex", force=False):
2016-02-12 00:44:11 +11:00
"""
2016-03-08 22:13:47 +11:00
Shows a Kodi popup, if user selected to do so. Pass message in unicode
or string
icon: "plex": shows Plex icon
"error": shows Kodi error icon
2016-02-12 00:44:11 +11:00
"""
2018-11-03 20:36:37 +11:00
if not force and state.SYNC_DIALOG is not True and self.force_dialog is not True:
return
if icon == "plex":
2018-06-22 03:24:37 +10:00
utils.dialog('notification',
heading='{plex}',
message=message,
icon='{plex}',
sound=False)
elif icon == "error":
2018-06-22 03:24:37 +10:00
utils.dialog('notification',
heading='{plex}',
message=message,
icon='{error}')
2016-02-12 00:44:11 +11:00
2017-08-22 02:53:38 +10:00
def triage_lib_scans(self):
"""
2017-08-22 03:38:41 +10:00
Decides what to do if state.RUN_LIB_SCAN has been set. E.g. manually
triggered full or repair syncs
2017-08-22 02:53:38 +10:00
"""
if state.RUN_LIB_SCAN in ("full", "repair"):
2018-10-24 16:08:32 +11:00
set_library_scan_toggle()
2018-04-16 02:33:20 +10:00
LOG.info('Full library scan requested, starting')
2018-10-24 16:08:32 +11:00
self.start_library_sync(show_dialog=True,
repair=state.RUN_LIB_SCAN == 'repair',
block=True)
if self.sync_successful:
# Full library sync finished
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39407))
elif not self.suspend_item_sync():
self.force_dialog = True
# ERROR in library sync
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39410), icon='error')
self.force_dialog = False
2017-08-22 02:53:38 +10:00
elif state.RUN_LIB_SCAN == 'fanart':
2018-11-03 20:36:37 +11:00
# Only look for missing fanart (No) or refresh all fanart (Yes)
2018-09-19 00:26:40 +10:00
from .windows import optionsdialog
refresh = optionsdialog.show(utils.lang(29999),
utils.lang(39223),
utils.lang(39224), # refresh all
utils.lang(39225)) == 0
2018-11-03 20:36:37 +11:00
if not self.start_fanart_download(refresh=refresh):
2018-11-06 00:13:25 +11:00
# Fanart download already running
2018-11-03 20:36:37 +11:00
utils.dialog('notification',
heading='{plex}',
2018-11-06 00:13:25 +11:00
message=utils.lang(30015),
2018-11-03 20:36:37 +11:00
icon='{plex}',
sound=False)
2017-08-22 02:53:38 +10:00
elif state.RUN_LIB_SCAN == 'textures':
artwork.Artwork().fullTextureCacheSync()
2018-10-24 16:08:32 +11:00
2018-11-03 20:36:37 +11:00
def on_library_scan_finished(self, successful):
2018-10-24 16:08:32 +11:00
"""
Hit this after the full sync has finished
"""
self.sync_successful = successful
self.last_full_sync = utils.unix_timestamp()
set_library_scan_toggle(boolean=False)
# try:
# self.lock.release()
# except backgroundthread.threading.ThreadError:
# pass
2018-10-24 16:08:32 +11:00
def start_library_sync(self, show_dialog=None, repair=False, block=False):
show_dialog = show_dialog if show_dialog is not None else state.SYNC_DIALOG
library_sync.start(show_dialog, repair, self.on_library_scan_finished)
# if block:
# self.lock.acquire()
# Will block until scan is finished
# self.lock.acquire()
# self.lock.release()
2018-11-03 20:36:37 +11:00
def start_fanart_download(self, refresh):
if not utils.settings('FanartTV') == 'true':
LOG.info('Additional fanart download is deactivated')
return False
elif self.fanart is None or not self.fanart.is_alive():
LOG.info('Start downloading additional fanart with refresh %s',
refresh)
self.fanart = library_sync.FanartThread(self.on_fanart_download_finished, refresh)
self.fanart.start()
return True
else:
LOG.info('Still downloading fanart')
return False
def on_fanart_download_finished(self):
# FanartTV lookup completed
if state.SYNC_DIALOG:
utils.dialog('notification',
heading='{plex}',
message=utils.lang(30019),
icon='{plex}',
sound=False)
2017-08-22 02:53:38 +10:00
2016-08-07 23:33:36 +10:00
def run(self):
2015-12-25 07:07:00 +11:00
try:
self._run_internal()
2018-10-24 16:08:32 +11:00
except:
state.DB_SCAN = False
2018-06-22 03:24:37 +10:00
utils.window('plex_dbScan', clear=True)
2018-11-03 20:36:37 +11:00
utils.ERROR(txt='sync.py crashed', notify=True)
2015-12-25 07:07:00 +11:00
raise
def _run_internal(self):
2018-11-06 00:03:19 +11:00
LOG.info("---===### Starting Sync Thread ###===---")
2018-10-24 16:08:32 +11:00
install_sync_done = utils.settings('SyncInstallRunDone') == 'true'
playlist_monitor = None
initial_sync_done = False
2018-11-02 01:43:27 +11:00
last_websocket_processing = 0
2018-05-20 22:28:56 +10:00
last_time_sync = 0
2018-06-22 03:24:37 +10:00
one_day_in_seconds = 60 * 60 * 24
2016-12-28 03:33:52 +11:00
# Link to Websocket queue
queue = state.WEBSOCKET_QUEUE
2016-03-25 04:52:02 +11:00
2018-10-25 21:46:57 +11:00
# Kodi Version supported by PKC?
2018-10-24 16:08:32 +11:00
if (not path_ops.exists(v.DB_VIDEO_PATH) or
not path_ops.exists(v.DB_TEXTURE_PATH) or
(state.ENABLE_MUSIC and not path_ops.exists(v.DB_MUSIC_PATH))):
# Database does not exists
2018-10-24 16:08:32 +11:00
LOG.error('The current Kodi version is incompatible')
2018-06-22 03:24:37 +10:00
LOG.error('Current Kodi version: %s', utils.try_decode(
xbmc.getInfoLabel('System.BuildVersion')))
# "Current Kodi version is unsupported, cancel lib sync"
2018-09-19 00:26:40 +10:00
utils.messageDialog(utils.lang(29999), utils.lang(39403))
return
2018-10-25 21:46:57 +11:00
# Check whether we need to reset the Kodi DB
if install_sync_done:
current_version = utils.settings('dbCreatedWithVersion')
if not utils.compare_version(current_version,
v.MIN_DB_VERSION):
LOG.warn("Db version out of date: %s minimum version "
"required: %s", current_version, v.MIN_DB_VERSION)
# DB out of date. Proceed to recreate?
if not utils.yesno_dialog(utils.lang(29999),
utils.lang(39401)):
LOG.warn("Db version out of date! USER IGNORED!")
# PKC may not work correctly until reset
utils.messageDialog(utils.lang(29999),
'%s%s' % (utils.lang(29999),
utils.lang(39402)))
else:
utils.reset(ask_user=False)
return
2018-10-24 16:08:32 +11:00
# Ensure that Plex DB is set-up
plex_db.initialize()
# Hack to speed up look-ups for actors (giant table!)
utils.create_actor_db_index()
with kodidb.GetKodiDB('video') as kodi_db:
# Setup the paths for addon-paths (even when using direct paths)
kodi_db.setup_path_table()
2018-10-24 16:08:32 +11:00
while not self.isCanceled():
# In the event the server goes offline
2018-10-24 16:08:32 +11:00
while self.isSuspended():
if self.isCanceled():
2015-12-25 07:07:00 +11:00
# Abort was requested while waiting. We should exit
2018-11-06 00:03:19 +11:00
LOG.info("###===--- Sync Thread Stopped ---===###")
2016-01-28 06:41:28 +11:00
return
2016-02-11 20:56:01 +11:00
xbmc.sleep(1000)
2015-12-25 07:07:00 +11:00
2018-10-24 16:08:32 +11:00
if not install_sync_done:
# Very FIRST sync ever upon installation or reset of Kodi DB
2018-10-25 21:46:57 +11:00
self.force_dialog = True
2018-10-24 16:08:32 +11:00
set_library_scan_toggle()
2018-05-20 22:28:56 +10:00
# Initialize time offset Kodi - PMS
2018-10-24 16:08:32 +11:00
library_sync.sync_pms_time()
2018-05-20 22:28:56 +10:00
last_time_sync = utils.unix_timestamp()
LOG.info('Initial start-up full sync starting')
xbmc.executebuiltin('InhibitIdleShutdown(true)')
2018-10-24 16:08:32 +11:00
# This call will block until scan is completed
self.start_library_sync(show_dialog=True, block=True)
if self.sync_successful:
LOG.info('Initial start-up full sync successful')
2018-06-22 03:24:37 +10:00
utils.settings('SyncInstallRunDone', value='true')
2018-10-24 16:08:32 +11:00
install_sync_done = True
2018-10-25 21:46:57 +11:00
initial_sync_done = True
2018-06-22 03:24:37 +10:00
utils.settings('dbCreatedWithVersion', v.ADDON_VERSION)
self.force_dialog = False
2018-10-24 16:08:32 +11:00
if library_sync.PLAYLIST_SYNC_ENABLED:
from . import playlists
playlist_monitor = playlists.kodi_playlist_monitor()
2018-11-03 20:36:37 +11:00
self.start_fanart_download(refresh=False)
self.image_cache_thread.start()
else:
LOG.error('Initial start-up full sync unsuccessful')
self.force_dialog = False
2018-10-25 21:46:57 +11:00
xbmc.executebuiltin('InhibitIdleShutdown(false)')
elif not initial_sync_done:
# First sync upon PKC restart. Skipped if very first sync upon
# PKC installation has been completed
2018-10-24 16:08:32 +11:00
set_library_scan_toggle()
LOG.info('Doing initial sync on Kodi startup')
if state.SUSPEND_SYNC:
LOG.warning('Forcing startup sync even if Kodi is playing')
state.SUSPEND_SYNC = False
2018-10-24 16:08:32 +11:00
self.start_library_sync(block=True)
if self.sync_successful:
initial_sync_done = True
LOG.info('Done initial sync on Kodi startup')
2018-10-24 16:08:32 +11:00
if library_sync.PLAYLIST_SYNC_ENABLED:
from . import playlists
playlist_monitor = playlists.kodi_playlist_monitor()
2018-04-29 22:26:53 +10:00
artwork.Artwork().cache_major_artwork()
2018-11-03 20:36:37 +11:00
self.start_fanart_download(refresh=False)
self.image_cache_thread.start()
else:
LOG.info('Startup sync has not yet been successful')
2016-01-28 06:41:28 +11:00
2018-10-24 16:08:32 +11:00
# Currently no db scan, so we could start a new scan
elif state.DB_SCAN is False:
2016-01-28 06:41:28 +11:00
# Full scan was requested from somewhere else, e.g. userclient
2017-08-22 02:53:38 +10:00
if state.RUN_LIB_SCAN is not None:
# Force-show dialogs since they are user-initiated
self.force_dialog = True
self.triage_lib_scans()
self.force_dialog = False
2018-10-24 16:08:32 +11:00
# Reset the flag
state.RUN_LIB_SCAN = None
2017-08-22 02:53:38 +10:00
continue
2018-10-24 16:08:32 +11:00
2017-08-22 02:53:38 +10:00
# Standard syncs - don't force-show dialogs
2018-10-24 16:08:32 +11:00
now = utils.unix_timestamp()
2017-08-22 02:53:38 +10:00
self.force_dialog = False
2018-10-24 16:08:32 +11:00
if (now - self.last_full_sync > state.FULL_SYNC_INTERVALL):
2018-04-16 02:33:20 +10:00
LOG.info('Doing scheduled full library scan')
2018-10-24 16:08:32 +11:00
set_library_scan_toggle()
success = self.maintain_views()
if success:
2018-10-24 16:08:32 +11:00
success = library_sync.start()
if not success and not self.suspend_item_sync():
2018-04-16 02:33:20 +10:00
LOG.error('Could not finish scheduled full sync')
2017-08-22 02:53:38 +10:00
self.force_dialog = True
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39410),
icon='error')
2017-08-22 02:53:38 +10:00
self.force_dialog = False
elif success:
2018-10-24 16:08:32 +11:00
self.last_full_sync = now
# Full library sync finished successfully
2018-06-22 03:24:37 +10:00
self.show_kodi_note(utils.lang(39407))
else:
LOG.info('Full sync interrupted')
elif now - last_time_sync > one_day_in_seconds:
2018-04-16 02:33:20 +10:00
LOG.info('Starting daily time sync')
2018-10-24 16:08:32 +11:00
library_sync.sync_pms_time()
last_time_sync = now
elif not state.BACKGROUND_SYNC_DISABLED:
2018-11-02 01:43:27 +11:00
# Check back whether we should process something Only do
# this once a while (otherwise, potentially many screen
# refreshes lead to flickering)
if (library_sync.WEBSOCKET_MESSAGES and
now - last_websocket_processing > 5):
last_websocket_processing = now
library_sync.process_websocket_messages()
2017-08-22 02:53:38 +10:00
# See if there is a PMS message we need to handle
try:
message = queue.get(block=False)
2018-10-24 16:08:32 +11:00
except backgroundthread.Queue.Empty:
pass
2017-08-22 02:53:38 +10:00
# Got a message from PMS; process it
else:
2018-11-02 01:43:27 +11:00
library_sync.store_websocket_message(message)
2017-08-22 02:53:38 +10:00
queue.task_done()
# Sleep just a bit
xbmc.sleep(10)
2017-08-22 02:53:38 +10:00
continue
xbmc.sleep(100)
2018-04-28 17:12:29 +10:00
# Shut down playlist monitoring
2018-05-01 22:48:49 +10:00
if playlist_monitor:
playlist_monitor.stop()
2016-04-10 00:57:45 +10:00
# doUtils could still have a session open due to interrupted sync
try:
DU().stopSession()
2018-10-24 16:08:32 +11:00
except AttributeError:
2016-04-10 00:57:45 +10:00
pass
2018-11-06 00:03:19 +11:00
LOG.info("###===--- Sync Thread Stopped ---===###")