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,49 +67,6 @@ 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):
url = double_urlencode(utils.try_encode(url))
sleeptime = 0
while True:
try:
requests.head(
url="http://%s:%s/image/image://%s"
% (state.WEBSERVER_HOST,
state.WEBSERVER_PORT,
url),
auth=(state.WEBSERVER_USERNAME,
state.WEBSERVER_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 self.isCanceled():
# Kodi terminated
break
# Server thinks its a DOS attack, ('error 10053')
# Wait before trying again
if sleeptime > 5:
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',
2**sleeptime, double_urldecode(url))
xbmc.sleep((2**sleeptime) * 1000)
sleeptime += 1
continue
except Exception as err:
LOG.error('Unknown exception for url %s: %s'.
double_urldecode(url), err)
import traceback
LOG.error("Traceback:\n%s", traceback.format_exc())
break
# We did not even get a timeout
break
def run(self): def run(self):
LOG.info("---===### Starting ImageCachingThread ###===---") LOG.info("---===### Starting ImageCachingThread ###===---")
# Cache already synced artwork first # Cache already synced artwork first
@ -123,167 +80,113 @@ class ImageCachingThread(backgroundthread.KillableThread):
LOG.info("---===### Stopped ImageCachingThread ###===---") LOG.info("---===### Stopped ImageCachingThread ###===---")
return return
xbmc.sleep(1000) xbmc.sleep(1000)
self._cache_url(url) cache_url(url)
# Now wait for more stuff to cache - via the queue
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 ###===---") LOG.info("---===### Stopped ImageCachingThread ###===---")
class Artwork(): def cache_url(url):
enableTextureCache = utils.settings('enableTextureCache') == "true" url = double_urlencode(utils.try_encode(url))
if enableTextureCache: sleeptime = 0
queue = ARTWORK_QUEUE while True:
try:
requests.head(
url="http://%s:%s/image/image://%s"
% (state.WEBSERVER_HOST,
state.WEBSERVER_PORT,
url),
auth=(state.WEBSERVER_USERNAME,
state.WEBSERVER_PASSWORD),
timeout=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 state.STOP_PKC:
# Kodi terminated
break
# Server thinks its a DOS attack, ('error 10053')
# Wait before trying again
if sleeptime > 5:
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',
2**sleeptime, double_urldecode(url))
xbmc.sleep((2**sleeptime) * 1000)
sleeptime += 1
continue
except Exception as err:
LOG.error('Unknown exception for url %s: %s'.
double_urldecode(url), err)
import traceback
LOG.error("Traceback:\n%s", traceback.format_exc())
break
# We did not even get a timeout
break
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") def modify_artwork(artworks, kodi_id, kodi_type, cursor):
"""
Pass in an artworks dict (see PlexAPI) to set an items artwork.
"""
for kodi_art, url in artworks.iteritems():
modify_art(url, kodi_id, kodi_type, kodi_art, cursor)
# 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 def modify_art(url, kodi_id, kodi_type, kodi_art, cursor):
connection = utils.kodi_sql('texture') """
cursor = connection.cursor() Adds or modifies the artwork of kind kodi_art (e.g. 'poster') in the
query = 'SELECT tbl_name FROM sqlite_master WHERE type=?' Kodi art table for item kodi_id/kodi_type. Will also cache everything
cursor.execute(query, ('table', )) except actor portraits.
rows = cursor.fetchall() """
for row in rows: query = '''
tableName = row[0] SELECT url FROM art
if tableName != "version": WHERE media_id = ? AND media_type = ? AND type = ?
cursor.execute("DELETE FROM %s" % tableName) LIMIT 1
connection.commit() '''
connection.close() cursor.execute(query, (kodi_id, kodi_type, kodi_art,))
try:
# Cache all entries in video DB # Update the artwork
connection = utils.kodi_sql('video') old_url = cursor.fetchone()[0]
cursor = connection.cursor() except TypeError:
# dont include actors # Add the artwork
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.
"""
for kodi_art, url in artworks.iteritems():
self.modify_art(url, kodi_id, kodi_type, kodi_art, cursor)
def modify_art(self, url, kodi_id, kodi_type, kodi_art, cursor):
"""
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
except actor portraits.
"""
query = ''' query = '''
SELECT url FROM art INSERT INTO art(media_id, media_type, type, url)
WHERE media_id = ? AND media_type = ? AND type = ? VALUES (?, ?, ?, ?)
LIMIT 1
''' '''
cursor.execute(query, (kodi_id, kodi_type, kodi_art,)) cursor.execute(query, (kodi_id, kodi_type, kodi_art, url))
try: else:
# Update the artwork if url == old_url:
old_url = cursor.fetchone()[0] # Only cache artwork if it changed
except TypeError: return
# Add the artwork delete_cached_artwork(old_url)
query = ''' query = '''
INSERT INTO art(media_id, media_type, type, url) UPDATE art SET url = ?
VALUES (?, ?, ?, ?) WHERE media_id = ? AND media_type = ? AND type = ?
''' '''
cursor.execute(query, (kodi_id, kodi_type, kodi_art, url)) cursor.execute(query, (url, kodi_id, kodi_type, kodi_art))
else:
if url == old_url:
# Only cache artwork if it changed
return
self.delete_cached_artwork(old_url)
query = '''
UPDATE art SET url = ?
WHERE media_id = ? AND media_type = ? AND type = ?
'''
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 = ?'
cursor.execute(query, (kodiId, mediaType,))
for row in cursor.fetchall():
self.delete_cached_artwork(row[0])
@staticmethod def delete_artwork(kodiId, mediaType, cursor):
def delete_cached_artwork(url): cursor.execute('SELECT url FROM art WHERE media_id = ? AND media_type = ?',
""" (kodiId, mediaType, ))
Deleted the cached artwork with path url (if it exists) for row in cursor.fetchall():
""" delete_cached_artwork(row[0])
connection = utils.kodi_sql('texture')
cursor = connection.cursor()
def delete_cached_artwork(url):
"""
Deleted the cached artwork with path url (if it exists)
"""
from . import kodidb_functions as kodidb
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,10 +74,10 @@ 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)
def update_userdata(self, xml_element, plex_type): def update_userdata(self, xml_element, plex_type):
""" """

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,10 +165,10 @@ 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)
self.kodi_db.modify_streams(file_id, api.mediastreams(), runtime) self.kodi_db.modify_streams(file_id, api.mediastreams(), runtime)
self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_MOVIE, studios) self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_MOVIE, studios)
tags = [section_name] tags = [section_name]
@ -190,10 +190,10 @@ 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)
break break
self.kodi_db.modify_tags(kodi_id, v.KODI_TYPE_MOVIE, tags) self.kodi_db.modify_tags(kodi_id, v.KODI_TYPE_MOVIE, tags)
# Process playstate # Process playstate
@ -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,10 +203,10 @@ 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)
self.plexdb.add_artist(plex_id, self.plexdb.add_artist(plex_id,
api.checksum(), api.checksum(),
section_id, section_id,
@ -366,10 +364,10 @@ 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)
self.plexdb.add_album(plex_id, self.plexdb.add_album(plex_id,
api.checksum(), api.checksum(),
section_id, section_id,
@ -734,16 +732,16 @@ 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)
# Create the reference in plex table # Create the reference in plex table
self.plexdb.add_song(plex_id, self.plexdb.add_song(plex_id,
api.checksum(), api.checksum(),

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,10 +246,10 @@ 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)
# Process studios # Process studios
self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_SHOW, studios) self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_SHOW, studios)
# Process tags: view, PMS collection tags # Process tags: view, PMS collection tags
@ -301,10 +295,10 @@ 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)
self.plexdb.add_season(plex_id=plex_id, self.plexdb.add_season(plex_id=plex_id,
checksum=api.checksum(), checksum=api.checksum(),
section_id=section_id, section_id=section_id,
@ -505,10 +499,10 @@ 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)
streams = api.mediastreams() streams = api.mediastreams()
self.kodi_db.modify_streams(kodi_fileid, streams, api.runtime()) self.kodi_db.modify_streams(kodi_fileid, streams, api.runtime())
self.kodi_db.set_resume(kodi_fileid, self.kodi_db.set_resume(kodi_fileid,

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,11 +503,11 @@ 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',
self.cursor) self.cursor)
return actor_id return actor_id
def get_art(self, kodi_id, kodi_type): def get_art(self, kodi_id, kodi_type):
@ -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,24 +119,26 @@ 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":
self.image_cache_thread = artwork.ImageCachingThread()
self.image_cache_thread.start()
else:
LOG.info('Image caching has been deactivated') 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.start()
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')