2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
2017-01-21 03:21:51 +11:00
|
|
|
from json import dumps, loads
|
2015-12-25 06:51:47 +11:00
|
|
|
import requests
|
2017-05-04 04:30:33 +10:00
|
|
|
from shutil import rmtree
|
2016-09-17 22:02:50 +10:00
|
|
|
from urllib import quote_plus, unquote
|
2016-10-19 05:32:16 +11:00
|
|
|
from threading import Thread
|
2017-01-21 03:21:51 +11:00
|
|
|
from Queue import Queue, Empty
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-21 03:21:51 +11:00
|
|
|
from xbmc import executeJSONRPC, sleep, translatePath
|
2017-05-12 03:26:13 +10:00
|
|
|
from xbmcvfs import exists
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 20:21:13 +10:00
|
|
|
from utils import window, settings, language as lang, kodiSQL, tryEncode, \
|
2017-05-21 04:24:47 +10:00
|
|
|
thread_methods, dialog, exists_dir, tryDecode
|
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 03:39:59 +10:00
|
|
|
###############################################################################
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2016-08-30 03:39:59 +10:00
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
2016-03-24 02:57:49 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-21 03:21:51 +11:00
|
|
|
ARTWORK_QUEUE = Queue()
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 21:51:53 +10:00
|
|
|
def setKodiWebServerDetails():
|
|
|
|
"""
|
|
|
|
Get the Kodi webserver details - used to set the texture cache
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
xbmc_port = None
|
|
|
|
xbmc_username = None
|
|
|
|
xbmc_password = None
|
2016-09-11 21:51:53 +10:00
|
|
|
web_query = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
"setting": "services.webserver"
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2016-09-11 21:51:53 +10:00
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_query))
|
|
|
|
result = loads(result)
|
2016-09-11 21:51:53 +10:00
|
|
|
try:
|
|
|
|
xbmc_webserver_enabled = result['result']['value']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
xbmc_webserver_enabled = False
|
|
|
|
if not xbmc_webserver_enabled:
|
|
|
|
# Enable the webserver, it is disabled
|
2015-12-27 13:07:13 +11:00
|
|
|
web_port = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
2016-09-11 21:51:53 +10:00
|
|
|
"method": "Settings.SetSettingValue",
|
2015-12-27 13:07:13 +11:00
|
|
|
"params": {
|
2016-09-11 21:51:53 +10:00
|
|
|
"setting": "services.webserverport",
|
|
|
|
"value": 8080
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2015-12-27 13:07:13 +11:00
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_port))
|
2016-09-11 21:51:53 +10:00
|
|
|
xbmc_port = 8080
|
2015-12-27 13:07:13 +11:00
|
|
|
web_user = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
2016-09-11 21:51:53 +10:00
|
|
|
"method": "Settings.SetSettingValue",
|
2015-12-27 13:07:13 +11:00
|
|
|
"params": {
|
2016-09-11 21:51:53 +10:00
|
|
|
"setting": "services.webserver",
|
|
|
|
"value": True
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
2015-12-27 13:07:13 +11:00
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_user))
|
2016-09-11 21:51:53 +10:00
|
|
|
xbmc_username = "kodi"
|
|
|
|
# Webserver already enabled
|
|
|
|
web_port = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
"setting": "services.webserverport"
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_port))
|
|
|
|
result = loads(result)
|
2016-09-11 21:51:53 +10:00
|
|
|
try:
|
|
|
|
xbmc_port = result['result']['value']
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
pass
|
|
|
|
web_user = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
"setting": "services.webserverusername"
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_user))
|
|
|
|
result = loads(result)
|
2016-09-11 21:51:53 +10:00
|
|
|
try:
|
|
|
|
xbmc_username = result['result']['value']
|
2017-02-27 04:25:41 +11:00
|
|
|
except (TypeError, KeyError):
|
2016-09-11 21:51:53 +10:00
|
|
|
pass
|
|
|
|
web_pass = {
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "Settings.GetSettingValue",
|
|
|
|
"params": {
|
|
|
|
"setting": "services.webserverpassword"
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 03:21:51 +11:00
|
|
|
result = executeJSONRPC(dumps(web_pass))
|
|
|
|
result = loads(result)
|
2016-09-11 21:51:53 +10:00
|
|
|
try:
|
|
|
|
xbmc_password = result['result']['value']
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
return (xbmc_port, xbmc_username, xbmc_password)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-09-17 22:02:50 +10:00
|
|
|
def double_urlencode(text):
|
|
|
|
return quote_plus(quote_plus(text))
|
|
|
|
|
|
|
|
|
|
|
|
def double_urldecode(text):
|
|
|
|
return unquote(unquote(text))
|
|
|
|
|
|
|
|
|
2017-05-17 21:55:24 +10:00
|
|
|
@thread_methods(add_stops=['STOP_SYNC'],
|
|
|
|
add_suspends=['SUSPEND_LIBRARY_THREAD', 'DB_SCAN'])
|
2016-09-17 19:24:12 +10:00
|
|
|
class Image_Cache_Thread(Thread):
|
2016-09-17 22:02:50 +10:00
|
|
|
xbmc_host = 'localhost'
|
2016-09-17 19:24:12 +10:00
|
|
|
xbmc_port, xbmc_username, xbmc_password = setKodiWebServerDetails()
|
2016-10-18 07:34:15 +11:00
|
|
|
sleep_between = 50
|
2016-12-21 02:13:19 +11:00
|
|
|
# Potentially issues with limited number of threads
|
|
|
|
# Hence let Kodi wait till download is successful
|
|
|
|
timeout = (35.1, 35.1)
|
2016-09-17 18:58:08 +10:00
|
|
|
|
2017-01-21 03:21:51 +11:00
|
|
|
def __init__(self):
|
|
|
|
self.queue = ARTWORK_QUEUE
|
2016-09-17 19:24:12 +10:00
|
|
|
Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
2017-05-17 18:09:50 +10:00
|
|
|
thread_stopped = self.thread_stopped
|
|
|
|
thread_suspended = self.thread_suspended
|
2016-09-17 19:24:12 +10:00
|
|
|
queue = self.queue
|
2016-09-17 23:39:20 +10:00
|
|
|
sleep_between = self.sleep_between
|
2017-05-17 18:09:50 +10:00
|
|
|
while not thread_stopped():
|
2016-09-17 19:24:12 +10:00
|
|
|
# In the event the server goes offline
|
2017-05-17 18:09:50 +10:00
|
|
|
while thread_suspended():
|
2016-09-17 19:24:12 +10:00
|
|
|
# Set in service.py
|
2017-05-17 18:09:50 +10:00
|
|
|
if thread_stopped():
|
2016-09-17 19:24:12 +10:00
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
log.info("---===### Stopped Image_Cache_Thread ###===---")
|
|
|
|
return
|
2017-01-21 03:21:51 +11:00
|
|
|
sleep(1000)
|
2016-09-17 19:24:12 +10:00
|
|
|
try:
|
|
|
|
url = queue.get(block=False)
|
2017-01-21 03:21:51 +11:00
|
|
|
except Empty:
|
|
|
|
sleep(1000)
|
2016-09-17 19:24:12 +10:00
|
|
|
continue
|
2017-01-21 03:32:28 +11:00
|
|
|
sleeptime = 0
|
2016-09-17 19:24:12 +10:00
|
|
|
while True:
|
2016-09-17 22:02:50 +10:00
|
|
|
try:
|
|
|
|
requests.head(
|
|
|
|
url="http://%s:%s/image/image://%s"
|
|
|
|
% (self.xbmc_host, self.xbmc_port, url),
|
|
|
|
auth=(self.xbmc_username, self.xbmc_password),
|
2016-10-18 07:34:15 +11:00
|
|
|
timeout=self.timeout)
|
2016-09-17 22:02:50 +10:00
|
|
|
except requests.Timeout:
|
|
|
|
# We don't need the result, only trigger Kodi to start the
|
|
|
|
# download. All is well
|
|
|
|
break
|
|
|
|
except requests.ConnectionError:
|
2017-05-17 18:09:50 +10:00
|
|
|
if thread_stopped():
|
2017-01-25 06:12:46 +11:00
|
|
|
# Kodi terminated
|
|
|
|
break
|
2016-09-17 22:02:50 +10:00
|
|
|
# Server thinks its a DOS attack, ('error 10053')
|
|
|
|
# Wait before trying again
|
2017-01-21 03:32:28 +11:00
|
|
|
if sleeptime > 5:
|
2016-09-17 22:02:50 +10:00
|
|
|
log.error('Repeatedly got ConnectionError for url %s'
|
|
|
|
% double_urldecode(url))
|
|
|
|
break
|
|
|
|
log.debug('Were trying too hard to download art, server '
|
|
|
|
'over-loaded. Sleep %s seconds before trying '
|
|
|
|
'again to download %s'
|
2017-01-21 03:32:28 +11:00
|
|
|
% (2**sleeptime, double_urldecode(url)))
|
|
|
|
sleep((2**sleeptime)*1000)
|
|
|
|
sleeptime += 1
|
2016-09-17 22:02:50 +10:00
|
|
|
continue
|
|
|
|
except Exception as e:
|
|
|
|
log.error('Unknown exception for url %s: %s'
|
|
|
|
% (double_urldecode(url), e))
|
|
|
|
import traceback
|
|
|
|
log.error("Traceback:\n%s" % traceback.format_exc())
|
2016-09-17 19:24:12 +10:00
|
|
|
break
|
2016-09-17 22:02:50 +10:00
|
|
|
# We did not even get a timeout
|
|
|
|
break
|
|
|
|
queue.task_done()
|
2016-10-18 08:15:05 +11:00
|
|
|
log.debug('Cached art: %s' % double_urldecode(url))
|
2016-09-17 22:02:50 +10:00
|
|
|
# Sleep for a bit to reduce CPU strain
|
2017-01-21 03:21:51 +11:00
|
|
|
sleep(sleep_between)
|
2016-09-17 19:24:12 +10:00
|
|
|
log.info("---===### Stopped Image_Cache_Thread ###===---")
|
2016-09-17 18:58:08 +10:00
|
|
|
|
2016-09-17 19:24:12 +10:00
|
|
|
|
|
|
|
class Artwork():
|
|
|
|
enableTextureCache = settings('enableTextureCache') == "true"
|
|
|
|
if enableTextureCache:
|
2017-01-21 03:21:51 +11:00
|
|
|
queue = ARTWORK_QUEUE
|
2016-03-04 00:00:48 +11:00
|
|
|
|
2016-09-11 21:51:53 +10:00
|
|
|
def fullTextureCacheSync(self):
|
|
|
|
"""
|
|
|
|
This method will sync all Kodi artwork to textures13.db
|
|
|
|
and cache them locally. This takes diskspace!
|
|
|
|
"""
|
2017-01-25 05:59:38 +11:00
|
|
|
if not dialog('yesno', "Image Texture Cache", lang(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-16 14:07:36 +11:00
|
|
|
# ask to rest all existing or not
|
2017-01-25 05:59:38 +11:00
|
|
|
if dialog('yesno', "Image Texture Cache", lang(39251)):
|
2016-08-30 03:39:59 +10:00
|
|
|
log.info("Resetting all cache data first")
|
2016-01-16 14:07:36 +11:00
|
|
|
# Remove all existing textures first
|
2017-05-21 04:24:47 +10:00
|
|
|
path = tryDecode(translatePath("special://thumbnails/"))
|
2017-05-12 03:26:13 +10:00
|
|
|
if exists_dir(path):
|
2017-05-04 04:30:33 +10:00
|
|
|
rmtree(path, ignore_errors=True)
|
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()
|
2017-05-04 04:30:33 +10:00
|
|
|
query = 'SELECT tbl_name FROM sqlite_master WHERE type=?'
|
|
|
|
cursor.execute(query, ('table', ))
|
2016-08-30 02:44:27 +10:00
|
|
|
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":
|
2017-05-12 21:25:46 +10:00
|
|
|
cursor.execute("DELETE FROM %s" % tableName)
|
2016-08-30 02:44:27 +10:00
|
|
|
connection.commit()
|
2016-09-11 21:51:53 +10:00
|
|
|
connection.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-09-11 21:51:53 +10:00
|
|
|
# dont include actors
|
2017-05-04 04:30:33 +10:00
|
|
|
query = "SELECT url FROM art WHERE media_type != ?"
|
|
|
|
cursor.execute(query, ('actor', ))
|
2015-12-25 06:51:47 +11:00
|
|
|
result = cursor.fetchall()
|
2016-01-16 14:07:36 +11:00
|
|
|
total = len(result)
|
2016-09-17 22:48:40 +10:00
|
|
|
log.info("Image cache sync about to process %s video images" % total)
|
2016-09-11 21:51:53 +10:00
|
|
|
connection.close()
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
for url in result:
|
2016-08-30 02:44:27 +10:00
|
|
|
self.cacheTexture(url[0])
|
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-09-17 22:48:40 +10:00
|
|
|
log.info("Image cache sync about to process %s music images" % total)
|
2016-09-11 21:51:53 +10:00
|
|
|
connection.close()
|
2015-12-25 06:51:47 +11:00
|
|
|
for url in result:
|
2016-08-30 02:44:27 +10:00
|
|
|
self.cacheTexture(url[0])
|
2015-12-25 06:51:47 +11: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:
|
2017-01-08 21:38:37 +11:00
|
|
|
self.queue.put(double_urlencode(tryEncode(url)))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 23:49:50 +10:00
|
|
|
def addArtwork(self, artwork, kodiId, mediaType, cursor):
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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
|
2016-09-11 21:51:53 +10:00
|
|
|
# Process extra fanart for artwork downloader (fanart, fanart1,
|
|
|
|
# fanart2...)
|
2015-12-25 06:51:47 +11:00
|
|
|
backdrops = artwork[art]
|
|
|
|
backdropsNumber = len(backdrops)
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type LIKE ?"
|
|
|
|
))
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType, "fanart%",))
|
|
|
|
rows = cursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
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 ?"
|
|
|
|
))
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType, "fanart_",))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process backdrops and extra fanart
|
|
|
|
index = ""
|
|
|
|
for backdrop in backdrops:
|
|
|
|
self.addOrUpdateArt(
|
|
|
|
imageUrl=backdrop,
|
|
|
|
kodiId=kodiId,
|
|
|
|
mediaType=mediaType,
|
2016-09-11 23:49:50 +10:00
|
|
|
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:
|
2017-05-04 04:30:33 +10:00
|
|
|
try: # Will only fail on the first try, str to int.
|
2015-12-25 06:51:47 +11:00
|
|
|
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,
|
2016-09-11 23:49:50 +10:00
|
|
|
imageType=artType,
|
|
|
|
cursor=cursor)
|
|
|
|
|
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,
|
2016-09-11 23:49:50 +10:00
|
|
|
imageType=kodiart[art],
|
|
|
|
cursor=cursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 23:49:50 +10:00
|
|
|
def addOrUpdateArt(self, imageUrl, kodiId, mediaType, imageType, cursor):
|
2016-09-11 21:51:53 +10:00
|
|
|
if not imageUrl:
|
|
|
|
# Possible that the imageurl is an empty string
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 21:51:53 +10:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type = ?"
|
|
|
|
))
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType, imageType,))
|
2016-09-11 21:51:53 +10:00
|
|
|
try:
|
|
|
|
# Update the artwork
|
2016-09-11 23:49:50 +10:00
|
|
|
url = cursor.fetchone()[0]
|
2016-09-11 21:51:53 +10:00
|
|
|
except TypeError:
|
|
|
|
# Add the artwork
|
|
|
|
log.debug("Adding Art Link for kodiId: %s (%s)"
|
|
|
|
% (kodiId, imageUrl))
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO art(media_id, media_type, type, url)
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType, imageType, imageUrl))
|
2016-09-11 21:51:53 +10:00
|
|
|
else:
|
|
|
|
if url == imageUrl:
|
|
|
|
# Only cache artwork if it changed
|
|
|
|
return
|
|
|
|
# Only for the main backdrop, poster
|
|
|
|
if (window('plex_initialScan') != "true" and
|
|
|
|
imageType in ("fanart", "poster")):
|
|
|
|
# Delete current entry before updating with the new one
|
|
|
|
self.deleteCachedArtwork(url)
|
|
|
|
log.debug("Updating Art url for %s kodiId %s %s -> (%s)"
|
|
|
|
% (imageType, kodiId, url, imageUrl))
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
2016-09-11 21:51:53 +10:00
|
|
|
"UPDATE art",
|
|
|
|
"SET url = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type = ?"
|
|
|
|
))
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (imageUrl, kodiId, mediaType, imageType))
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2016-09-11 21:51:53 +10:00
|
|
|
# Cache fanart and poster in Kodi texture cache
|
2017-01-21 02:30:37 +11:00
|
|
|
if mediaType != 'actor':
|
|
|
|
self.cacheTexture(imageUrl)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 23:49:50 +10:00
|
|
|
def deleteArtwork(self, kodiId, mediaType, cursor):
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
2016-09-11 21:51:53 +10:00
|
|
|
"SELECT url",
|
2015-12-25 06:51:47 +11:00
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-09-11 23:49:50 +10:00
|
|
|
cursor.execute(query, (kodiId, mediaType,))
|
|
|
|
rows = cursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
for row in rows:
|
2016-09-11 21:51:53 +10:00
|
|
|
self.deleteCachedArtwork(row[0])
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
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-09-11 21:51:53 +10:00
|
|
|
cursor.execute("SELECT cachedurl FROM texture WHERE url = ?",
|
|
|
|
(url,))
|
2015-12-25 06:51:47 +11:00
|
|
|
cachedurl = cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info("Could not find cached url.")
|
2016-09-11 21:51:53 +10:00
|
|
|
else:
|
|
|
|
# Delete thumbnail as well as the entry
|
2017-05-04 04:30:33 +10:00
|
|
|
path = translatePath("special://thumbnails/%s" % cachedurl)
|
|
|
|
log.debug("Deleting cached thumbnail: %s" % path)
|
|
|
|
if exists(path):
|
2017-05-21 04:24:47 +10:00
|
|
|
rmtree(tryDecode(path), ignore_errors=True)
|
2016-09-17 22:02:50 +10:00
|
|
|
cursor.execute("DELETE FROM texture WHERE url = ?", (url,))
|
|
|
|
connection.commit()
|
2015-12-25 06:51:47 +11:00
|
|
|
finally:
|
2016-09-11 21:51:53 +10:00
|
|
|
connection.close()
|