Fix library sync

Adjust lock, re-add screensaver deactivated during sync, prep compare sync, stop library updates from being processed before startup sync is completed
This commit is contained in:
angelblue05 2018-10-03 00:12:30 -05:00
parent b8ea16ed46
commit 8d838d257e
4 changed files with 246 additions and 153 deletions

View file

@ -276,14 +276,14 @@ class Service(xbmc.Monitor):
self.connect.setup_manual_server() self.connect.setup_manual_server()
elif method == 'UserDataChanged' and self.library_thread: elif method == 'UserDataChanged' and self.library_thread:
if data.get('ServerId'): if data.get('ServerId') or not window('emby_startup.bool'):
return return
LOG.info("[ UserDataChanged ] %s", data) LOG.info("[ UserDataChanged ] %s", data)
self.library_thread.userdata(data['UserDataList']) self.library_thread.userdata(data['UserDataList'])
elif method == 'LibraryChanged' and self.library_thread: elif method == 'LibraryChanged' and self.library_thread:
if data.get('ServerId'): if data.get('ServerId') or not window('emby_startup.bool'):
return return
LOG.info("[ LibraryChanged ] %s", data) LOG.info("[ LibraryChanged ] %s", data)
@ -415,11 +415,9 @@ class Service(xbmc.Monitor):
properties = [ # TODO: review properties = [ # TODO: review
"emby_state", "emby_serverStatus", "emby_state", "emby_serverStatus",
"emby_syncRunning", "emby_dbCheck", "emby_syncRunning", "emby_currUser",
"emby_currUser", "emby_dbScan",
"emby_initialScan",
"emby_play", "emby_online", "emby.connected", "emby.resume", "emby_play", "emby_online", "emby.connected", "emby.resume", "emby_startup",
"emby.external", "emby.external_check", "emby_deviceId", "emby_db_check", "emby_pathverified" "emby.external", "emby.external_check", "emby_deviceId", "emby_db_check", "emby_pathverified"
] ]
for prop in properties: for prop in properties:

View file

@ -15,6 +15,7 @@ import helper.xmls as xmls
from database import Database, get_sync, save_sync, emby_db from database import Database, get_sync, save_sync, emby_db
from objects import Movies, TVShows, MusicVideos, Music from objects import Movies, TVShows, MusicVideos, Music
from helper import _, settings, progress, dialog, LibraryException from helper import _, settings, progress, dialog, LibraryException
from helper.utils import get_screensaver, set_screensaver
from emby import Emby from emby import Emby
################################################################################################## ##################################################################################################
@ -102,7 +103,7 @@ class FullSync(object):
def select_libraries(self, libraries): def select_libraries(self, libraries):
''' Select all or whitelist libraries. Provides a new list. ''' Select all or certain libraries to be whitelisted.
''' '''
if dialog("yesno", heading="{emby}", line1=_(33125), nolabel=_(33127), yeslabel=_(33126)): if dialog("yesno", heading="{emby}", line1=_(33125), nolabel=_(33127), yeslabel=_(33126)):
LOG.info("Selected sync later.") LOG.info("Selected sync later.")
@ -145,20 +146,36 @@ class FullSync(object):
save_sync(self.sync) save_sync(self.sync)
start_time = datetime.datetime.now() start_time = datetime.datetime.now()
for library in list(self.sync['Libraries']): if not settings('dbSyncScreensaver.bool'):
self.process_library(library) xbmc.executebuiltin('InhibitIdleShutdown(true)')
screensaver = get_screensaver()
set_screensaver(value="")
if not library.startswith('Boxsets:') and library not in self.sync['Whitelist']: try:
self.sync['Whitelist'].append(library) for library in list(self.sync['Libraries']):
self.sync['Libraries'].pop(self.sync['Libraries'].index(library)) self.process_library(library)
self.sync['RestorePoint'] = {}
if not library.startswith('Boxsets:') and library not in self.sync['Whitelist']:
self.sync['Whitelist'].append(library)
self.sync['Libraries'].pop(self.sync['Libraries'].index(library))
self.sync['RestorePoint'] = {}
except Exception as error:
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(false)')
set_screensaver(value=screensaver)
raise
elapsed = datetime.datetime.now() - start_time elapsed = datetime.datetime.now() - start_time
settings('SyncInstallRunDone.bool', True) settings('SyncInstallRunDone.bool', True)
self.library.save_last_sync() self.library.save_last_sync()
save_sync(self.sync) save_sync(self.sync)
xbmc.executebuiltin('UpdateLibrary(video)') xbmc.executebuiltin('UpdateLibrary(video)')
dialog("notification", heading="{emby}", message="%s %s" % (_(33025), str(elapsed).split('.')[0]), dialog("notification", heading="{emby}", message="%s %s" % (_(33025), str(elapsed).split('.')[0]),
icon="{emby}", sound=False) icon="{emby}", sound=False)
@ -219,133 +236,183 @@ class FullSync(object):
''' Process movies from a single library. ''' Process movies from a single library.
''' '''
with Database() as videodb: with self.library.database_lock:
with Database('emby') as embydb: with Database() as videodb:
obj = Movies(self.server, embydb, videodb, self.direct_path) with Database('emby') as embydb:
for items in server.get_items(library['Id'], "Movie", False, self.sync['RestorePoint'].get('params')): obj = Movies(self.server, embydb, videodb, self.direct_path)
self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex']
for index, movie in enumerate(items['Items']): for items in server.get_items(library['Id'], "Movie", False, self.sync['RestorePoint'].get('params')):
self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex']
dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100), for index, movie in enumerate(items['Items']):
heading="%s: %s" % (_('addon_name'), library['Name']),
message=movie['Name']) dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100),
obj.movie(movie, library=library) heading="%s: %s" % (_('addon_name'), library['Name']),
message=movie['Name'])
obj.movie(movie, library=library)
#self.movies_compare(library, obj, embydb)
def movies_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress() @progress()
def tvshows(self, library, dialog): def tvshows(self, library, dialog):
''' Process tvshows and episodes from a single library. ''' Process tvshows and episodes from a single library.
''' '''
with Database() as videodb: with self.library.database_lock:
with Database('emby') as embydb: with Database() as videodb:
obj = TVShows(self.server, embydb, videodb, self.direct_path) with Database('emby') as embydb:
obj = TVShows(self.server, embydb, videodb, self.direct_path)
for items in server.get_items(library['Id'], "Series", False, self.sync['RestorePoint'].get('params')): for items in server.get_items(library['Id'], "Series", False, self.sync['RestorePoint'].get('params')):
self.sync['RestorePoint'] = items['RestorePoint'] self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex'] start_index = items['RestorePoint']['params']['StartIndex']
for index, show in enumerate(items['Items']): for index, show in enumerate(items['Items']):
percent = int((float(start_index + index) / float(items['TotalRecordCount']))*100) percent = int((float(start_index + index) / float(items['TotalRecordCount']))*100)
message = show['Name'] message = show['Name']
dialog.update(percent, heading="%s: %s" % (_('addon_name'), library['Name']), message=message) dialog.update(percent, heading="%s: %s" % (_('addon_name'), library['Name']), message=message)
if obj.tvshow(show, library=library) != False: if obj.tvshow(show, library=library) != False:
for episodes in server.get_episode_by_show(show['Id']): for episodes in server.get_episode_by_show(show['Id']):
for episode in episodes['Items']: for episode in episodes['Items']:
dialog.update(percent, message="%s/%s" % (message, episode['Name'][:10])) dialog.update(percent, message="%s/%s" % (message, episode['Name'][:10]))
obj.episode(episode) obj.episode(episode)
def tvshows_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress() @progress()
def musicvideos(self, library, dialog): def musicvideos(self, library, dialog):
''' Process musicvideos from a single library. ''' Process musicvideos from a single library.
''' '''
with Database() as videodb: with self.library.database_lock:
with Database('emby') as embydb: with Database() as videodb:
obj = MusicVideos(self.server, embydb, videodb, self.direct_path) with Database('emby') as embydb:
obj = MusicVideos(self.server, embydb, videodb, self.direct_path)
for items in server.get_items(library['Id'], "MusicVideo", False, self.sync['RestorePoint'].get('params')): for items in server.get_items(library['Id'], "MusicVideo", False, self.sync['RestorePoint'].get('params')):
self.sync['RestorePoint'] = items['RestorePoint'] self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex'] start_index = items['RestorePoint']['params']['StartIndex']
for index, mvideo in enumerate(items['Items']): for index, mvideo in enumerate(items['Items']):
dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100), dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100),
heading="%s: %s" % (_('addon_name'), library['Name']), heading="%s: %s" % (_('addon_name'), library['Name']),
message=mvideo['Name']) message=mvideo['Name'])
obj.musicvideo(mvideo, library=library) obj.musicvideo(mvideo, library=library)
#self.movies_compare(library, obj, embydb)
def musicvideos_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress() @progress()
def music(self, library, dialog): def music(self, library, dialog):
''' Process artists, album, songs from a single library. ''' Process artists, album, songs from a single library.
''' '''
with Database('music') as musicdb: with self.library.music_database_lock:
with Database('emby') as embydb: with Database('music') as musicdb:
obj = Music(self.server, embydb, musicdb, self.direct_path) with Database('emby') as embydb:
obj = Music(self.server, embydb, musicdb, self.direct_path)
for items in server.get_artists(library['Id'], False, self.sync['RestorePoint'].get('params')): for items in server.get_artists(library['Id'], False, self.sync['RestorePoint'].get('params')):
self.sync['RestorePoint'] = items['RestorePoint'] self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex'] start_index = items['RestorePoint']['params']['StartIndex']
for index, artist in enumerate(items['Items']): for index, artist in enumerate(items['Items']):
percent = int((float(start_index + index) / float(items['TotalRecordCount']))*100) percent = int((float(start_index + index) / float(items['TotalRecordCount']))*100)
message = artist['Name'] message = artist['Name']
dialog.update(percent, heading="%s: %s" % (_('addon_name'), library['Name']), message=message) dialog.update(percent, heading="%s: %s" % (_('addon_name'), library['Name']), message=message)
obj.artist(artist, library=library) obj.artist(artist, library=library)
for albums in server.get_albums_by_artist(artist['Id']): for albums in server.get_albums_by_artist(artist['Id']):
for album in albums['Items']: for album in albums['Items']:
obj.album(album) obj.album(album)
for songs in server.get_items(album['Id'], "Audio"): for songs in server.get_items(album['Id'], "Audio"):
for song in songs['Items']: for song in songs['Items']:
dialog.update(percent, dialog.update(percent,
message="%s/%s/%s" % (message, album['Name'][:7], song['Name'][:7])) message="%s/%s/%s" % (message, album['Name'][:7], song['Name'][:7]))
obj.song(song) obj.song(song)
@progress(_(33018)) @progress(_(33018))
def boxsets(self, library_id=None, dialog=None): def boxsets(self, library_id=None, dialog=None):
''' Process all boxsets. ''' Process all boxsets.
''' '''
with Database() as videodb: with self.library.database_lock:
with Database('emby') as embydb: with Database() as videodb:
obj = Movies(self.server, embydb, videodb, self.direct_path) with Database('emby') as embydb:
obj = Movies(self.server, embydb, videodb, self.direct_path)
for items in server.get_items(library_id, "BoxSet", False, self.sync['RestorePoint'].get('params')): for items in server.get_items(library_id, "BoxSet", False, self.sync['RestorePoint'].get('params')):
self.sync['RestorePoint'] = items['RestorePoint'] self.sync['RestorePoint'] = items['RestorePoint']
start_index = items['RestorePoint']['params']['StartIndex'] start_index = items['RestorePoint']['params']['StartIndex']
for index, boxset in enumerate(items['Items']): for index, boxset in enumerate(items['Items']):
dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100), dialog.update(int((float(start_index + index) / float(items['TotalRecordCount']))*100),
heading="%s: %s" % (_('addon_name'), _('boxsets')), heading="%s: %s" % (_('addon_name'), _('boxsets')),
message=boxset['Name']) message=boxset['Name'])
obj.boxset(boxset) obj.boxset(boxset)
def refresh_boxsets(self): def refresh_boxsets(self):
''' Delete all exisitng boxsets and re-add. ''' Delete all exisitng boxsets and re-add.
''' '''
with Database() as videodb: with self.library.database_lock:
with Database('emby') as embydb: with Database() as videodb:
with Database('emby') as embydb:
obj = Movies(self.server, embydb, videodb, self.direct_path) obj = Movies(self.server, embydb, videodb, self.direct_path)
obj.boxsets_reset() obj.boxsets_reset()
self.boxsets(None) self.boxsets(None)

View file

@ -172,6 +172,27 @@ def should_stop():
return False return False
def get_screensaver():
''' Get the current screensaver value.
'''
result = JSONRPC('Settings.getSettingValue').execute({'setting': "screensaver.mode"})
try:
return result['result']['value']
except KeyError:
return ""
def set_screensaver(value):
''' Toggle the screensaver
'''
params = {
'setting': "screensaver.mode",
'value': value
}
result = JSONRPC('Settings.setSettingValue').execute(params)
LOG.info("---[ screensaver/%s ] %s", value, result)
class JSONRPC(object): class JSONRPC(object):
version = 1 version = 1

View file

@ -16,7 +16,7 @@ from full_sync import FullSync
from views import Views from views import Views
from downloader import GetItemWorker from downloader import GetItemWorker
from helper import _, stop, settings, window, dialog, event, progress, LibraryException from helper import _, stop, settings, window, dialog, event, progress, LibraryException
from helper.utils import split_list from helper.utils import split_list, set_screensaver, get_screensaver
from emby import Emby from emby import Emby
################################################################################################## ##################################################################################################
@ -47,6 +47,7 @@ class Library(threading.Thread):
stop_thread = False stop_thread = False
suspend = False suspend = False
pending_refresh = False pending_refresh = False
screensaver = ""
def __init__(self, monitor): def __init__(self, monitor):
@ -90,6 +91,8 @@ class Library(threading.Thread):
if not self.startup(): if not self.startup():
self.stop_client() self.stop_client()
window('emby_startup.bool', True)
while not self.stop_thread: while not self.stop_thread:
try: try:
@ -178,18 +181,28 @@ class Library(threading.Thread):
LOG.info("-->[ q:removed/%s/%s ]", queues, id(new_thread)) LOG.info("-->[ q:removed/%s/%s ]", queues, id(new_thread))
self.writer_threads['removed'].append(new_thread) self.writer_threads['removed'].append(new_thread)
self.pending_refresh = True self.pending_refresh = True
if self.pending_refresh:
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(true)')
self.screensaver = get_screensaver()
set_screensaver(value="")
if (self.pending_refresh and not self.download_threads and not self.writer_threads['updated'] and if (self.pending_refresh and not self.download_threads and not self.writer_threads['updated'] and
not self.writer_threads['userdata'] and not self.writer_threads['removed']): not self.writer_threads['userdata'] and not self.writer_threads['removed']):
self.pending_refresh = False self.pending_refresh = False
self.save_last_sync() self.save_last_sync()
if xbmc.getCondVisibility('Container.Content(musicvideos)'): # Prevent cursor from moving if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(false)')
set_screensaver(value=self.screensaver)
if xbmc.getCondVisibility('Container.Content(musicvideos)') or xbmc.getCondVisibility('Window.IsMedia'): # Prevent cursor from moving
xbmc.executebuiltin('Container.Refresh') xbmc.executebuiltin('Container.Refresh')
elif not xbmc.getCondVisibility('Window.IsMedia'): # Update widgets else: # Update widgets
xbmc.executebuiltin('UpdateLibrary(video)') xbmc.executebuiltin('UpdateLibrary(video)')
else: # Update listing
xbmc.executebuiltin('Container.Refresh')
def stop_client(self): def stop_client(self):
self.stop_thread = True self.stop_thread = True
@ -198,42 +211,33 @@ class Library(threading.Thread):
''' Run at startup. Will check for the server plugin. ''' Run at startup. Will check for the server plugin.
''' '''
fast_sync = False
Views().get_views() Views().get_views()
Views().get_nodes() Views().get_nodes()
try: try:
if not settings('kodiCompanion.bool') and settings('SyncInstallRunDone.bool'): if get_sync()['Libraries'] or not settings('SyncInstallRunDone.bool'):
return True
if settings('kodiCompanion.bool'):
for plugin in self.server['api'].get_plugins():
if plugin['Name'] in ("Emby.Kodi Sync Queue", "Kodi companion"):
fast_sync = True
break
else:
raise LibraryException('CompanionMissing')
if get_sync()['Libraries']:
FullSync(self) FullSync(self)
Views().get_nodes() Views().get_nodes()
if settings('SyncInstallRunDone.bool'): if settings('SyncInstallRunDone.bool'):
if settings('kodiCompanion.bool'):
if fast_sync and not self.fast_sync(): for plugin in self.server['api'].get_plugins():
dialog("ok", heading="{emby}", line1=_(33128)) if plugin['Name'] in ("Emby.Kodi Sync Queue", "Kodi companion"):
if not self.fast_sync():
dialog("ok", heading="{emby}", line1=_(33128))
raise Exception("Failed to retrieve latest updates") raise Exception("Failed to retrieve latest updates")
LOG.info("--<[ retrieve changes ]") LOG.info("--<[ retrieve changes ]")
else:
FullSync(self) break
Views().get_nodes() else:
raise LibraryException('CompanionMissing')
return True return True
except LibraryException as error: except LibraryException as error:
LOG.error(error.status) LOG.error(error.status)
@ -522,34 +526,35 @@ class UpdatedWorker(threading.Thread):
def run(self): def run(self):
with self.lock: with self.database as kodidb:
with self.database as kodidb: with Database('emby') as embydb:
with Database('emby') as embydb:
while True: while True:
try: try:
item = self.queue.get(timeout=3) item = self.queue.get(timeout=3)
except Queue.Empty: except Queue.Empty:
LOG.info("--<[ q:updated/%s ]", id(self)) LOG.info("--<[ q:updated/%s ]", id(self))
self.is_done = True self.is_done = True
break break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])[item['Type']] obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])[item['Type']]
try: try:
obj(item) obj(item)
self.queue.task_done()
except LibraryException as error: except LibraryException as error:
if error.status == 'StopCalled': if error.status == 'StopCalled':
break break
except Exception as error: except Exception as error:
LOG.exception(error) LOG.exception(error)
if window('emby_should_stop.bool'): self.queue.task_done()
break
if window('emby_should_stop.bool'):
break
class UserDataWorker(threading.Thread): class UserDataWorker(threading.Thread):
@ -565,34 +570,35 @@ class UserDataWorker(threading.Thread):
def run(self): def run(self):
with self.lock: with self.database as kodidb:
with self.database as kodidb: with Database('emby') as embydb:
with Database('emby') as embydb:
while True: while True:
try: try:
item = self.queue.get(timeout=3) item = self.queue.get(timeout=3)
except Queue.Empty: except Queue.Empty:
LOG.info("--<[ q:userdata/%s ]", id(self)) LOG.info("--<[ q:userdata/%s ]", id(self))
self.is_done = True self.is_done = True
break break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['UserData'] obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['UserData']
try: try:
obj(item) obj(item)
self.queue.task_done()
except LibraryException as error: except LibraryException as error:
if error.status == 'StopCalled': if error.status == 'StopCalled':
break break
except Exception as error: except Exception as error:
LOG.exception(error) LOG.exception(error)
if window('emby_should_stop.bool'): self.queue.task_done()
break
if window('emby_should_stop.bool'):
break
class SortWorker(threading.Thread): class SortWorker(threading.Thread):
@ -652,34 +658,35 @@ class RemovedWorker(threading.Thread):
def run(self): def run(self):
with self.lock: with self.database as kodidb:
with self.database as kodidb: with Database('emby') as embydb:
with Database('emby') as embydb:
while True: while True:
try: try:
item = self.queue.get(timeout=3) item = self.queue.get(timeout=2)
except Queue.Empty: except Queue.Empty:
LOG.info("--<[ q:removed/%s ]", id(self)) LOG.info("--<[ q:removed/%s ]", id(self))
self.is_done = True self.is_done = True
break break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['Remove'] obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['Remove']
try: try:
obj(item['Id']) obj(item['Id'])
self.queue.task_done()
except LibraryException as error: except LibraryException as error:
if error.status == 'StopCalled': if error.status == 'StopCalled':
break break
except Exception as error: except Exception as error:
LOG.exception(error) LOG.exception(error)
if window('emby_should_stop.bool'): self.queue.task_done()
break
if window('emby_should_stop.bool'):
break
class NotifyWorker(threading.Thread): class NotifyWorker(threading.Thread):