Rewire artwork caching

This commit is contained in:
croneter 2018-11-05 18:00:01 +01:00
parent e761567592
commit 07c4d64a84
9 changed files with 249 additions and 312 deletions

View file

@ -2,11 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals from __future__ import absolute_import, division, unicode_literals
from logging import getLogger from logging import getLogger
from Queue import Queue, Empty
from urllib import quote_plus, unquote from urllib import quote_plus, unquote
from threading import Thread
import requests import requests
import xbmc import xbmc
from . import backgroundthread, path_ops, utils from . import backgroundthread, path_ops, utils
@ -17,7 +14,11 @@ LOG = getLogger('PLEX.artwork')
# Disable annoying requests warnings # Disable annoying requests warnings
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
ARTWORK_QUEUE = Queue()
# Potentially issues with limited number of threads Hence let Kodi wait till
# download is successful
TIMEOUT = (35.1, 35.1)
IMAGE_CACHING_SUSPENDS = [ IMAGE_CACHING_SUSPENDS = [
state.SUSPEND_LIBRARY_THREAD, state.SUSPEND_LIBRARY_THREAD,
state.DB_SCAN state.DB_SCAN
@ -37,20 +38,19 @@ def double_urldecode(text):
class ImageCachingThread(backgroundthread.KillableThread): class ImageCachingThread(backgroundthread.KillableThread):
# Potentially issues with limited number of threads
# Hence let Kodi wait till download is successful
timeout = (35.1, 35.1)
def __init__(self): def __init__(self):
self.queue = ARTWORK_QUEUE self._canceled = False
Thread.__init__(self) super(ImageCachingThread, self).__init__()
def isCanceled(self): def isCanceled(self):
return state.STOP_PKC return self._canceled or state.STOP_PKC
def isSuspended(self): def isSuspended(self):
return any(IMAGE_CACHING_SUSPENDS) return any(IMAGE_CACHING_SUSPENDS)
def cancel(self):
self._canceled = True
@staticmethod @staticmethod
def _art_url_generator(): def _art_url_generator():
from . import kodidb_functions as kodidb from . import kodidb_functions as kodidb
@ -67,7 +67,24 @@ class ImageCachingThread(backgroundthread.KillableThread):
if kodi_db.url_not_yet_cached(url): if kodi_db.url_not_yet_cached(url):
yield url yield url
def _cache_url(self, url): def run(self):
LOG.info("---===### Starting ImageCachingThread ###===---")
# Cache already synced artwork first
for url in self.missing_art_cache_generator():
if self.isCanceled():
return
while self.isSuspended():
# Set in service.py
if self.isCanceled():
# Abort was requested while waiting. We should exit
LOG.info("---===### Stopped ImageCachingThread ###===---")
return
xbmc.sleep(1000)
cache_url(url)
LOG.info("---===### Stopped ImageCachingThread ###===---")
def cache_url(url):
url = double_urlencode(utils.try_encode(url)) url = double_urlencode(utils.try_encode(url))
sleeptime = 0 sleeptime = 0
while True: while True:
@ -79,13 +96,13 @@ class ImageCachingThread(backgroundthread.KillableThread):
url), url),
auth=(state.WEBSERVER_USERNAME, auth=(state.WEBSERVER_USERNAME,
state.WEBSERVER_PASSWORD), state.WEBSERVER_PASSWORD),
timeout=self.timeout) timeout=TIMEOUT)
except requests.Timeout: except requests.Timeout:
# We don't need the result, only trigger Kodi to start the # We don't need the result, only trigger Kodi to start the
# download. All is well # download. All is well
break break
except requests.ConnectionError: except requests.ConnectionError:
if self.isCanceled(): if state.STOP_PKC:
# Kodi terminated # Kodi terminated
break break
# Server thinks its a DOS attack, ('error 10053') # Server thinks its a DOS attack, ('error 10053')
@ -110,128 +127,16 @@ class ImageCachingThread(backgroundthread.KillableThread):
# We did not even get a timeout # We did not even get a timeout
break break
def run(self):
LOG.info("---===### Starting ImageCachingThread ###===---")
# Cache already synced artwork first
for url in self.missing_art_cache_generator():
if self.isCanceled():
return
while self.isSuspended():
# Set in service.py
if self.isCanceled():
# Abort was requested while waiting. We should exit
LOG.info("---===### Stopped ImageCachingThread ###===---")
return
xbmc.sleep(1000)
self._cache_url(url)
# Now wait for more stuff to cache - via the queue def modify_artwork(artworks, kodi_id, kodi_type, cursor):
while not self.isCanceled():
# In the event the server goes offline
while self.isSuspended():
# Set in service.py
if self.isCanceled():
# Abort was requested while waiting. We should exit
LOG.info("---===### Stopped ImageCachingThread ###===---")
return
xbmc.sleep(1000)
try:
url = self.queue.get(block=False)
except Empty:
xbmc.sleep(1000)
continue
if isinstance(url, ArtworkSyncMessage):
if state.IMAGE_SYNC_NOTIFICATIONS:
utils.dialog('notification',
heading=utils.lang(29999),
message=url.message,
icon='{plex}',
sound=False)
self.queue.task_done()
continue
self._cache_url(url)
self.queue.task_done()
# Sleep for a bit to reduce CPU strain
xbmc.sleep(100)
LOG.info("---===### Stopped ImageCachingThread ###===---")
class Artwork():
enableTextureCache = utils.settings('enableTextureCache') == "true"
if enableTextureCache:
queue = ARTWORK_QUEUE
def fullTextureCacheSync(self):
"""
This method will sync all Kodi artwork to textures13.db
and cache them locally. This takes diskspace!
"""
if not utils.yesno_dialog("Image Texture Cache", utils.lang(39250)):
return
LOG.info("Doing Image Cache Sync")
# ask to rest all existing or not
if utils.yesno_dialog("Image Texture Cache", utils.lang(39251)):
LOG.info("Resetting all cache data first")
# Remove all existing textures first
path = path_ops.translate_path('special://thumbnails/')
if path_ops.exists(path):
path_ops.rmtree(path, ignore_errors=True)
self.restore_cache_directories()
# remove all existing data from texture DB
connection = utils.kodi_sql('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":
cursor.execute("DELETE FROM %s" % tableName)
connection.commit()
connection.close()
# Cache all entries in video DB
connection = utils.kodi_sql('video')
cursor = connection.cursor()
# dont include actors
query = "SELECT url FROM art WHERE media_type != ?"
cursor.execute(query, ('actor', ))
result = cursor.fetchall()
total = len(result)
LOG.info("Image cache sync about to process %s video images", total)
connection.close()
for url in result:
self.cache_texture(url[0])
# Cache all entries in music DB
connection = utils.kodi_sql('music')
cursor = connection.cursor()
cursor.execute("SELECT url FROM art")
result = cursor.fetchall()
total = len(result)
LOG.info("Image cache sync about to process %s music images", total)
connection.close()
for url in result:
self.cache_texture(url[0])
def cache_texture(self, url):
'''
Cache a single image url to the texture cache. url: unicode
'''
if url and self.enableTextureCache:
self.queue.put(url)
def modify_artwork(self, artworks, kodi_id, kodi_type, cursor):
""" """
Pass in an artworks dict (see PlexAPI) to set an items artwork. Pass in an artworks dict (see PlexAPI) to set an items artwork.
""" """
for kodi_art, url in artworks.iteritems(): for kodi_art, url in artworks.iteritems():
self.modify_art(url, kodi_id, kodi_type, kodi_art, cursor) modify_art(url, kodi_id, kodi_type, kodi_art, cursor)
def modify_art(self, url, kodi_id, kodi_type, kodi_art, cursor):
def modify_art(url, kodi_id, kodi_type, kodi_art, cursor):
""" """
Adds or modifies the artwork of kind kodi_art (e.g. 'poster') in the Adds or modifies the artwork of kind kodi_art (e.g. 'poster') in the
Kodi art table for item kodi_id/kodi_type. Will also cache everything Kodi art table for item kodi_id/kodi_type. Will also cache everything
@ -257,33 +162,31 @@ class Artwork():
if url == old_url: if url == old_url:
# Only cache artwork if it changed # Only cache artwork if it changed
return return
self.delete_cached_artwork(old_url) delete_cached_artwork(old_url)
query = ''' query = '''
UPDATE art SET url = ? UPDATE art SET url = ?
WHERE media_id = ? AND media_type = ? AND type = ? WHERE media_id = ? AND media_type = ? AND type = ?
''' '''
cursor.execute(query, (url, kodi_id, kodi_type, kodi_art)) cursor.execute(query, (url, kodi_id, kodi_type, kodi_art))
# Cache fanart and poster in Kodi texture cache
if kodi_type != 'actor':
self.cache_texture(url)
def delete_artwork(self, kodiId, mediaType, cursor):
query = 'SELECT url FROM art WHERE media_id = ? AND media_type = ?' def delete_artwork(kodiId, mediaType, cursor):
cursor.execute(query, (kodiId, mediaType,)) cursor.execute('SELECT url FROM art WHERE media_id = ? AND media_type = ?',
(kodiId, mediaType, ))
for row in cursor.fetchall(): for row in cursor.fetchall():
self.delete_cached_artwork(row[0]) delete_cached_artwork(row[0])
@staticmethod
def delete_cached_artwork(url): def delete_cached_artwork(url):
""" """
Deleted the cached artwork with path url (if it exists) Deleted the cached artwork with path url (if it exists)
""" """
connection = utils.kodi_sql('texture') from . import kodidb_functions as kodidb
cursor = connection.cursor() with kodidb.GetKodiDB('texture') as kodi_db:
try: try:
cursor.execute("SELECT cachedurl FROM texture WHERE url=? LIMIT 1", kodi_db.cursor.execute("SELECT cachedurl FROM texture WHERE url=? LIMIT 1",
(url,)) (url, ))
cachedurl = cursor.fetchone()[0] cachedurl = kodi_db.cursor.fetchone()[0]
except TypeError: except TypeError:
# Could not find cached url # Could not find cached url
pass pass
@ -293,25 +196,4 @@ class Artwork():
% cachedurl) % cachedurl)
if path_ops.exists(path): if path_ops.exists(path):
path_ops.rmtree(path, ignore_errors=True) path_ops.rmtree(path, ignore_errors=True)
cursor.execute("DELETE FROM texture WHERE url = ?", (url,)) kodi_db.cursor.execute("DELETE FROM texture WHERE url = ?", (url,))
connection.commit()
finally:
connection.close()
@staticmethod
def restore_cache_directories():
LOG.info("Restoring cache directories...")
paths = ("", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f",
"Video", "plex")
for path in paths:
new_path = path_ops.translate_path("special://thumbnails/%s" % path)
path_ops.makedirs(path_ops.encode_path(new_path))
class ArtworkSyncMessage(object):
"""
Put in artwork queue to display the message as a Kodi notification
"""
def __init__(self, message):
self.message = message

View file

@ -7,7 +7,7 @@ from ntpath import dirname
from ..plex_api import API from ..plex_api import API
from ..plex_db import PlexDB from ..plex_db import PlexDB
from .. import kodidb_functions as kodidb from .. import kodidb_functions as kodidb
from .. import artwork, utils, variables as v from .. import artwork, utils
LOG = getLogger('PLEX.itemtypes.common') LOG = getLogger('PLEX.itemtypes.common')
@ -41,7 +41,6 @@ class ItemBase(object):
""" """
def __init__(self, last_sync, plexdb=None, kodi_db=None): def __init__(self, last_sync, plexdb=None, kodi_db=None):
self.last_sync = last_sync self.last_sync = last_sync
self.artwork = artwork.Artwork()
self.plexconn = None self.plexconn = None
self.plexcursor = plexdb.cursor if plexdb else None self.plexcursor = plexdb.cursor if plexdb else None
self.kodiconn = None self.kodiconn = None
@ -75,7 +74,7 @@ class ItemBase(object):
""" """
Writes artworks [dict containing only set artworks] to the Kodi art DB Writes artworks [dict containing only set artworks] to the Kodi art DB
""" """
self.artwork.modify_artwork(artworks, artwork.modify_artwork(artworks,
kodi_id, kodi_id,
kodi_type, kodi_type,
self.kodicursor) self.kodicursor)

View file

@ -5,7 +5,7 @@ from logging import getLogger
from .common import ItemBase from .common import ItemBase
from ..plex_api import API from ..plex_api import API
from .. import state, variables as v, plex_functions as PF from .. import artwork, state, variables as v, plex_functions as PF
LOG = getLogger('PLEX.movies') LOG = getLogger('PLEX.movies')
@ -165,7 +165,7 @@ class Movie(ItemBase):
v.KODI_TYPE_MOVIE, v.KODI_TYPE_MOVIE,
api.people_list()) api.people_list())
self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_MOVIE, genres) self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_MOVIE, genres)
self.artwork.modify_artwork(api.artwork(), artwork.modify_artwork(api.artwork(),
kodi_id, kodi_id,
v.KODI_TYPE_MOVIE, v.KODI_TYPE_MOVIE,
self.kodicursor) self.kodicursor)
@ -190,7 +190,7 @@ class Movie(ItemBase):
coll_plex_id) coll_plex_id)
continue continue
set_api = API(set_xml[0]) set_api = API(set_xml[0])
self.artwork.modify_artwork(set_api.artwork(), artwork.modify_artwork(set_api.artwork(),
kodi_set_id, kodi_set_id,
v.KODI_TYPE_SET, v.KODI_TYPE_SET,
self.kodicursor) self.kodicursor)
@ -230,7 +230,7 @@ class Movie(ItemBase):
# Remove the plex reference # Remove the plex reference
self.plexdb.remove(plex_id, v.PLEX_TYPE_MOVIE) self.plexdb.remove(plex_id, v.PLEX_TYPE_MOVIE)
# Remove artwork # Remove artwork
self.artwork.delete_artwork(kodi_id, kodi_type, self.self.kodicursor) artwork.delete_artwork(kodi_id, kodi_type, self.self.kodicursor)
set_id = self.kodi_db.get_set_id(kodi_id) set_id = self.kodi_db.get_set_id(kodi_id)
self.kodi_db.modify_countries(kodi_id, kodi_type) self.kodi_db.modify_countries(kodi_id, kodi_type)
self.kodi_db.modify_people(kodi_id, kodi_type) self.kodi_db.modify_people(kodi_id, kodi_type)

View file

@ -6,7 +6,7 @@ from logging import getLogger
from .common import ItemBase from .common import ItemBase
from ..plex_api import API from ..plex_api import API
from ..plex_db import PlexDB from ..plex_db import PlexDB
from .. import kodidb_functions as kodidb from .. import artwork, kodidb_functions as kodidb
from .. import plex_functions as PF, utils, state, variables as v from .. import plex_functions as PF, utils, state, variables as v
LOG = getLogger('PLEX.music') LOG = getLogger('PLEX.music')
@ -106,7 +106,7 @@ class MusicMixin(object):
self.kodi_db.delete_song_from_song_genre(kodi_id) self.kodi_db.delete_song_from_song_genre(kodi_id)
query = 'DELETE FROM albuminfosong WHERE idAlbumInfoSong = ?' query = 'DELETE FROM albuminfosong WHERE idAlbumInfoSong = ?'
self.kodicursor.execute(query, (kodi_id, )) self.kodicursor.execute(query, (kodi_id, ))
self.artwork.delete_artwork(kodi_id, v.KODI_TYPE_SONG, self.kodicursor) artwork.delete_artwork(kodi_id, v.KODI_TYPE_SONG, self.kodicursor)
def remove_album(self, kodi_id): def remove_album(self, kodi_id):
''' '''
@ -121,7 +121,7 @@ class MusicMixin(object):
(kodi_id, )) (kodi_id, ))
self.kodicursor.execute('DELETE FROM album WHERE idAlbum = ?', self.kodicursor.execute('DELETE FROM album WHERE idAlbum = ?',
(kodi_id, )) (kodi_id, ))
self.artwork.delete_artwork(kodi_id, v.KODI_TYPE_ALBUM, self.kodicursor) artwork.delete_artwork(kodi_id, v.KODI_TYPE_ALBUM, self.kodicursor)
def remove_artist(self, kodi_id): def remove_artist(self, kodi_id):
''' '''
@ -135,9 +135,7 @@ class MusicMixin(object):
(kodi_id, )) (kodi_id, ))
self.kodicursor.execute('DELETE FROM discography WHERE idArtist = ?', self.kodicursor.execute('DELETE FROM discography WHERE idArtist = ?',
(kodi_id, )) (kodi_id, ))
self.artwork.delete_artwork(kodi_id, artwork.delete_artwork(kodi_id, v.KODI_TYPE_ARTIST, self.kodicursor)
v.KODI_TYPE_ARTIST,
self.kodicursor)
class Artist(MusicMixin, ItemBase): class Artist(MusicMixin, ItemBase):
@ -205,7 +203,7 @@ class Artist(MusicMixin, ItemBase):
utils.unix_date_to_kodi(self.last_sync), utils.unix_date_to_kodi(self.last_sync),
kodi_id)) kodi_id))
# Update artwork # Update artwork
self.artwork.modify_artwork(artworks, artwork.modify_artwork(artworks,
kodi_id, kodi_id,
v.KODI_TYPE_ARTIST, v.KODI_TYPE_ARTIST,
self.kodicursor) self.kodicursor)
@ -366,7 +364,7 @@ class Album(MusicMixin, ItemBase):
genres, genres,
v.KODI_TYPE_ALBUM) v.KODI_TYPE_ALBUM)
# Update artwork # Update artwork
self.artwork.modify_artwork(artworks, artwork.modify_artwork(artworks,
kodi_id, kodi_id,
v.KODI_TYPE_ALBUM, v.KODI_TYPE_ALBUM,
self.kodicursor) self.kodicursor)
@ -734,13 +732,13 @@ class Song(MusicMixin, ItemBase):
if genres: if genres:
self.kodi_db.add_music_genres(kodi_id, genres, v.KODI_TYPE_SONG) self.kodi_db.add_music_genres(kodi_id, genres, v.KODI_TYPE_SONG)
artworks = api.artwork() artworks = api.artwork()
self.artwork.modify_artwork(artworks, artwork.modify_artwork(artworks,
kodi_id, kodi_id,
v.KODI_TYPE_SONG, v.KODI_TYPE_SONG,
self.kodicursor) self.kodicursor)
if xml.get('parentKey') is None: if xml.get('parentKey') is None:
# Update album artwork # Update album artwork
self.artwork.modify_artwork(artworks, artwork.modify_artwork(artworks,
parent_id, parent_id,
v.KODI_TYPE_ALBUM, v.KODI_TYPE_ALBUM,
self.kodicursor) self.kodicursor)

View file

@ -5,7 +5,7 @@ from logging import getLogger
from .common import ItemBase, process_path from .common import ItemBase, process_path
from ..plex_api import API from ..plex_api import API
from .. import plex_functions as PF, state, variables as v from .. import artwork, plex_functions as PF, state, variables as v
LOG = getLogger('PLEX.tvshows') LOG = getLogger('PLEX.tvshows')
@ -77,9 +77,7 @@ class TvShowMixin(object):
self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_SHOW) self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_SHOW)
self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_SHOW) self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_SHOW)
self.kodi_db.modify_tags(kodi_id, v.KODI_TYPE_SHOW) self.kodi_db.modify_tags(kodi_id, v.KODI_TYPE_SHOW)
self.artwork.delete_artwork(kodi_id, artwork.delete_artwork(kodi_id, v.KODI_TYPE_SHOW, self.kodicursor)
v.KODI_TYPE_SHOW,
self.kodicursor)
self.kodicursor.execute("DELETE FROM tvshow WHERE idShow = ?", self.kodicursor.execute("DELETE FROM tvshow WHERE idShow = ?",
(kodi_id,)) (kodi_id,))
if v.KODIVERSION >= 17: if v.KODIVERSION >= 17:
@ -91,9 +89,7 @@ class TvShowMixin(object):
""" """
Remove a season, and only a season, not the show or episodes Remove a season, and only a season, not the show or episodes
""" """
self.artwork.delete_artwork(kodi_id, artwork.delete_artwork(kodi_id, v.KODI_TYPE_SEASON, self.kodicursor)
v.KODI_TYPE_SEASON,
self.kodicursor)
self.kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?", self.kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?",
(kodi_id,)) (kodi_id,))
LOG.debug("Removed season: %s", kodi_id) LOG.debug("Removed season: %s", kodi_id)
@ -104,9 +100,7 @@ class TvShowMixin(object):
""" """
self.kodi_db.modify_people(kodi_id, v.KODI_TYPE_EPISODE) self.kodi_db.modify_people(kodi_id, v.KODI_TYPE_EPISODE)
self.kodi_db.remove_file(file_id, plex_type=v.PLEX_TYPE_EPISODE) self.kodi_db.remove_file(file_id, plex_type=v.PLEX_TYPE_EPISODE)
self.artwork.delete_artwork(kodi_id, artwork.delete_artwork(kodi_id, v.KODI_TYPE_EPISODE, self.kodicursor)
v.KODI_TYPE_EPISODE,
self.kodicursor)
self.kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?", self.kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?",
(kodi_id,)) (kodi_id,))
if v.KODIVERSION >= 17: if v.KODIVERSION >= 17:
@ -252,7 +246,7 @@ class Show(ItemBase, TvShowMixin):
v.KODI_TYPE_SHOW, v.KODI_TYPE_SHOW,
api.people_list()) api.people_list())
self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_SHOW, genres) self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_SHOW, genres)
self.artwork.modify_artwork(api.artwork(), artwork.modify_artwork(api.artwork(),
kodi_id, kodi_id,
v.KODI_TYPE_SHOW, v.KODI_TYPE_SHOW,
self.kodicursor) self.kodicursor)
@ -301,7 +295,7 @@ class Season(ItemBase, TvShowMixin):
return return
parent_id = show['kodi_id'] parent_id = show['kodi_id']
kodi_id = self.kodi_db.add_season(parent_id, api.season_number()) kodi_id = self.kodi_db.add_season(parent_id, api.season_number())
self.artwork.modify_artwork(api.artwork(), artwork.modify_artwork(api.artwork(),
kodi_id, kodi_id,
v.KODI_TYPE_SEASON, v.KODI_TYPE_SEASON,
self.kodicursor) self.kodicursor)
@ -505,7 +499,7 @@ class Episode(ItemBase, TvShowMixin):
self.kodi_db.modify_people(kodi_id, self.kodi_db.modify_people(kodi_id,
v.KODI_TYPE_EPISODE, v.KODI_TYPE_EPISODE,
api.people_list()) api.people_list())
self.artwork.modify_artwork(api.artwork(), artwork.modify_artwork(api.artwork(),
kodi_id, kodi_id,
v.KODI_TYPE_EPISODE, v.KODI_TYPE_EPISODE,
self.kodicursor) self.kodicursor)

View file

@ -13,8 +13,6 @@ from . import artwork, utils, variables as v, state, path_ops
LOG = getLogger('PLEX.kodidb_functions') LOG = getLogger('PLEX.kodidb_functions')
###############################################################################
class GetKodiDB(object): class GetKodiDB(object):
""" """
@ -49,7 +47,6 @@ class KodiDBMethods(object):
""" """
def __init__(self, cursor): def __init__(self, cursor):
self.cursor = cursor self.cursor = cursor
self.artwork = artwork.Artwork()
def setup_path_table(self): def setup_path_table(self):
""" """
@ -460,7 +457,7 @@ class KodiDBMethods(object):
self.cursor.execute(query_actor_delete, (person[0],)) self.cursor.execute(query_actor_delete, (person[0],))
if kind == 'actor': if kind == 'actor':
# Delete any associated artwork # Delete any associated artwork
self.artwork.delete_artwork(person[0], 'actor', self.cursor) artwork.delete_artwork(person[0], 'actor', self.cursor)
# Save new people to Kodi DB by iterating over the remaining entries # Save new people to Kodi DB by iterating over the remaining entries
if kind == 'actor': if kind == 'actor':
query = 'INSERT INTO actor_link VALUES (?, ?, ?, ?, ?)' query = 'INSERT INTO actor_link VALUES (?, ?, ?, ?, ?)'
@ -506,7 +503,7 @@ class KodiDBMethods(object):
'VALUES (?, ?)', 'VALUES (?, ?)',
(actor_id, name)) (actor_id, name))
if art_url: if art_url:
self.artwork.modify_art(art_url, artwork.modify_art(art_url,
actor_id, actor_id,
'actor', 'actor',
'thumb', 'thumb',
@ -1199,6 +1196,11 @@ class KodiDBMethods(object):
''' '''
self.cursor.execute(query, (kodi_id, kodi_type)) self.cursor.execute(query, (kodi_id, kodi_type))
def art_urls(self, kodi_id, kodi_type):
self.cursor.execute('SELECT url FROM art WHERE media_id = ? AND media_type = ?',
(kodi_id, kodi_type))
return (x[0] for x in self.cursor)
def artwork_generator(self, kodi_type): def artwork_generator(self, kodi_type):
""" """
""" """
@ -1275,6 +1277,27 @@ def setup_kodi_default_entries():
utils.unix_timestamp()))) utils.unix_timestamp())))
def reset_cached_images():
LOG.info('Resetting cached artwork')
# Remove all existing textures first
path = path_ops.translate_path('special://thumbnails/')
if path_ops.exists(path):
path_ops.rmtree(path, ignore_errors=True)
paths = ('', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f',
'Video', 'plex')
for path in paths:
new_path = path_ops.translate_path('special://thumbnails/%s' % path)
path_ops.makedirs(path_ops.encode_path(new_path))
with GetKodiDB('texture') as kodi_db:
query = 'SELECT tbl_name FROM sqlite_master WHERE type=?'
kodi_db.cursor.execute(query, ('table', ))
rows = kodi_db.cursor.fetchall()
for row in rows:
if row[0] != 'version':
kodi_db.cursor.execute("DELETE FROM %s" % row[0])
def wipe_dbs(): def wipe_dbs():
""" """
Completely resets the Kodi databases 'video', 'texture' and 'music' (if Completely resets the Kodi databases 'video', 'texture' and 'music' (if
@ -1304,3 +1327,14 @@ def wipe_dbs():
xbmc.executebuiltin('UpdateLibrary(video)') xbmc.executebuiltin('UpdateLibrary(video)')
if utils.settings('enableMusic') == 'true': if utils.settings('enableMusic') == 'true':
xbmc.executebuiltin('UpdateLibrary(music)') xbmc.executebuiltin('UpdateLibrary(music)')
KODIDB_FROM_PLEXTYPE = {
v.PLEX_TYPE_MOVIE: GetKodiDB('video'),
v.PLEX_TYPE_SHOW: GetKodiDB('video'),
v.PLEX_TYPE_SEASON: GetKodiDB('video'),
v.PLEX_TYPE_EPISODE: GetKodiDB('video'),
v.PLEX_TYPE_ARTIST: GetKodiDB('music'),
v.PLEX_TYPE_ALBUM: GetKodiDB('music'),
v.PLEX_TYPE_SONG: GetKodiDB('music')
}

View file

@ -8,11 +8,14 @@ from .full_sync import PLAYLIST_SYNC_ENABLED
from .fanart import SYNC_FANART, FanartTask from .fanart import SYNC_FANART, FanartTask
from ..plex_api import API from ..plex_api import API
from ..plex_db import PlexDB from ..plex_db import PlexDB
from .. import kodidb_functions as kodidb
from .. import backgroundthread, playlists, plex_functions as PF, itemtypes from .. import backgroundthread, playlists, plex_functions as PF, itemtypes
from .. import utils, variables as v, state from .. import artwork, utils, variables as v, state
LOG = getLogger('PLEX.sync.websocket') LOG = getLogger('PLEX.sync.websocket')
CACHING_ENALBED = utils.settings('enableTextureCache') == "true"
WEBSOCKET_MESSAGES = [] WEBSOCKET_MESSAGES = []
# Dict to save info for Plex items currently being played somewhere # Dict to save info for Plex items currently being played somewhere
PLAYSTATE_SESSIONS = {} PLAYSTATE_SESSIONS = {}
@ -116,23 +119,25 @@ def process_websocket_messages():
def process_new_item_message(message): def process_new_item_message(message):
xml = PF.GetPlexMetadata(message['ratingKey']) plex_id = message['ratingKey']
xml = PF.GetPlexMetadata(plex_id)
try: try:
plex_type = xml[0].attrib['type'] plex_type = xml[0].attrib['type']
except (IndexError, KeyError, TypeError): except (IndexError, KeyError, TypeError):
LOG.error('Could not download metadata for %s', message['ratingKey']) LOG.error('Could not download metadata for %s', plex_id)
return False, False, False return False, False, False
LOG.debug("Processing new/updated PMS item: %s", message['ratingKey']) LOG.debug("Processing new/updated PMS item: %s", plex_id)
typus = itemtypes.ITEMTYPE_FROM_PLEXTYPE[plex_type](utils.unix_timestamp()) with itemtypes.ITEMTYPE_FROM_PLEXTYPE[plex_type](utils.unix_timestamp()) as typus:
typus.add_update(xml[0], typus.add_update(xml[0],
section_name=xml.get('librarySectionTitle'), section_name=xml.get('librarySectionTitle'),
section_id=xml.get('librarySectionID')) section_id=xml.get('librarySectionID'))
cache_artwork(plex_id, plex_type)
return True, plex_type in v.PLEX_VIDEOTYPES, plex_type in v.PLEX_AUDIOTYPES return True, plex_type in v.PLEX_VIDEOTYPES, plex_type in v.PLEX_AUDIOTYPES
def process_delete_message(message): def process_delete_message(message):
plex_type = message['type'] plex_type = message['type']
typus = itemtypes.ITEMTYPE_FROM_PLEXTYPE[plex_type](None) with itemtypes.ITEMTYPE_FROM_PLEXTYPE[plex_type](None) as typus:
typus.remove(message['ratingKey'], plex_type=plex_type) typus.remove(message['ratingKey'], plex_type=plex_type)
return True, plex_type in v.PLEX_VIDEOTYPES, plex_type in v.PLEX_AUDIOTYPES return True, plex_type in v.PLEX_VIDEOTYPES, plex_type in v.PLEX_AUDIOTYPES
@ -340,3 +345,21 @@ def process_playing(data):
session['file_id'], session['file_id'],
utils.unix_timestamp(), utils.unix_timestamp(),
v.PLEX_TYPE_FROM_KODI_TYPE[session['kodi_type']]) v.PLEX_TYPE_FROM_KODI_TYPE[session['kodi_type']])
def cache_artwork(plex_id, plex_type, kodi_id=None, kodi_type=None):
"""
Triggers caching of artwork (if so enabled in the PKC settings)
"""
if not CACHING_ENALBED:
return
if not kodi_id:
with PlexDB() as plexdb:
item = plexdb.item_by_id(plex_id, plex_type)
if not item:
LOG.error('Could not retrieve Plex db info for %s', plex_id)
return
kodi_id, kodi_type = item['kodi_id'], item['kodi_type']
with kodidb.KODIDB_FROM_PLEXTYPE[plex_type]() as kodi_db:
for url in kodi_db.art_urls(kodi_id, kodi_type):
artwork.cache_url(url)

View file

@ -115,7 +115,13 @@ class Sync(backgroundthread.KillableThread):
icon='{plex}', icon='{plex}',
sound=False) sound=False)
elif state.RUN_LIB_SCAN == 'textures': elif state.RUN_LIB_SCAN == 'textures':
artwork.Artwork().fullTextureCacheSync() LOG.info("Caching of images requested")
if not utils.yesno_dialog("Image Texture Cache", utils.lang(39250)):
return
# ask to reset all existing or not
if utils.yesno_dialog('Image Texture Cache', utils.lang(39251)):
kodidb.reset_cached_images()
self.start_image_cache_thread()
def on_library_scan_finished(self, successful): def on_library_scan_finished(self, successful):
""" """
@ -162,11 +168,14 @@ class Sync(backgroundthread.KillableThread):
sound=False) sound=False)
def start_image_cache_thread(self): def start_image_cache_thread(self):
if utils.settings('enableTextureCache') == "true": if not utils.settings('enableTextureCache') == "true":
LOG.info('Image caching has been deactivated')
return
if self.image_cache_thread and self.image_cache_thread.is_alive():
self.image_cache_thread.cancel()
self.image_cache_thread.join()
self.image_cache_thread = artwork.ImageCachingThread() self.image_cache_thread = artwork.ImageCachingThread()
self.image_cache_thread.start() self.image_cache_thread.start()
else:
LOG.info('Image caching has been deactivated')
def run(self): def run(self):
try: try:

View file

@ -536,9 +536,7 @@ def wipe_database():
LOG.info("Resetting all cached artwork.") LOG.info("Resetting all cached artwork.")
# Remove all cached artwork # Remove all cached artwork
path = path_ops.translate_path("special://thumbnails/") kodidb_functions.reset_cached_images()
if path_ops.exists(path):
path_ops.rmtree(path, ignore_errors=True)
# reset the install run flag # reset the install run flag
settings('SyncInstallRunDone', value="false") settings('SyncInstallRunDone', value="false")
LOG.info('Wiping done') LOG.info('Wiping done')