only do DB Connection if needed for each action

This commit is contained in:
shaun 2016-11-05 20:44:12 +11:00
parent b55b4d0b96
commit 8bd44bd408
2 changed files with 71 additions and 63 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
*.pyo *.pyo
machine_guid machine_guid
/resources/media/Thumbs.db /resources/media/Thumbs.db
.idea/

View file

@ -24,7 +24,7 @@ import videonodes
from objects import Movies, MusicVideos, TVShows, Music from objects import Movies, MusicVideos, TVShows, Music
from utils import window, settings, language as lang, should_stop from utils import window, settings, language as lang, should_stop
from ga_client import GoogleAnalytics from ga_client import GoogleAnalytics
from database import DatabaseConn from database import DatabaseConn, db_reset
from contextlib import closing from contextlib import closing
################################################################################################## ##################################################################################################
@ -714,86 +714,92 @@ class LibrarySync(threading.Thread):
processlist[process].extend(items) processlist[process].extend(items)
def incrementalSync(self): def incrementalSync(self):
with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
emby_db = embydb.Embydb_Functions(cursor_emby)
pDialog = None
update_embydb = False
if self.refresh_views: update_embydb = False
pDialog = None
# do a view update if needed
if self.refresh_views:
with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
# Received userconfig update # Received userconfig update
self.refresh_views = False self.refresh_views = False
self.maintainViews(cursor_emby, cursor_video) self.maintainViews(cursor_emby, cursor_video)
self.forceLibraryUpdate = True self.forceLibraryUpdate = True
update_embydb = True update_embydb = True
incSyncIndicator = int(settings('incSyncIndicator') or 10) # do a lib update if any items in list
totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems) totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems)
if totalUpdates > 0:
if incSyncIndicator != -1 and totalUpdates > incSyncIndicator: with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
# Only present dialog if we are going to process items with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
pDialog = self.progressDialog('Incremental sync')
log.info("incSyncIndicator=" + str(incSyncIndicator) + " totalUpdates=" + str(totalUpdates))
process = { emby_db = embydb.Embydb_Functions(cursor_emby)
'added': self.addedItems, incSyncIndicator = int(settings('incSyncIndicator') or 10)
'update': self.updateItems, if incSyncIndicator != -1 and totalUpdates > incSyncIndicator:
'userdata': self.userdataItems, # Only present dialog if we are going to process items
'remove': self.removeItems pDialog = self.progressDialog('Incremental sync')
} log.info("incSyncIndicator=" + str(incSyncIndicator) + " totalUpdates=" + str(totalUpdates))
for process_type in ['added', 'update', 'userdata', 'remove']:
if process[process_type] and window('emby_kodiScan') != "true": process = {
listItems = list(process[process_type]) 'added': self.addedItems,
del process[process_type][:] # Reset class list 'update': self.updateItems,
'userdata': self.userdataItems,
'remove': self.removeItems
}
for process_type in ['added', 'update', 'userdata', 'remove']:
items_process = itemtypes.Items(cursor_emby, cursor_video) if process[process_type] and window('emby_kodiScan') != "true":
update = False
# Prepare items according to process process_type listItems = list(process[process_type])
if process_type == "added": del process[process_type][:] # Reset class list
items = self.emby.sortby_mediatype(listItems)
elif process_type in ("userdata", "remove"): items_process = itemtypes.Items(cursor_emby, cursor_video)
items = emby_db.sortby_mediaType(listItems, unsorted=False) update = False
else: # Prepare items according to process process_type
items = emby_db.sortby_mediaType(listItems) if process_type == "added":
if items.get('Unsorted'): items = self.emby.sortby_mediatype(listItems)
sorted_items = self.emby.sortby_mediatype(items['Unsorted'])
doupdate = items_process.itemsbyId(sorted_items, "added", pDialog)
if doupdate:
embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
del items['Unsorted']
doupdate = items_process.itemsbyId(items, process_type, pDialog) elif process_type in ("userdata", "remove"):
if doupdate: items = emby_db.sortby_mediaType(listItems, unsorted=False)
embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
if update_embydb: else:
update_embydb = False items = emby_db.sortby_mediaType(listItems)
log.info("Updating emby database.") if items.get('Unsorted'):
self.saveLastSync() sorted_items = self.emby.sortby_mediatype(items['Unsorted'])
doupdate = items_process.itemsbyId(sorted_items, "added", pDialog)
if doupdate:
embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
del items['Unsorted']
if self.forceLibraryUpdate: doupdate = items_process.itemsbyId(items, process_type, pDialog)
# Force update the Kodi library if doupdate:
self.forceLibraryUpdate = False embyupdate, kodiupdate_video = doupdate
if embyupdate:
update_embydb = True
if kodiupdate_video:
self.forceLibraryUpdate = True
log.info("Updating video library.") # if stuff happened then do some stuff
window('emby_kodiScan', value="true") if update_embydb:
xbmc.executebuiltin('UpdateLibrary(video)') update_embydb = False
log.info("Updating emby database.")
self.saveLastSync()
if self.forceLibraryUpdate:
# Force update the Kodi library
self.forceLibraryUpdate = False
log.info("Updating video library.")
window('emby_kodiScan', value="true")
xbmc.executebuiltin('UpdateLibrary(video)')
if pDialog: if pDialog:
pDialog.close() pDialog.close()
@ -904,7 +910,7 @@ class LibrarySync(threading.Thread):
log.warn("Database version is out of date! USER IGNORED!") log.warn("Database version is out of date! USER IGNORED!")
dialog.ok(lang(29999), lang(33023)) dialog.ok(lang(29999), lang(33023))
else: else:
database.db_reset() db_reset()
break break