Small fixes

This commit is contained in:
tomkat83 2016-01-27 15:14:30 +01:00
parent 42bd570187
commit e5311981b4
2 changed files with 121 additions and 42 deletions

View file

@ -60,6 +60,29 @@ try:
except ImportError: except ImportError:
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
def XbmcItemtypes():
return ['photo', 'video', 'audio']
def PlexItemtypes():
return ['photo', 'video', 'audio']
def PlexLibraryItemtypes():
return ['movie', 'show']
# later add: 'artist', 'photo'
def XbmcPhoto():
return "photo"
def XbmcVideo():
return "video"
def XbmcAudio():
return "audio"
def PlexPhoto():
return "photo"
def PlexVideo():
return "video"
def PlexAudio():
return "music"
@utils.logging @utils.logging
class PlexAPI(): class PlexAPI():

View file

@ -48,11 +48,16 @@ class ThreadedGetMetadata(threading.Thread):
def run(self): def run(self):
plx = PlexAPI.PlexAPI() plx = PlexAPI.PlexAPI()
# cache local variables because it's faster
queue = self.queue
out_queue = self.out_queue
lock = self.lock
threadStopped = self.threadStopped
global getMetadataCount global getMetadataCount
while self.threadStopped() is False: while threadStopped() is False:
# grabs Plex item from queue # grabs Plex item from queue
try: try:
updateItem = self.queue.get(block=False) updateItem = queue.get(block=False)
# Empty queue # Empty queue
except Queue.Empty: except Queue.Empty:
continue continue
@ -65,14 +70,16 @@ class ThreadedGetMetadata(threading.Thread):
if plexXML: if plexXML:
updateItem['XML'] = plexXML updateItem['XML'] = plexXML
# place item into out queue # place item into out queue
self.out_queue.put(updateItem) out_queue.put(updateItem)
del plexXML
del updateItem
# If we don't have a valid XML, don't put that into the queue # If we don't have a valid XML, don't put that into the queue
# but skip this item for now # but skip this item for now
# Keep track of where we are at # Keep track of where we are at
with self.lock: with lock:
getMetadataCount += 1 getMetadataCount += 1
# signals to queue job is done # signals to queue job is done
self.queue.task_done() queue.task_done()
@utils.ThreadMethodsStopsync @utils.ThreadMethodsStopsync
@ -98,13 +105,17 @@ class ThreadedProcessMetadata(threading.Thread):
def run(self): def run(self):
# Constructs the method name, e.g. itemtypes.Movies # Constructs the method name, e.g. itemtypes.Movies
itemFkt = getattr(itemtypes, self.itemType) itemFkt = getattr(itemtypes, self.itemType)
# cache local variables because it's faster
queue = self.queue
lock = self.lock
threadStopped = self.threadStopped
global processMetadataCount global processMetadataCount
global processingViewName global processingViewName
with itemFkt() as item: with itemFkt() as item:
while self.threadStopped() is False: while threadStopped() is False:
# grabs item from queue # grabs item from queue
try: try:
updateItem = self.queue.get(block=False) updateItem = queue.get(block=False)
# Empty queue # Empty queue
except Queue.Empty: except Queue.Empty:
continue continue
@ -115,13 +126,15 @@ class ThreadedProcessMetadata(threading.Thread):
viewId = updateItem['viewId'] viewId = updateItem['viewId']
title = updateItem['title'] title = updateItem['title']
itemSubFkt = getattr(item, method) itemSubFkt = getattr(item, method)
with self.lock: with lock:
itemSubFkt(plexitem, itemSubFkt(plexitem,
viewtag=viewName, viewtag=viewName,
viewid=viewId) viewid=viewId)
# Keep track of where we are at # Keep track of where we are at
processMetadataCount += 1 processMetadataCount += 1
processingViewName = title processingViewName = title
del plexitem
del updateItem
# signals to queue job is done # signals to queue job is done
self.queue.task_done() self.queue.task_done()
@ -146,18 +159,21 @@ class ThreadedShowSyncInfo(threading.Thread):
threading.Thread.__init__(self) threading.Thread.__init__(self)
def run(self): def run(self):
# cache local variables because it's faster
total = self.total total = self.total
dialog = self.dialog
threadStopped = self.threadStopped
downloadLock = self.locks[0] downloadLock = self.locks[0]
processLock = self.locks[1] processLock = self.locks[1]
self.dialog.create("%s: Sync %s: %s items" dialog.create("%s: Sync %s: %s items"
% (self.addonName, self.itemType, str(total)), % (self.addonName, self.itemType, str(total)),
"Starting") "Starting")
global getMetadataCount global getMetadataCount
global processMetadataCount global processMetadataCount
global processingViewName global processingViewName
total = 2 * total total = 2 * total
totalProgress = 0 totalProgress = 0
while self.threadStopped() is False: while threadStopped() is False:
with downloadLock: with downloadLock:
getMetadataProgress = getMetadataCount getMetadataProgress = getMetadataCount
with processLock: with processLock:
@ -168,13 +184,13 @@ class ThreadedShowSyncInfo(threading.Thread):
percentage = int(float(totalProgress) / float(total)*100.0) percentage = int(float(totalProgress) / float(total)*100.0)
except ZeroDivisionError: except ZeroDivisionError:
percentage = 0 percentage = 0
self.dialog.update(percentage, dialog.update(percentage,
message="Downloaded: %s, Processed: %s: %s" message="Downloaded: %s, Processed: %s: %s"
% (getMetadataProgress, % (getMetadataProgress,
processMetadataProgress, viewName)) processMetadataProgress, viewName))
# Sleep for x milliseconds # Sleep for x milliseconds
xbmc.sleep(500) xbmc.sleep(500)
self.dialog.close() dialog.close()
@utils.logging @utils.logging
@ -238,35 +254,71 @@ class LibrarySync(threading.Thread):
return completed return completed
def fastSync(self): def fastSync(self):
"""
Fast incremential lib sync
lastSync = utils.settings('LastIncrementalSync') Using /library/recentlyAdded is NOT working as changes to lib items are
if not lastSync: not reflected
lastSync = "2010-01-01T00:00:00Z" """
self.logMsg("Last sync run: %s" % lastSync, 1) # Get last sync time
lastSync = utils.window('LastIncrementalSync')
if not lastSync:
lastSync = "2016-01-01T00:00:00Z"
self.logMsg("Last sync run: %s" % lastSync, 1)
url = "{server}/emby/Emby.Kodi.SyncQueue/{UserId}/GetItems?format=json" # Convert time to unix main time or whatever it is called
params = {'LastUpdateDT': lastSync} # Get all PMS views
result = self.doUtils.downloadUrl(url, parameters=params) self.maintainViews()
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
views = []
for itemtype in PlexAPI.PlexLibraryItemtypes():
views.append(emby_db.getView_byType(itemtype))
# Also get checksums of Plex items already saved in Kodi
self.allKodiElementsId = {}
for itemtype in PlexAPI.dk():
try: try:
processlist = { self.allKodiElementsId = dict(emby_db.getChecksum('Movie'))
except ValueError:
pass
'added': result['ItemsAdded'], views = doUtils.downloadUrl("{server}/library/sections")
'update': result['ItemsUpdated'], try:
'userdata': result['UserDataChanged'], views = views['_children']
'remove': result['ItemsRemoved'] except TypeError:
} self.logMsg("Could not process fastSync view json, aborting", 0)
return False
except (KeyError, TypeError): # Run through views and get latest changed elements using time diff
self.logMsg("Failed to retrieve latest updates using fast sync.", 1) for view in views:
return False pass
else: # Figure out whether an item needs updating
self.logMsg("Fast sync changes: %s" % result, 1) # Process updates
for action in processlist: url = "{server}/library/recentlyAdded"
self.triage_items(action, processlist[action]) result = self.doUtils.downloadUrl(url, parameters=params)
return True try:
processlist = {
'added': result['ItemsAdded'],
'update': result['ItemsUpdated'],
'userdata': result['UserDataChanged'],
'remove': result['ItemsRemoved']
}
except (KeyError, TypeError):
self.logMsg("Failed to retrieve latest updates using fast sync.", 1)
return False
else:
self.logMsg("Fast sync changes: %s" % result, 1)
for action in processlist:
self.triage_items(action, processlist[action])
return True
def saveLastSync(self): def saveLastSync(self):
# Save last sync time # Save last sync time
@ -276,7 +328,7 @@ class LibrarySync(threading.Thread):
lastSync = time_now.strftime('%Y-%m-%dT%H:%M:%SZ') lastSync = time_now.strftime('%Y-%m-%dT%H:%M:%SZ')
self.logMsg("New sync time: client time -%s min: %s" self.logMsg("New sync time: client time -%s min: %s"
% (overlap, lastSync), 1) % (overlap, lastSync), 1)
utils.settings('LastIncrementalSync', value=lastSync) utils.window('LastIncrementalSync', value=lastSync)
def initializeDBs(self): def initializeDBs(self):
""" """
@ -662,6 +714,10 @@ class LibrarySync(threading.Thread):
thread.join(5.0) thread.join(5.0)
if thread.isAlive(): if thread.isAlive():
self.logMsg("Could not terminate thread", -1) self.logMsg("Could not terminate thread", -1)
try:
del threads
except:
self.logMsg("Could not delete threads", -1)
# Make sure dialog window is closed # Make sure dialog window is closed
if dialog: if dialog:
dialog.close() dialog.close()