2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import json
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
2015-12-25 06:51:47 +11:00
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
import urllib
|
2016-02-29 14:39:00 +11:00
|
|
|
from sqlite3 import OperationalError
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import xbmc
|
2016-01-16 14:07:36 +11:00
|
|
|
import xbmcgui
|
2015-12-25 06:51:47 +11:00
|
|
|
import xbmcvfs
|
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
import image_cache_thread
|
2016-08-30 02:44:27 +10:00
|
|
|
from utils import window, settings, language as lang, kodiSQL
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-07 02:23:51 +10:00
|
|
|
# Disable annoying requests warnings
|
|
|
|
import requests.packages.urllib3
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
2016-08-30 02:44:27 +10:00
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
2016-03-24 02:57:49 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2016-08-30 02:44:27 +10:00
|
|
|
##################################################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2016-02-20 06:03:06 +11:00
|
|
|
class Artwork():
|
2015-12-25 06:51:47 +11:00
|
|
|
xbmc_host = 'localhost'
|
|
|
|
xbmc_port = None
|
|
|
|
xbmc_username = None
|
|
|
|
xbmc_password = None
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
imageCacheThreads = []
|
|
|
|
imageCacheLimitThreads = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def __init__(self):
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
self.enableTextureCache = settings('enableTextureCache') == "true"
|
|
|
|
self.imageCacheLimitThreads = int(settings('imageCacheLimit'))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.imageCacheLimitThreads = int(self.imageCacheLimitThreads * 5)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Using Image Cache Thread Count: %s" % self.imageCacheLimitThreads)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if not self.xbmc_port and self.enableTextureCache:
|
|
|
|
self.setKodiWebServerDetails()
|
|
|
|
|
2016-03-11 02:02:46 +11:00
|
|
|
self.userId = utils.window('currUserId')
|
|
|
|
self.server = utils.window('pms_server')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def double_urlencode(self, text):
|
|
|
|
text = self.single_urlencode(text)
|
|
|
|
text = self.single_urlencode(text)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return text
|
|
|
|
|
|
|
|
def single_urlencode(self, text):
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-05-07 21:15:02 +10:00
|
|
|
text = urllib.urlencode({'blahblahblah': utils.tryEncode(text)}) #urlencode needs a utf- string
|
2015-12-25 06:51:47 +11:00
|
|
|
text = text[13:]
|
|
|
|
|
2016-05-07 21:15:02 +10:00
|
|
|
return utils.tryDecode(text) #return the result again as unicode
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def setKodiWebServerDetails(self):
|
|
|
|
# Get the Kodi webserver details - used to set the texture cache
|
|
|
|
web_query = {
|
|
|
|
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
|
|
|
|
"setting": "services.webserver"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_query))
|
|
|
|
result = json.loads(result)
|
|
|
|
try:
|
|
|
|
xbmc_webserver_enabled = result['result']['value']
|
2016-04-26 21:53:19 +10:00
|
|
|
except (KeyError, TypeError):
|
2015-12-25 06:51:47 +11:00
|
|
|
xbmc_webserver_enabled = False
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if not xbmc_webserver_enabled:
|
|
|
|
# Enable the webserver, it is disabled
|
|
|
|
web_port = {
|
|
|
|
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.SetSettingValue",
|
|
|
|
"params": {
|
|
|
|
|
|
|
|
"setting": "services.webserverport",
|
|
|
|
"value": 8080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_port))
|
|
|
|
self.xbmc_port = 8080
|
|
|
|
|
|
|
|
web_user = {
|
|
|
|
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.SetSettingValue",
|
|
|
|
"params": {
|
|
|
|
|
|
|
|
"setting": "services.webserver",
|
|
|
|
"value": True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_user))
|
|
|
|
self.xbmc_username = "kodi"
|
|
|
|
|
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
# Webserver already enabled
|
|
|
|
web_port = {
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
|
|
|
|
"setting": "services.webserverport"
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2015-12-27 13:07:13 +11:00
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_port))
|
|
|
|
result = json.loads(result)
|
|
|
|
try:
|
|
|
|
self.xbmc_port = result['result']['value']
|
2016-08-30 02:44:27 +10:00
|
|
|
except (TypeError, KeyError):
|
2015-12-27 13:07:13 +11:00
|
|
|
pass
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
web_user = {
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
"setting": "services.webserverusername"
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2015-12-27 13:07:13 +11:00
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_user))
|
|
|
|
result = json.loads(result)
|
|
|
|
try:
|
|
|
|
self.xbmc_username = result['result']['value']
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
web_pass = {
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
"setting": "services.webserverpassword"
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2015-12-27 13:07:13 +11:00
|
|
|
}
|
|
|
|
result = xbmc.executeJSONRPC(json.dumps(web_pass))
|
|
|
|
result = json.loads(result)
|
|
|
|
try:
|
|
|
|
self.xbmc_password = result['result']['value']
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
def fullTextureCacheSync(self):
|
2015-12-25 06:51:47 +11:00
|
|
|
# This method will sync all Kodi artwork to textures13.db
|
|
|
|
# and cache them locally. This takes diskspace!
|
2016-03-04 00:00:48 +11:00
|
|
|
import xbmcaddon
|
|
|
|
string = xbmcaddon.Addon().getLocalizedString
|
|
|
|
|
2016-03-07 23:38:45 +11:00
|
|
|
if not xbmcgui.Dialog().yesno(
|
2016-05-07 21:15:02 +10:00
|
|
|
"Image Texture Cache", string(39250)):
|
2016-01-16 14:07:36 +11:00
|
|
|
return
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Doing Image Cache Sync")
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-19 18:58:42 +11:00
|
|
|
dialog = xbmcgui.DialogProgress()
|
2016-05-31 16:06:42 +10:00
|
|
|
dialog.create("PlexKodiConnect", "Image Cache Sync")
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
# ask to rest all existing or not
|
2016-03-07 23:38:45 +11:00
|
|
|
if xbmcgui.Dialog().yesno(
|
2016-05-07 21:15:02 +10:00
|
|
|
"Image Texture Cache", string(39251), ""):
|
2016-01-16 14:07:36 +11:00
|
|
|
self.logMsg("Resetting all cache data first", 1)
|
|
|
|
# Remove all existing textures first
|
2016-05-07 21:15:02 +10:00
|
|
|
path = utils.tryDecode(xbmc.translatePath("special://thumbnails/"))
|
2016-03-08 02:30:50 +11:00
|
|
|
if utils.IfExists(path):
|
2016-01-16 14:07:36 +11:00
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path)
|
|
|
|
for dir in allDirs:
|
|
|
|
allDirs, allFiles = xbmcvfs.listdir(path+dir)
|
|
|
|
for file in allFiles:
|
2016-01-18 22:05:05 +11:00
|
|
|
if os.path.supports_unicode_filenames:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(os.path.join(
|
|
|
|
path + utils.tryDecode(dir),
|
|
|
|
utils.tryDecode(file)))
|
2016-01-18 22:05:05 +11:00
|
|
|
else:
|
2016-05-07 21:15:02 +10:00
|
|
|
xbmcvfs.delete(os.path.join(
|
|
|
|
utils.tryEncode(path) + dir,
|
|
|
|
file))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
# remove all existing data from texture DB
|
2016-08-30 02:44:27 +10:00
|
|
|
connection = kodiSQL('texture')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
|
|
|
|
rows = cursor.fetchall()
|
2016-01-16 14:07:36 +11:00
|
|
|
for row in rows:
|
|
|
|
tableName = row[0]
|
2016-08-30 02:44:27 +10:00
|
|
|
if tableName != "version":
|
|
|
|
cursor.execute("DELETE FROM " + tableName)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
2016-01-16 14:07:36 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Cache all entries in video DB
|
2016-08-30 02:44:27 +10:00
|
|
|
connection = kodiSQL('video')
|
2015-12-25 06:51:47 +11:00
|
|
|
cursor = connection.cursor()
|
2016-01-16 14:07:36 +11:00
|
|
|
cursor.execute("SELECT url FROM art WHERE media_type != 'actor'") # dont include actors
|
2015-12-25 06:51:47 +11:00
|
|
|
result = cursor.fetchall()
|
2016-01-16 14:07:36 +11:00
|
|
|
total = len(result)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Image cache sync about to process %s images" % total)
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
count = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
for url in result:
|
2016-08-30 02:44:27 +10:00
|
|
|
|
|
|
|
if pdialog.iscanceled():
|
2016-01-19 18:58:42 +11:00
|
|
|
break
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-08-30 02:44:27 +10:00
|
|
|
message = "%s of %s (%s)" % (count, total, self.imageCacheThreads)
|
|
|
|
pdialog.update(percentage, "%s %s" % (lang(33045), message))
|
|
|
|
self.cacheTexture(url[0])
|
2016-01-16 14:07:36 +11:00
|
|
|
count += 1
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Cache all entries in music DB
|
2016-08-30 02:44:27 +10:00
|
|
|
connection = kodiSQL('music')
|
2015-12-25 06:51:47 +11:00
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("SELECT url FROM art")
|
|
|
|
result = cursor.fetchall()
|
2016-01-16 14:07:36 +11:00
|
|
|
total = len(result)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Image cache sync about to process %s images" % total)
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
count = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
for url in result:
|
2016-08-30 02:44:27 +10:00
|
|
|
|
|
|
|
if pdialog.iscanceled():
|
2016-04-26 21:53:19 +10:00
|
|
|
break
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-08-30 02:44:27 +10:00
|
|
|
message = "%s of %s" % (count, total)
|
|
|
|
pdialog.update(percentage, "%s %s" % (lang(33045), message))
|
|
|
|
self.cacheTexture(url[0])
|
2016-01-16 14:07:36 +11:00
|
|
|
count += 1
|
2016-08-30 02:44:27 +10:00
|
|
|
|
|
|
|
pdialog.update(100, "%s %s" % (lang(33046), len(self.imageCacheThreads)))
|
|
|
|
log.info("Waiting for all threads to exit")
|
|
|
|
|
|
|
|
while len(self.imageCacheThreads):
|
2016-01-16 14:07:36 +11:00
|
|
|
for thread in self.imageCacheThreads:
|
2016-08-30 02:44:27 +10:00
|
|
|
if thread.is_finished:
|
2016-01-16 14:07:36 +11:00
|
|
|
self.imageCacheThreads.remove(thread)
|
2016-08-30 02:44:27 +10:00
|
|
|
pdialog.update(100, "%s %s" % (lang(33046), len(self.imageCacheThreads)))
|
|
|
|
log.info("Waiting for all threads to exit: %s" % len(self.imageCacheThreads))
|
2016-01-16 14:07:36 +11:00
|
|
|
xbmc.sleep(500)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
pdialog.close()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
def addWorkerImageCacheThread(self, url):
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
while True:
|
2016-01-16 14:07:36 +11:00
|
|
|
# removed finished
|
|
|
|
for thread in self.imageCacheThreads:
|
2016-08-30 02:44:27 +10:00
|
|
|
if thread.is_finished:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.imageCacheThreads.remove(thread)
|
2016-01-16 14:07:36 +11:00
|
|
|
|
|
|
|
# add a new thread or wait and retry if we hit our limit
|
2016-08-30 02:44:27 +10:00
|
|
|
if len(self.imageCacheThreads) < self.imageCacheLimitThreads:
|
|
|
|
newThread = image_cache_thread.ImageCacheThread()
|
|
|
|
newThread.set_url(self.double_urlencode(url))
|
|
|
|
newThread.set_host(self.xbmc_host, self.xbmc_port)
|
|
|
|
newThread.set_auth(self.xbmc_username, self.xbmc_password)
|
2016-01-16 14:07:36 +11:00
|
|
|
newThread.start()
|
|
|
|
self.imageCacheThreads.append(newThread)
|
|
|
|
return
|
|
|
|
else:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Waiting for empty queue spot: %s" % len(self.imageCacheThreads))
|
2016-01-16 14:07:36 +11:00
|
|
|
xbmc.sleep(50)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
def cacheTexture(self, url):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Cache a single image url to the texture cache
|
|
|
|
if url and self.enableTextureCache:
|
2016-08-30 02:44:27 +10:00
|
|
|
self.logMsg("Processing: %s" % url, 2)
|
|
|
|
|
|
|
|
log.debug("Processing: %s" % url)
|
|
|
|
|
|
|
|
if not self.imageCacheLimitThreads:
|
|
|
|
# Add image to texture cache by simply calling it at the http endpoint
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
url = self.double_urlencode(url)
|
|
|
|
try: # Extreme short timeouts so we will have a exception.
|
|
|
|
response = requests.head(
|
2016-08-30 02:44:27 +10:00
|
|
|
url=("http://%s:%s/image/image://%s"
|
2016-01-16 14:07:36 +11:00
|
|
|
% (self.xbmc_host, self.xbmc_port, url)),
|
|
|
|
auth=(self.xbmc_username, self.xbmc_password),
|
|
|
|
timeout=(0.01, 0.01))
|
|
|
|
# We don't need the result
|
|
|
|
except: pass
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-01-16 14:07:36 +11:00
|
|
|
else:
|
|
|
|
self.addWorkerImageCacheThread(url)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-01-23 01:57:22 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def addArtwork(self, artwork, kodiId, mediaType, cursor):
|
|
|
|
# Kodi conversion table
|
|
|
|
kodiart = {
|
|
|
|
|
|
|
|
'Primary': ["thumb", "poster"],
|
|
|
|
'Banner': "banner",
|
|
|
|
'Logo': "clearlogo",
|
|
|
|
'Art': "clearart",
|
|
|
|
'Thumb': "landscape",
|
|
|
|
'Disc': "discart",
|
|
|
|
'Backdrop': "fanart",
|
|
|
|
'BoxRear': "poster"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Artwork is a dictionary
|
|
|
|
for art in artwork:
|
|
|
|
if art == "Backdrop":
|
|
|
|
# Backdrop entry is a list
|
|
|
|
# Process extra fanart for artwork downloader (fanart, fanart1, fanart2...)
|
|
|
|
backdrops = artwork[art]
|
|
|
|
backdropsNumber = len(backdrops)
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type LIKE ?"
|
|
|
|
))
|
|
|
|
cursor.execute(query, (kodiId, mediaType, "fanart%",))
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
if len(rows) > backdropsNumber:
|
|
|
|
# More backdrops in database. Delete extra fanart.
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type LIKE ?"
|
|
|
|
))
|
|
|
|
cursor.execute(query, (kodiId, mediaType, "fanart_",))
|
|
|
|
|
|
|
|
# Process backdrops and extra fanart
|
|
|
|
index = ""
|
|
|
|
for backdrop in backdrops:
|
|
|
|
self.addOrUpdateArt(
|
|
|
|
imageUrl=backdrop,
|
|
|
|
kodiId=kodiId,
|
|
|
|
mediaType=mediaType,
|
|
|
|
imageType="%s%s" % ("fanart", index),
|
|
|
|
cursor=cursor)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if backdropsNumber > 1:
|
|
|
|
try: # Will only fail on the first try, str to int.
|
|
|
|
index += 1
|
|
|
|
except TypeError:
|
|
|
|
index = 1
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
elif art == "Primary":
|
|
|
|
# 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,
|
|
|
|
cursor=cursor)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
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],
|
|
|
|
cursor=cursor)
|
|
|
|
|
|
|
|
def addOrUpdateArt(self, imageUrl, kodiId, mediaType, imageType, cursor):
|
|
|
|
# Possible that the imageurl is an empty string
|
|
|
|
if imageUrl:
|
|
|
|
cacheimage = False
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type = ?"
|
|
|
|
))
|
|
|
|
cursor.execute(query, (kodiId, mediaType, imageType,))
|
|
|
|
try: # Update the artwork
|
|
|
|
url = cursor.fetchone()[0]
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError: # Add the artwork
|
|
|
|
cacheimage = True
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Adding Art Link for kodiId: %s (%s)" % (kodiId, imageUrl))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO art(media_id, media_type, type, url)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2015-12-27 13:07:13 +11:00
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2015-12-25 06:51:47 +11:00
|
|
|
cursor.execute(query, (kodiId, mediaType, imageType, imageUrl))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
else: # Only cache artwork if it changed
|
|
|
|
if url != imageUrl:
|
|
|
|
cacheimage = True
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Only for the main backdrop, poster
|
2016-05-31 16:06:42 +10:00
|
|
|
if (utils.window('plex_initialScan') != "true" and
|
2015-12-28 15:51:47 +11:00
|
|
|
imageType in ("fanart", "poster")):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete current entry before updating with the new one
|
|
|
|
self.deleteCachedArtwork(url)
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Updating Art url for %s kodiId: %s (%s) -> (%s)"
|
|
|
|
% (imageType, kodiId, url, imageUrl))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE art",
|
|
|
|
"SET url = ?",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type = ?"
|
|
|
|
))
|
|
|
|
cursor.execute(query, (imageUrl, kodiId, mediaType, imageType))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Cache fanart and poster in Kodi texture cache
|
2016-04-29 20:45:40 +10:00
|
|
|
if cacheimage:
|
2016-08-30 02:44:27 +10:00
|
|
|
self.cacheTexture(imageUrl)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
def deleteArtwork(self, kodiId, mediaType, cursor):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT url, type",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-08-30 02:44:27 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType,))
|
2015-12-25 06:51:47 +11:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
|
|
|
|
url = row[0]
|
2016-08-30 02:44:27 +10:00
|
|
|
imageType = row[1]
|
|
|
|
if imageType in ("poster", "fanart"):
|
2015-12-25 06:51:47 +11:00
|
|
|
self.deleteCachedArtwork(url)
|
|
|
|
|
|
|
|
def deleteCachedArtwork(self, url):
|
|
|
|
# Only necessary to remove and apply a new backdrop or poster
|
2016-08-30 02:44:27 +10:00
|
|
|
connection = kodiSQL('texture')
|
2015-12-25 06:51:47 +11:00
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
try:
|
2016-02-29 14:39:00 +11:00
|
|
|
cursor.execute("SELECT cachedurl FROM texture WHERE url = ?", (url,))
|
2015-12-25 06:51:47 +11:00
|
|
|
cachedurl = cursor.fetchone()[0]
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Could not find cached url.")
|
2016-02-29 14:39:00 +11:00
|
|
|
|
|
|
|
except OperationalError:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Database is locked. Skip deletion process.")
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
else: # Delete thumbnail as well as the entry
|
2016-05-07 21:15:02 +10:00
|
|
|
thumbnails = utils.tryDecode(
|
|
|
|
xbmc.translatePath("special://thumbnails/%s" % cachedurl))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Deleting cached thumbnail: %s" % thumbnails)
|
2015-12-25 06:51:47 +11:00
|
|
|
xbmcvfs.delete(thumbnails)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
|
|
|
cursor.execute("DELETE FROM texture WHERE url = ?", (url,))
|
|
|
|
connection.commit()
|
2016-02-29 14:39:00 +11:00
|
|
|
except OperationalError:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Issue deleting url from cache. Skipping.")
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
finally:
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
def getPeopleArtwork(self, people):
|
|
|
|
# append imageurl if existing
|
|
|
|
for person in people:
|
|
|
|
|
|
|
|
personId = person['Id']
|
|
|
|
tag = person.get('PrimaryImageTag')
|
|
|
|
|
|
|
|
image = ""
|
|
|
|
if tag:
|
|
|
|
image = (
|
|
|
|
"%s/emby/Items/%s/Images/Primary?"
|
|
|
|
"MaxWidth=400&MaxHeight=400&Index=0&Tag=%s"
|
|
|
|
% (self.server, personId, tag))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
person['imageurl'] = image
|
|
|
|
|
|
|
|
return people
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
def getUserArtwork(self, itemId, itemType):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Load user information set by UserClient
|
|
|
|
image = ("%s/emby/Users/%s/Images/%s?Format=original"
|
2016-08-30 02:44:27 +10:00
|
|
|
% (self.server, itemId, itemType))
|
2015-12-25 06:51:47 +11:00
|
|
|
return image
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
|
2015-12-31 01:57:55 +11:00
|
|
|
def getAllArtwork(self, item, parentInfo=False):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-13 08:14:07 +11:00
|
|
|
itemid = item['Id']
|
2015-12-31 01:57:55 +11:00
|
|
|
artworks = item['ImageTags']
|
2016-08-30 02:44:27 +10:00
|
|
|
backdrops = item.get('BackdropImageTags', [])
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
maxHeight = 10000
|
|
|
|
maxWidth = 10000
|
|
|
|
customquery = ""
|
|
|
|
|
2016-03-04 01:28:44 +11:00
|
|
|
# if utils.settings('compressArt') == "true":
|
|
|
|
# customquery = "&Quality=90"
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-04 01:28:44 +11:00
|
|
|
# if utils.settings('enableCoverArt') == "false":
|
|
|
|
# customquery += "&EnableImageEnhancers=false"
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
allartworks = {
|
|
|
|
|
|
|
|
'Primary': "",
|
|
|
|
'Art': "",
|
|
|
|
'Banner': "",
|
|
|
|
'Logo': "",
|
|
|
|
'Thumb': "",
|
|
|
|
'Disc': "",
|
|
|
|
'Backdrop': []
|
|
|
|
}
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process backdrops
|
2016-03-13 08:14:07 +11:00
|
|
|
for index, tag in enumerate(backdrops):
|
2015-12-31 01:57:55 +11:00
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/Backdrop/%s?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-04-26 21:53:19 +10:00
|
|
|
% (self.server, itemid, index, maxWidth, maxHeight, tag, customquery))
|
2015-12-31 01:57:55 +11:00
|
|
|
allartworks['Backdrop'].append(artwork)
|
|
|
|
|
|
|
|
# Process the rest of the artwork
|
|
|
|
for art in artworks:
|
|
|
|
# Filter backcover
|
|
|
|
if art != "BoxRear":
|
|
|
|
tag = artworks[art]
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/%s/0?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-04-26 21:53:19 +10:00
|
|
|
% (self.server, itemid, art, maxWidth, maxHeight, tag, customquery))
|
2015-12-31 01:57:55 +11:00
|
|
|
allartworks[art] = artwork
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process parent items if the main item is missing artwork
|
|
|
|
if parentInfo:
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process parent backdrops
|
|
|
|
if not allartworks['Backdrop']:
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
parentId = item.get('ParentBackdropItemId')
|
|
|
|
if parentId:
|
|
|
|
# If there is a parentId, go through the parent backdrop list
|
|
|
|
parentbackdrops = item['ParentBackdropImageTags']
|
|
|
|
|
2016-03-13 08:14:07 +11:00
|
|
|
for index, tag in enumerate(parentbackdrops):
|
2015-12-25 06:51:47 +11:00
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/Backdrop/%s?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-04-26 21:53:19 +10:00
|
|
|
% (self.server, parentId, index, maxWidth, maxHeight, tag, customquery))
|
2015-12-25 06:51:47 +11:00
|
|
|
allartworks['Backdrop'].append(artwork)
|
|
|
|
|
|
|
|
# Process the rest of the artwork
|
|
|
|
parentartwork = ['Logo', 'Art', 'Thumb']
|
|
|
|
for parentart in parentartwork:
|
|
|
|
|
|
|
|
if not allartworks[parentart]:
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
parentId = item.get('Parent%sItemId' % parentart)
|
|
|
|
if parentId:
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
parentTag = item['Parent%sImageTag' % parentart]
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/%s/0?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-04-26 21:53:19 +10:00
|
|
|
% (self.server, parentId, parentart,
|
2015-12-25 06:51:47 +11:00
|
|
|
maxWidth, maxHeight, parentTag, customquery))
|
|
|
|
allartworks[parentart] = artwork
|
|
|
|
|
|
|
|
# Parent album works a bit differently
|
|
|
|
if not allartworks['Primary']:
|
|
|
|
|
|
|
|
parentId = item.get('AlbumId')
|
|
|
|
if parentId and item.get('AlbumPrimaryImageTag'):
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
parentTag = item['AlbumPrimaryImageTag']
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/Primary/0?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-04-26 21:53:19 +10:00
|
|
|
% (self.server, parentId, maxWidth, maxHeight, parentTag, customquery))
|
2015-12-25 06:51:47 +11:00
|
|
|
allartworks['Primary'] = artwork
|
|
|
|
|
2016-06-16 15:43:36 +10:00
|
|
|
return allartworks
|