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,10 +159,13 @@ 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
@ -157,7 +173,7 @@ class ThreadedShowSyncInfo(threading.Thread):
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,14 +254,50 @@ 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
not reflected
"""
# Get last sync time
lastSync = utils.window('LastIncrementalSync')
if not lastSync: if not lastSync:
lastSync = "2010-01-01T00:00:00Z" lastSync = "2016-01-01T00:00:00Z"
self.logMsg("Last sync run: %s" % lastSync, 1) 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
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:
self.allKodiElementsId = dict(emby_db.getChecksum('Movie'))
except ValueError:
pass
views = doUtils.downloadUrl("{server}/library/sections")
try:
views = views['_children']
except TypeError:
self.logMsg("Could not process fastSync view json, aborting", 0)
return False
# Run through views and get latest changed elements using time diff
for view in views:
pass
# Figure out whether an item needs updating
# Process updates
url = "{server}/library/recentlyAdded"
result = self.doUtils.downloadUrl(url, parameters=params) result = self.doUtils.downloadUrl(url, parameters=params)
try: try:
@ -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()