Centralized logging

This commit is contained in:
angelblue05 2016-06-16 00:43:36 -05:00
parent f5404fa1c1
commit 7a0f69e014
9 changed files with 542 additions and 516 deletions

View file

@ -12,9 +12,9 @@ import xbmc
import xbmcgui
import xbmcvfs
import utils
import clientinfo
import image_cache_thread
from utils import Logging, window, settings, kodiSQL
#################################################################################################
@ -29,24 +29,25 @@ class Artwork():
imageCacheThreads = []
imageCacheLimitThreads = 0
def __init__(self):
global log
log = Logging(self.__class__.__name__).log
self.clientinfo = clientinfo.ClientInfo()
self.addonName = self.clientinfo.getAddonName()
self.enableTextureCache = utils.settings('enableTextureCache') == "true"
self.imageCacheLimitThreads = int(utils.settings("imageCacheLimit"))
self.enableTextureCache = settings('enableTextureCache') == "true"
self.imageCacheLimitThreads = int(settings('imageCacheLimit'))
self.imageCacheLimitThreads = int(self.imageCacheLimitThreads * 5)
utils.logMsg("Using Image Cache Thread Count: " + str(self.imageCacheLimitThreads), 1)
log("Using Image Cache Thread Count: %s" % self.imageCacheLimitThreads, 1)
if not self.xbmc_port and self.enableTextureCache:
self.setKodiWebServerDetails()
self.userId = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userId)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
self.userId = window('emby_currUser')
self.server = window('emby_server%s' % self.userId)
def double_urlencode(self, text):
@ -56,8 +57,8 @@ class Artwork():
return text
def single_urlencode(self, text):
text = urllib.urlencode({'blahblahblah':text.encode("utf-8")}) #urlencode needs a utf- string
# urlencode needs a utf- string
text = urllib.urlencode({'blahblahblah':text.encode("utf-8")})
text = text[13:]
return text.decode("utf-8") #return the result again as unicode
@ -167,102 +168,116 @@ class Artwork():
def FullTextureCacheSync(self):
# This method will sync all Kodi artwork to textures13.db
# and cache them locally. This takes diskspace!
dialog = xbmcgui.Dialog()
if not xbmcgui.Dialog().yesno("Image Texture Cache", "Running the image cache process can take some time.", "Are you sure you want continue?"):
if not dialog.yesno(
heading="Image Texture Cache",
line1=(
"Running the image cache process can take some time. "
"Are you sure you want continue?")):
return
self.logMsg("Doing Image Cache Sync", 1)
log("Doing Image Cache Sync", 1)
dialog = xbmcgui.DialogProgress()
dialog.create("Emby for Kodi", "Image Cache Sync")
pdialog = xbmcgui.DialogProgress()
pdialog.create("Emby for Kodi", "Image Cache Sync")
# ask to rest all existing or not
if xbmcgui.Dialog().yesno("Image Texture Cache", "Reset all existing cache data first?", ""):
self.logMsg("Resetting all cache data first", 1)
if dialog.yesno("Image Texture Cache", "Reset all existing cache data first?"):
log("Resetting all cache data first.", 1)
# Remove all existing textures first
path = xbmc.translatePath("special://thumbnails/").decode('utf-8')
path = xbmc.translatePath('special://thumbnails/').decode('utf-8')
if xbmcvfs.exists(path):
allDirs, allFiles = xbmcvfs.listdir(path)
for dir in allDirs:
allDirs, allFiles = xbmcvfs.listdir(path+dir)
for file in allFiles:
if os.path.supports_unicode_filenames:
xbmcvfs.delete(os.path.join(path+dir.decode('utf-8'),file.decode('utf-8')))
path = os.path.join(path+dir.decode('utf-8'),file.decode('utf-8'))
xbmcvfs.delete(path)
else:
xbmcvfs.delete(os.path.join(path.encode('utf-8')+dir,file))
# remove all existing data from texture DB
textureconnection = utils.kodiSQL('texture')
texturecursor = textureconnection.cursor()
texturecursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = texturecursor.fetchall()
connection = kodiSQL('texture')
cursor = connection.cursor()
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:
tableName = row[0]
if(tableName != "version"):
texturecursor.execute("DELETE FROM " + tableName)
textureconnection.commit()
texturecursor.close()
if tableName != "version":
cursor.execute("DELETE FROM " + tableName)
connection.commit()
cursor.close()
# Cache all entries in video DB
connection = utils.kodiSQL('video')
connection = kodiSQL('video')
cursor = connection.cursor()
cursor.execute("SELECT url FROM art WHERE media_type != 'actor'") # dont include actors
result = cursor.fetchall()
total = len(result)
count = 1
percentage = 0
self.logMsg("Image cache sync about to process " + str(total) + " images", 1)
for url in result:
if dialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
textMessage = str(count) + " of " + str(total) + " (" + str(len(self.imageCacheThreads)) + ")"
dialog.update(percentage, "Updating Image Cache: " + textMessage)
self.CacheTexture(url[0])
count += 1
log("Image cache sync about to process %s images" % total, 1)
cursor.close()
count = 0
for url in result:
if pdialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
message = "%s of %s (%s)" % (count, total, self.imageCacheThreads)
pdialog.update(percentage, "Updating Image Cache: %s" % message)
self.cacheTexture(url[0])
count += 1
# Cache all entries in music DB
connection = utils.kodiSQL('music')
connection = kodiSQL('music')
cursor = connection.cursor()
cursor.execute("SELECT url FROM art")
result = cursor.fetchall()
total = len(result)
count = 1
percentage = 0
self.logMsg("Image cache sync about to process " + str(total) + " images", 1)
for url in result:
if dialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
textMessage = str(count) + " of " + str(total)
dialog.update(percentage, "Updating Image Cache: " + textMessage)
self.CacheTexture(url[0])
count += 1
log("Image cache sync about to process %s images" % total, 1)
cursor.close()
dialog.update(100, "Waiting for all threads to exit: " + str(len(self.imageCacheThreads)))
self.logMsg("Waiting for all threads to exit", 1)
while len(self.imageCacheThreads) > 0:
count = 0
for url in result:
if pdialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
message = "%s of %s" % (count, total)
pdialog.update(percentage, "Updating Image Cache: %s" % message)
self.cacheTexture(url[0])
count += 1
pdialog.update(100, "Waiting for all threads to exit: %s" % len(self.imageCacheThreads))
log("Waiting for all threads to exit", 1)
while len(self.imageCacheThreads):
for thread in self.imageCacheThreads:
if thread.isFinished:
self.imageCacheThreads.remove(thread)
dialog.update(100, "Waiting for all threads to exit: " + str(len(self.imageCacheThreads)))
self.logMsg("Waiting for all threads to exit: " + str(len(self.imageCacheThreads)), 1)
pdialog.update(100, "Waiting for all threads to exit: %s" % len(self.imageCacheThreads))
log("Waiting for all threads to exit: %s" % len(self.imageCacheThreads), 1)
xbmc.sleep(500)
dialog.close()
pdialog.close()
def addWorkerImageCacheThread(self, urlToAdd):
def addWorkerImageCacheThread(self, url):
while(True):
while True:
# removed finished
for thread in self.imageCacheThreads:
if thread.isFinished:
self.imageCacheThreads.remove(thread)
# add a new thread or wait and retry if we hit our limit
if(len(self.imageCacheThreads) < self.imageCacheLimitThreads):
if len(self.imageCacheThreads) < self.imageCacheLimitThreads:
newThread = image_cache_thread.image_cache_thread()
newThread.setUrl(self.double_urlencode(urlToAdd))
newThread.setHost(self.xbmc_host, self.xbmc_port)
@ -271,23 +286,21 @@ class Artwork():
self.imageCacheThreads.append(newThread)
return
else:
self.logMsg("Waiting for empty queue spot: " + str(len(self.imageCacheThreads)), 2)
log("Waiting for empty queue spot: %s" % len(self.imageCacheThreads), 2)
xbmc.sleep(50)
def CacheTexture(self, url):
def cacheTexture(self, url):
# Cache a single image url to the texture cache
if url and self.enableTextureCache:
self.logMsg("Processing: %s" % url, 2)
log("Processing: %s" % url, 2)
if(self.imageCacheLimitThreads == 0 or self.imageCacheLimitThreads == None):
#Add image to texture cache by simply calling it at the http endpoint
if not self.imageCacheLimitThreads:
# Add image to texture cache by simply calling it at the http endpoint
url = self.double_urlencode(url)
try: # Extreme short timeouts so we will have a exception.
response = requests.head(
url=(
"http://%s:%s/image/image://%s"
url=("http://%s:%s/image/image://%s"
% (self.xbmc_host, self.xbmc_port, url)),
auth=(self.xbmc_username, self.xbmc_password),
timeout=(0.01, 0.01))
@ -298,7 +311,7 @@ class Artwork():
self.addWorkerImageCacheThread(url)
def addArtwork(self, artwork, kodiId, mediaType, cursor):
def addArtwork(self, artwork, kodi_id, media_type, cursor):
# Kodi conversion table
kodiart = {
@ -329,7 +342,7 @@ class Artwork():
"AND media_type = ?",
"AND type LIKE ?"
))
cursor.execute(query, (kodiId, mediaType, "fanart%",))
cursor.execute(query, (kodi_id, media_type, "fanart%",))
rows = cursor.fetchall()
if len(rows) > backdropsNumber:
@ -341,16 +354,16 @@ class Artwork():
"AND media_type = ?",
"AND type LIKE ?"
))
cursor.execute(query, (kodiId, mediaType, "fanart_",))
cursor.execute(query, (kodi_id, media_type, "fanart_",))
# Process backdrops and extra fanart
index = ""
for backdrop in backdrops:
self.addOrUpdateArt(
imageUrl=backdrop,
kodiId=kodiId,
mediaType=mediaType,
imageType="%s%s" % ("fanart", index),
image_url=backdrop,
kodi_id=kodi_id,
media_type=media_type,
image_type="%s%s" % ("fanart", index),
cursor=cursor)
if backdropsNumber > 1:
@ -363,24 +376,24 @@ class Artwork():
# Primary art is processed as thumb and poster for Kodi.
for artType in kodiart[art]:
self.addOrUpdateArt(
imageUrl=artwork[art],
kodiId=kodiId,
mediaType=mediaType,
imageType=artType,
image_url=artwork[art],
kodi_id=kodi_id,
media_type=media_type,
image_type=artType,
cursor=cursor)
elif kodiart.get(art):
# Process the rest artwork type that Kodi can use
self.addOrUpdateArt(
imageUrl=artwork[art],
kodiId=kodiId,
mediaType=mediaType,
imageType=kodiart[art],
image_url=artwork[art],
kodi_id=kodi_id,
media_type=media_type,
image_type=kodiart[art],
cursor=cursor)
def addOrUpdateArt(self, imageUrl, kodiId, mediaType, imageType, cursor):
def addOrUpdateArt(self, image_url, kodi_id, media_type, image_type, cursor):
# Possible that the imageurl is an empty string
if imageUrl:
if image_url:
cacheimage = False
query = ' '.join((
@ -391,13 +404,13 @@ class Artwork():
"AND media_type = ?",
"AND type = ?"
))
cursor.execute(query, (kodiId, mediaType, imageType,))
cursor.execute(query, (kodi_id, media_type, image_type,))
try: # Update the artwork
url = cursor.fetchone()[0]
except TypeError: # Add the artwork
cacheimage = True
self.logMsg("Adding Art Link for kodiId: %s (%s)" % (kodiId, imageUrl), 2)
log("Adding Art Link for kodiId: %s (%s)" % (kodi_id, image_url), 2)
query = (
'''
@ -406,21 +419,20 @@ class Artwork():
VALUES (?, ?, ?, ?)
'''
)
cursor.execute(query, (kodiId, mediaType, imageType, imageUrl))
cursor.execute(query, (kodi_id, media_type, image_type, image_url))
else: # Only cache artwork if it changed
if url != imageUrl:
if url != image_url:
cacheimage = True
# Only for the main backdrop, poster
if (utils.window('emby_initialScan') != "true" and
if (window('emby_initialScan') != "true" and
imageType in ("fanart", "poster")):
# Delete current entry before updating with the new one
self.deleteCachedArtwork(url)
self.logMsg(
"Updating Art url for %s kodiId: %s (%s) -> (%s)"
% (imageType, kodiId, url, imageUrl), 1)
log("Updating Art url for %s kodiId: %s (%s) -> (%s)"
% (image_type, kodi_id, url, image_url), 1)
query = ' '.join((
@ -430,13 +442,13 @@ class Artwork():
"AND media_type = ?",
"AND type = ?"
))
cursor.execute(query, (imageUrl, kodiId, mediaType, imageType))
cursor.execute(query, (image_url, kodi_id, media_type, image_type))
# Cache fanart and poster in Kodi texture cache
if cacheimage and imageType in ("fanart", "poster"):
self.CacheTexture(imageUrl)
if cacheimage and image_type in ("fanart", "poster"):
self.cacheTexture(image_url)
def deleteArtwork(self, kodiid, mediatype, cursor):
def deleteArtwork(self, kodi_id, media_type, cursor):
query = ' '.join((
@ -445,7 +457,7 @@ class Artwork():
"WHERE media_id = ?",
"AND media_type = ?"
))
cursor.execute(query, (kodiid, mediatype,))
cursor.execute(query, (kodi_id, media_type,))
rows = cursor.fetchall()
for row in rows:
@ -456,7 +468,7 @@ class Artwork():
def deleteCachedArtwork(self, url):
# Only necessary to remove and apply a new backdrop or poster
connection = utils.kodiSQL('texture')
connection = kodiSQL('texture')
cursor = connection.cursor()
try:
@ -464,21 +476,21 @@ class Artwork():
cachedurl = cursor.fetchone()[0]
except TypeError:
self.logMsg("Could not find cached url.", 1)
log("Could not find cached url.", 1)
except OperationalError:
self.logMsg("Database is locked. Skip deletion process.", 1)
log("Database is locked. Skip deletion process.", 1)
else: # Delete thumbnail as well as the entry
thumbnails = xbmc.translatePath("special://thumbnails/%s" % cachedurl).decode('utf-8')
self.logMsg("Deleting cached thumbnail: %s" % thumbnails, 1)
log("Deleting cached thumbnail: %s" % thumbnails, 1)
xbmcvfs.delete(thumbnails)
try:
cursor.execute("DELETE FROM texture WHERE url = ?", (url,))
connection.commit()
except OperationalError:
self.logMsg("Issue deleting url from cache. Skipping.", 2)
log("Issue deleting url from cache. Skipping.", 2)
finally:
cursor.close()
@ -501,26 +513,26 @@ class Artwork():
return people
def getUserArtwork(self, itemid, itemtype):
def getUserArtwork(self, item_id, item_type):
# Load user information set by UserClient
image = ("%s/emby/Users/%s/Images/%s?Format=original"
% (self.server, itemid, itemtype))
% (self.server, item_id, item_type))
return image
def getAllArtwork(self, item, parentInfo=False):
def getAllArtwork(self, item, parent_artwork=False):
itemid = item['Id']
artworks = item['ImageTags']
backdrops = item.get('BackdropImageTags',[])
backdrops = item.get('BackdropImageTags', [])
maxHeight = 10000
maxWidth = 10000
customquery = ""
if utils.settings('compressArt') == "true":
if settings('compressArt') == "true":
customquery = "&Quality=90"
if utils.settings('enableCoverArt') == "false":
if settings('enableCoverArt') == "false":
customquery += "&EnableImageEnhancers=false"
allartworks = {
@ -554,7 +566,7 @@ class Artwork():
allartworks[art] = artwork
# Process parent items if the main item is missing artwork
if parentInfo:
if parent_artwork:
# Process parent backdrops
if not allartworks['Backdrop']:
@ -601,4 +613,4 @@ class Artwork():
% (self.server, parentId, maxWidth, maxHeight, parentTag, customquery))
allartworks['Primary'] = artwork
return allartworks
return allartworks