2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
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-19 00:59:17 +11:00
|
|
|
from . import library_sync, timing
|
2019-01-13 23:41:22 +11:00
|
|
|
from . import backgroundthread, utils, artwork, variables as v, app
|
2018-12-22 02:48:57 +11:00
|
|
|
from . import kodi_db
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2019-02-03 01:49:21 +11:00
|
|
|
if library_sync.PLAYLIST_SYNC_ENABLED:
|
|
|
|
from . import playlists
|
|
|
|
|
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
|
|
|
|
2018-10-25 02:19:36 +11:00
|
|
|
class Sync(backgroundthread.KillableThread):
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
The one and only library sync thread. Spawn only 1!
|
2016-03-25 04:52:02 +11:00
|
|
|
"""
|
2018-01-07 01:19:12 +11:00
|
|
|
def __init__(self):
|
2018-10-24 16:08:32 +11:00
|
|
|
self.sync_successful = False
|
|
|
|
self.last_full_sync = 0
|
2021-01-12 02:21:37 +11:00
|
|
|
self.metadata_thread = None
|
2018-11-06 01:23:51 +11:00
|
|
|
self.image_cache_thread = None
|
2018-10-24 16:08:32 +11:00
|
|
|
# Lock used to wait on a full sync, e.g. on initial sync
|
2018-11-05 23:52:31 +11:00
|
|
|
# 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
|
|
|
|
2017-08-22 02:53:38 +10:00
|
|
|
def triage_lib_scans(self):
|
|
|
|
"""
|
2018-11-19 00:59:17 +11:00
|
|
|
Decides what to do if app.SYNC.run_lib_scan has been set. E.g. manually
|
2017-08-22 03:38:41 +10:00
|
|
|
triggered full or repair syncs
|
2017-08-22 02:53:38 +10:00
|
|
|
"""
|
2018-11-19 00:59:17 +11:00
|
|
|
if app.SYNC.run_lib_scan in ("full", "repair"):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Full library scan requested, starting')
|
2020-06-10 01:09:12 +10:00
|
|
|
show_dialog = True if app.SYNC.run_lib_scan == 'repair' \
|
|
|
|
else None
|
|
|
|
self.start_library_sync(show_dialog=show_dialog,
|
2018-11-19 00:59:17 +11:00
|
|
|
repair=app.SYNC.run_lib_scan == 'repair',
|
2018-10-24 16:08:32 +11:00
|
|
|
block=True)
|
2019-11-29 03:49:48 +11:00
|
|
|
if (not self.sync_successful and
|
|
|
|
not self.should_suspend() and
|
|
|
|
not self.should_cancel()):
|
2018-04-18 04:18:25 +10:00
|
|
|
# ERROR in library sync
|
2019-01-30 19:34:38 +11:00
|
|
|
LOG.warn('Triggered full/repair sync has not been successful')
|
2018-11-19 00:59:17 +11:00
|
|
|
elif app.SYNC.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
|
2021-01-12 02:21:37 +11:00
|
|
|
if not self.start_additional_metadata(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)
|
2018-11-19 00:59:17 +11:00
|
|
|
elif app.SYNC.run_lib_scan == 'textures':
|
2018-11-06 04:00:01 +11:00
|
|
|
LOG.info("Caching of images requested")
|
|
|
|
if not utils.yesno_dialog("Image Texture Cache", utils.lang(39250)):
|
|
|
|
return
|
|
|
|
# ask to reset all existing or not
|
|
|
|
if utils.yesno_dialog('Image Texture Cache', utils.lang(39251)):
|
2018-11-09 07:22:16 +11:00
|
|
|
kodi_db.reset_cached_images()
|
2018-11-06 04:00:01 +11:00
|
|
|
self.start_image_cache_thread()
|
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
|
2018-11-19 00:59:17 +11:00
|
|
|
self.last_full_sync = timing.unix_timestamp()
|
2018-12-10 17:35:46 +11:00
|
|
|
if not successful:
|
2019-01-30 19:34:38 +11:00
|
|
|
LOG.warn('Could not finish scheduled full sync')
|
2021-01-12 02:21:37 +11:00
|
|
|
app.APP.resume_metadata_thread()
|
2019-01-31 06:36:52 +11:00
|
|
|
app.APP.resume_caching_thread()
|
2018-10-24 16:08:32 +11:00
|
|
|
|
|
|
|
def start_library_sync(self, show_dialog=None, repair=False, block=False):
|
2021-01-12 02:21:37 +11:00
|
|
|
app.APP.suspend_metadata_thread(block=True)
|
2019-01-31 06:36:52 +11:00
|
|
|
app.APP.suspend_caching_thread(block=True)
|
2018-11-19 00:59:17 +11:00
|
|
|
show_dialog = show_dialog if show_dialog is not None else app.SYNC.sync_dialog
|
2018-11-05 23:52:31 +11:00
|
|
|
library_sync.start(show_dialog, repair, self.on_library_scan_finished)
|
2018-11-03 20:36:37 +11:00
|
|
|
|
2021-01-12 02:21:37 +11:00
|
|
|
def start_additional_metadata(self, refresh):
|
2019-01-05 06:38:45 +11:00
|
|
|
if not app.SYNC.artwork:
|
|
|
|
LOG.info('Not synching Plex PMS artwork, not getting artwork')
|
|
|
|
return False
|
2021-01-12 02:21:37 +11:00
|
|
|
elif self.metadata_thread is None or not self.metadata_thread.is_alive():
|
|
|
|
LOG.info('Start downloading additional metadata with refresh %s',
|
2018-11-03 20:36:37 +11:00
|
|
|
refresh)
|
2021-01-12 02:21:37 +11:00
|
|
|
self.metadata_thread = library_sync.MetadataThread(self.on_metadata_finished, refresh)
|
|
|
|
self.metadata_thread.start()
|
2018-11-03 20:36:37 +11:00
|
|
|
return True
|
|
|
|
else:
|
2021-01-12 02:21:37 +11:00
|
|
|
LOG.info('Still downloading metadata')
|
2018-11-03 20:36:37 +11:00
|
|
|
return False
|
|
|
|
|
2021-01-12 02:21:37 +11:00
|
|
|
@staticmethod
|
|
|
|
def on_metadata_finished(successful):
|
2018-11-03 20:36:37 +11:00
|
|
|
# FanartTV lookup completed
|
2018-12-09 23:45:09 +11:00
|
|
|
if successful:
|
|
|
|
# Toggled to "Yes"
|
|
|
|
utils.settings('plex_status_fanarttv_lookup', value=utils.lang(107))
|
2017-08-22 02:53:38 +10:00
|
|
|
|
2018-11-06 01:23:51 +11:00
|
|
|
def start_image_cache_thread(self):
|
2018-12-10 00:16:33 +11:00
|
|
|
if not utils.settings('enableTextureCache') == "true":
|
2018-11-06 01:23:51 +11:00
|
|
|
LOG.info('Image caching has been deactivated')
|
2018-11-06 04:00:01 +11:00
|
|
|
return
|
2019-01-05 06:38:45 +11:00
|
|
|
if not app.SYNC.artwork:
|
|
|
|
LOG.info('Not synching Plex artwork - not caching')
|
|
|
|
return
|
2018-11-06 04:00:01 +11:00
|
|
|
if self.image_cache_thread and self.image_cache_thread.is_alive():
|
2019-11-29 03:49:48 +11:00
|
|
|
self.image_cache_thread.cancel()
|
2018-11-06 04:00:01 +11:00
|
|
|
self.image_cache_thread.join()
|
|
|
|
self.image_cache_thread = artwork.ImageCachingThread()
|
|
|
|
self.image_cache_thread.start()
|
2018-11-06 01:23:51 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def run(self):
|
2019-01-31 06:36:52 +11:00
|
|
|
LOG.info("---===### Starting Sync Thread ###===---")
|
|
|
|
app.APP.register_thread(self)
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2018-04-18 04:18:25 +10:00
|
|
|
self._run_internal()
|
2019-02-03 06:22:06 +11:00
|
|
|
except Exception:
|
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
|
2019-01-31 06:36:52 +11:00
|
|
|
finally:
|
2019-02-09 01:29:25 +11:00
|
|
|
try:
|
|
|
|
app.APP.deregister_thread(self)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-01-31 06:36:52 +11:00
|
|
|
LOG.info("###===--- Sync Thread Stopped ---===###")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
def _run_internal(self):
|
2018-10-24 16:08:32 +11:00
|
|
|
install_sync_done = utils.settings('SyncInstallRunDone') == 'true'
|
|
|
|
playlist_monitor = None
|
2018-04-18 04:18:25 +10:00
|
|
|
initial_sync_done = False
|
2018-11-02 01:43:27 +11:00
|
|
|
last_websocket_processing = 0
|
2016-12-28 03:33:52 +11:00
|
|
|
# Link to Websocket queue
|
2018-11-19 00:59:17 +11:00
|
|
|
queue = app.APP.websocket_queue
|
2016-03-25 04:52:02 +11:00
|
|
|
|
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)
|
2019-02-09 01:29:25 +11:00
|
|
|
# In order to not wait for this thread to suspend
|
|
|
|
app.APP.deregister_thread(self)
|
2018-10-25 21:46:57 +11:00
|
|
|
# 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-12-10 01:25:30 +11:00
|
|
|
|
|
|
|
utils.init_dbs()
|
2018-04-18 04:18:25 +10:00
|
|
|
|
2019-11-29 03:49:48 +11:00
|
|
|
while not self.should_cancel():
|
2016-04-08 17:11:03 +10:00
|
|
|
# In the event the server goes offline
|
2019-11-29 03:49:48 +11:00
|
|
|
if self.should_suspend():
|
|
|
|
if self.wait_while_suspended():
|
|
|
|
return
|
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-04-18 04:18:25 +10:00
|
|
|
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:
|
2018-04-18 04:18:25 +10:00
|
|
|
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)
|
2019-06-16 21:49:42 +10:00
|
|
|
# Reload skin in order to ensure items showing up on Kodi
|
|
|
|
# Homescreen
|
|
|
|
xbmc.executebuiltin('ReloadSkin()')
|
2018-10-24 16:08:32 +11:00
|
|
|
if library_sync.PLAYLIST_SYNC_ENABLED:
|
2018-08-06 02:09:48 +10:00
|
|
|
playlist_monitor = playlists.kodi_playlist_monitor()
|
2021-01-12 02:21:37 +11:00
|
|
|
self.start_additional_metadata(refresh=False)
|
2018-11-06 01:23:51 +11:00
|
|
|
self.start_image_cache_thread()
|
2018-04-18 04:18:25 +10:00
|
|
|
else:
|
|
|
|
LOG.error('Initial start-up full sync unsuccessful')
|
2019-11-29 03:49:48 +11:00
|
|
|
self.sleep(1)
|
2018-10-25 21:46:57 +11:00
|
|
|
xbmc.executebuiltin('InhibitIdleShutdown(false)')
|
2018-04-18 04:18:25 +10:00
|
|
|
|
|
|
|
elif not initial_sync_done:
|
|
|
|
# First sync upon PKC restart. Skipped if very first sync upon
|
|
|
|
# PKC installation has been completed
|
|
|
|
LOG.info('Doing initial sync on Kodi startup')
|
2018-10-24 16:08:32 +11:00
|
|
|
self.start_library_sync(block=True)
|
|
|
|
if self.sync_successful:
|
2018-04-18 04:18:25 +10:00
|
|
|
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:
|
2018-08-06 02:09:48 +10:00
|
|
|
playlist_monitor = playlists.kodi_playlist_monitor()
|
2021-01-12 02:21:37 +11:00
|
|
|
self.start_additional_metadata(refresh=False)
|
2018-11-06 01:23:51 +11:00
|
|
|
self.start_image_cache_thread()
|
2018-04-18 04:18:25 +10:00
|
|
|
else:
|
|
|
|
LOG.info('Startup sync has not yet been successful')
|
2019-11-29 03:49:48 +11:00
|
|
|
self.sleep(1)
|
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
|
2019-01-31 06:36:52 +11:00
|
|
|
else:
|
2018-11-19 00:59:17 +11:00
|
|
|
# Full scan was requested from somewhere else
|
|
|
|
if app.SYNC.run_lib_scan is not None:
|
2017-08-22 02:53:38 +10:00
|
|
|
self.triage_lib_scans()
|
2018-10-24 16:08:32 +11:00
|
|
|
# Reset the flag
|
2018-11-19 00:59:17 +11:00
|
|
|
app.SYNC.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-11-19 00:59:17 +11:00
|
|
|
now = timing.unix_timestamp()
|
2018-12-22 02:53:53 +11:00
|
|
|
if (now - self.last_full_sync > app.SYNC.full_sync_intervall and
|
2019-01-31 06:36:52 +11:00
|
|
|
not app.APP.is_playing_video):
|
2018-04-16 02:33:20 +10:00
|
|
|
LOG.info('Doing scheduled full library scan')
|
2018-11-06 04:13:57 +11:00
|
|
|
self.start_library_sync()
|
2021-03-18 07:13:11 +11:00
|
|
|
else:
|
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)
|
2020-12-19 18:11:45 +11:00
|
|
|
except backgroundthread.queue.Empty:
|
2018-04-18 04:18:25 +10:00
|
|
|
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()
|
2018-04-18 04:18:25 +10:00
|
|
|
# Sleep just a bit
|
2019-11-29 03:49:48 +11:00
|
|
|
self.sleep(0.01)
|
2017-08-22 02:53:38 +10:00
|
|
|
continue
|
2019-11-29 03:49:48 +11:00
|
|
|
self.sleep(0.1)
|
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:
|
2018-04-18 04:18:25 +10:00
|
|
|
DU().stopSession()
|
2018-10-24 16:08:32 +11:00
|
|
|
except AttributeError:
|
2016-04-10 00:57:45 +10:00
|
|
|
pass
|