PlexKodiConnect/resources/lib/artwork.py

361 lines
13 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-02-20 06:03:06 +11:00
###############################################################################
2017-12-09 05:43:06 +11:00
from logging import getLogger
import requests
from shutil import rmtree
from urllib import quote_plus, unquote
2016-10-19 05:32:16 +11:00
from threading import Thread
from Queue import Queue, Empty
2017-12-09 05:43:06 +11:00
import json_rpc as js
2017-12-09 05:43:06 +11:00
from xbmc import sleep, translatePath
from xbmcvfs import exists
2016-09-11 20:21:13 +10:00
from utils import window, settings, language as lang, kodiSQL, tryEncode, \
thread_methods, dialog, exists_dir, tryDecode
# Disable annoying requests warnings
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
2016-08-30 03:39:59 +10:00
###############################################################################
2017-12-09 05:43:06 +11:00
LOG = getLogger("PLEX." + __name__)
2016-02-20 06:03:06 +11:00
###############################################################################
ARTWORK_QUEUE = Queue()
2016-09-11 21:51:53 +10:00
def setKodiWebServerDetails():
"""
Get the Kodi webserver details - used to set the texture cache
"""
xbmc_port = None
xbmc_username = None
xbmc_password = None
2017-12-09 05:43:06 +11:00
if js.get_setting('services.webserver') in (None, False):
2016-09-11 21:51:53 +10:00
# Enable the webserver, it is disabled
xbmc_port = 8080
xbmc_username = "kodi"
2017-12-09 05:43:06 +11:00
js.set_setting('services.webserverport', xbmc_port)
js.set_setting('services.webserver', True)
2016-09-11 21:51:53 +10:00
# Webserver already enabled
2017-12-09 05:43:06 +11:00
xbmc_port = js.get_setting('services.webserverport')
xbmc_username = js.get_setting('services.webserverusername')
xbmc_password = js.get_setting('services.webserverpassword')
2016-09-11 21:51:53 +10:00
return (xbmc_port, xbmc_username, xbmc_password)
def double_urlencode(text):
return quote_plus(quote_plus(text))
def double_urldecode(text):
return unquote(unquote(text))
@thread_methods(add_suspends=['SUSPEND_LIBRARY_THREAD',
'DB_SCAN',
'STOP_SYNC'])
class Image_Cache_Thread(Thread):
xbmc_host = 'localhost'
xbmc_port, xbmc_username, xbmc_password = setKodiWebServerDetails()
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)
def __init__(self):
self.queue = ARTWORK_QUEUE
Thread.__init__(self)
def run(self):
thread_stopped = self.thread_stopped
thread_suspended = self.thread_suspended
queue = self.queue
sleep_between = self.sleep_between
while not thread_stopped():
# In the event the server goes offline
while thread_suspended():
# Set in service.py
if thread_stopped():
# Abort was requested while waiting. We should exit
2017-12-09 05:43:06 +11:00
LOG.info("---===### Stopped Image_Cache_Thread ###===---")
return
sleep(1000)
try:
url = queue.get(block=False)
except Empty:
sleep(1000)
continue
2017-01-21 03:32:28 +11:00
sleeptime = 0
while True:
try:
requests.head(
url="http://%s:%s/image/image://%s"
% (self.xbmc_host, self.xbmc_port, url),
auth=(self.xbmc_username, self.xbmc_password),
timeout=self.timeout)
except requests.Timeout:
# We don't need the result, only trigger Kodi to start the
# download. All is well
break
except requests.ConnectionError:
if thread_stopped():
2017-01-25 06:12:46 +11:00
# Kodi terminated
break
# Server thinks its a DOS attack, ('error 10053')
# Wait before trying again
2017-01-21 03:32:28 +11:00
if sleeptime > 5:
2017-12-09 05:43:06 +11:00
LOG.error('Repeatedly got ConnectionError for url %s'
% double_urldecode(url))
break
2017-12-09 05:43:06 +11:00
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
continue
except Exception as e:
2017-12-09 05:43:06 +11:00
LOG.error('Unknown exception for url %s: %s'
% (double_urldecode(url), e))
import traceback
2017-12-09 05:43:06 +11:00
LOG.error("Traceback:\n%s" % traceback.format_exc())
break
# We did not even get a timeout
break
queue.task_done()
2017-12-09 05:43:06 +11:00
LOG.debug('Cached art: %s' % double_urldecode(url))
# Sleep for a bit to reduce CPU strain
sleep(sleep_between)
2017-12-09 05:43:06 +11:00
LOG.info("---===### Stopped Image_Cache_Thread ###===---")
class Artwork():
enableTextureCache = settings('enableTextureCache') == "true"
if enableTextureCache:
queue = ARTWORK_QUEUE
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)):
return
2017-12-09 05:43:06 +11:00
LOG.info("Doing Image Cache Sync")
# ask to rest all existing or not
2017-01-25 05:59:38 +11:00
if dialog('yesno', "Image Texture Cache", lang(39251)):
2017-12-09 05:43:06 +11:00
LOG.info("Resetting all cache data first")
# Remove all existing textures first
path = tryDecode(translatePath("special://thumbnails/"))
if exists_dir(path):
rmtree(path, ignore_errors=True)
# remove all existing data from texture DB
connection = kodiSQL('texture')
cursor = connection.cursor()
query = 'SELECT tbl_name FROM sqlite_master WHERE type=?'
cursor.execute(query, ('table', ))
rows = cursor.fetchall()
for row in rows:
tableName = row[0]
if tableName != "version":
2017-05-12 21:25:46 +10:00
cursor.execute("DELETE FROM %s" % tableName)
connection.commit()
2016-09-11 21:51:53 +10:00
connection.close()
# Cache all entries in video DB
connection = kodiSQL('video')
cursor = connection.cursor()
2016-09-11 21:51:53 +10:00
# dont include actors
query = "SELECT url FROM art WHERE media_type != ?"
cursor.execute(query, ('actor', ))
result = cursor.fetchall()
total = len(result)
2017-12-09 05:43:06 +11:00
LOG.info("Image cache sync about to process %s video images" % total)
2016-09-11 21:51:53 +10:00
connection.close()
for url in result:
self.cacheTexture(url[0])
# Cache all entries in music DB
connection = kodiSQL('music')
cursor = connection.cursor()
cursor.execute("SELECT url FROM art")
result = cursor.fetchall()
total = len(result)
2017-12-09 05:43:06 +11:00
LOG.info("Image cache sync about to process %s music images" % total)
2016-09-11 21:51:53 +10:00
connection.close()
for url in result:
self.cacheTexture(url[0])
def cacheTexture(self, url):
# 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)))
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
2016-09-11 21:51:53 +10:00
# 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)
if backdropsNumber > 1:
try: # Will only fail on the first try, str to int.
index += 1
except TypeError:
index = 1
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)
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):
2016-09-11 21:51:53 +10:00
if not imageUrl:
# Possible that the imageurl is an empty string
return
2016-09-11 21:51:53 +10:00
query = ' '.join((
"SELECT url",
"FROM art",
"WHERE media_id = ?",
"AND media_type = ?",
"AND type = ?"
))
cursor.execute(query, (kodiId, mediaType, imageType,))
2016-09-11 21:51:53 +10:00
try:
# Update the artwork
url = cursor.fetchone()[0]
2016-09-11 21:51:53 +10:00
except TypeError:
# Add the artwork
2017-12-09 05:43:06 +11:00
LOG.debug("Adding Art Link for kodiId: %s (%s)"
2016-09-11 21:51:53 +10:00
% (kodiId, imageUrl))
query = (
'''
INSERT INTO art(media_id, media_type, type, url)
VALUES (?, ?, ?, ?)
'''
)
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)
2017-12-09 05:43:06 +11:00
LOG.debug("Updating Art url for %s kodiId %s %s -> (%s)"
2016-09-11 21:51:53 +10:00
% (imageType, kodiId, url, imageUrl))
query = ' '.join((
2016-09-11 21:51:53 +10:00
"UPDATE art",
"SET url = ?",
"WHERE media_id = ?",
"AND media_type = ?",
"AND type = ?"
))
cursor.execute(query, (imageUrl, kodiId, mediaType, imageType))
2016-09-11 21:51:53 +10:00
# Cache fanart and poster in Kodi texture cache
if mediaType != 'actor':
self.cacheTexture(imageUrl)
def deleteArtwork(self, kodiId, mediaType, cursor):
query = ' '.join((
2016-09-11 21:51:53 +10:00
"SELECT url",
"FROM art",
"WHERE media_id = ?",
"AND media_type = ?"
))
cursor.execute(query, (kodiId, mediaType,))
rows = cursor.fetchall()
for row in rows:
2016-09-11 21:51:53 +10:00
self.deleteCachedArtwork(row[0])
def deleteCachedArtwork(self, url):
# Only necessary to remove and apply a new backdrop or poster
connection = kodiSQL('texture')
cursor = connection.cursor()
try:
2016-09-11 21:51:53 +10:00
cursor.execute("SELECT cachedurl FROM texture WHERE url = ?",
(url,))
cachedurl = cursor.fetchone()[0]
except TypeError:
2017-12-09 05:43:06 +11:00
LOG.info("Could not find cached url.")
2016-09-11 21:51:53 +10:00
else:
# Delete thumbnail as well as the entry
path = translatePath("special://thumbnails/%s" % cachedurl)
2017-12-09 05:43:06 +11:00
LOG.debug("Deleting cached thumbnail: %s" % path)
if exists(path):
rmtree(tryDecode(path), ignore_errors=True)
cursor.execute("DELETE FROM texture WHERE url = ?", (url,))
connection.commit()
finally:
2016-09-11 21:51:53 +10:00
connection.close()