Fix rare library sync crash

- Fixes #513
This commit is contained in:
croneter 2018-07-27 13:38:41 +02:00
parent 633f23c02e
commit f0db5a82f8
3 changed files with 84 additions and 92 deletions

View file

@ -63,76 +63,14 @@ class Items(object):
self.kodiconn.close()
return self
@utils.catch_exceptions(warnuser=True)
def getfanart(self, plex_id, refresh=False):
def set_fanart(self, artworks, kodi_id, kodi_type):
"""
Tries to get additional fanart for movies (+sets) and TV shows.
Returns True if successful, False otherwise
Writes artworks [dict containing only set artworks] to the Kodi art DB
"""
with plexdb.Get_Plex_DB() as plex_db:
db_item = plex_db.getItem_byId(plex_id)
try:
kodi_id = db_item[0]
kodi_type = db_item[4]
except TypeError:
LOG.error('Could not get Kodi id for plex id %s, abort getfanart',
plex_id)
return False
if refresh is True:
# Leave the Plex art untouched
allartworks = None
else:
with kodidb.GetKodiDB('video') as kodi_db:
allartworks = kodi_db.get_art(kodi_id, kodi_type)
# Check if we even need to get additional art
needsupdate = False
for key in v.ALL_KODI_ARTWORK:
if key not in allartworks:
needsupdate = True
break
if needsupdate is False:
LOG.debug('Already got all fanart for Plex id %s', plex_id)
return True
xml = PF.GetPlexMetadata(plex_id)
if xml is None:
# Did not receive a valid XML - skip that item for now
LOG.error("Could not get metadata for %s. Skipping that item "
"for now", plex_id)
return False
elif xml == 401:
LOG.error('HTTP 401 returned by PMS. Too much strain? '
'Cancelling sync for now')
# Kill remaining items in queue (for main thread to cont.)
return False
api = API(xml[0])
if allartworks is None:
allartworks = api.artwork()
self.artwork.modify_artwork(api.fanart_artwork(allartworks),
self.artwork.modify_artwork(artworks,
kodi_id,
kodi_type,
self.kodicursor)
# Also get artwork for collections/movie sets
if kodi_type == v.KODI_TYPE_MOVIE:
for _, setname in api.collection_list():
LOG.debug('Getting artwork for movie set %s', setname)
setid = self.kodi_db.create_collection(setname)
external_set_artwork = api.set_artwork()
if (external_set_artwork and
utils.settings('PreferKodiCollectionArt') == 'true'):
# Need to make sure we are not overwriting existing Plex
# collection artwork
plex_artwork = api.artwork(kodi_id=setid,
kodi_type=v.KODI_TYPE_SET)
for art in plex_artwork:
if art in external_set_artwork:
del external_set_artwork[art]
self.artwork.modify_artwork(external_set_artwork,
setid,
v.KODI_TYPE_SET,
self.kodicursor)
return True
def updateUserdata(self, xml):
"""

View file

@ -5,12 +5,9 @@ from threading import Thread
from Queue import Empty
import xbmc
from .. import utils
from .. import plexdb_functions as plexdb
from .. import itemtypes
from .. import artwork
from .. import variables as v
from .. import state
from ..plex_api import API
from .. import utils, plexdb_functions as plexdb, kodidb_functions as kodidb
from .. import itemtypes, artwork, plex_functions as PF, variables as v, state
###############################################################################
@ -46,24 +43,22 @@ class ThreadedProcessFanart(Thread):
Do the work
"""
LOG.debug("---===### Starting FanartSync ###===---")
stopped = self.stopped
suspended = self.suspended
queue = self.queue
while not stopped():
while not self.stopped():
# In the event the server goes offline
while suspended():
while self.suspended():
# Set in service.py
if stopped():
if self.stopped():
# Abort was requested while waiting. We should exit
LOG.info("---===### Stopped FanartSync ###===---")
LOG.debug("---===### Stopped FanartSync ###===---")
return
xbmc.sleep(1000)
# grabs Plex item from queue
try:
item = queue.get(block=False)
item = self.queue.get(block=False)
except Empty:
xbmc.sleep(200)
continue
self.queue.task_done()
if isinstance(item, artwork.ArtworkSyncMessage):
if state.IMAGE_SYNC_NOTIFICATIONS:
utils.dialog('notification',
@ -71,17 +66,76 @@ class ThreadedProcessFanart(Thread):
message=item.message,
icon='{plex}',
sound=False)
queue.task_done()
continue
LOG.debug('Get additional fanart for Plex id %s', item['plex_id'])
with getattr(itemtypes,
v.ITEMTYPE_FROM_PLEXTYPE[item['plex_type']])() as item_type:
result = item_type.getfanart(item['plex_id'],
refresh=item['refresh'])
if result is True:
LOG.debug('Done getting fanart for Plex id %s', item['plex_id'])
with plexdb.Get_Plex_DB() as plex_db:
plex_db.set_fanart_synched(item['plex_id'])
queue.task_done()
_process(item)
LOG.debug("---===### Stopped FanartSync ###===---")
def _process(item):
done = False
try:
artworks = None
with plexdb.Get_Plex_DB() as plex_db:
db_item = plex_db.getItem_byId(item['plex_id'])
try:
kodi_id = db_item[0]
kodi_type = db_item[4]
except TypeError:
LOG.error('Could not get Kodi id for plex id %s, abort getfanart',
item['plex_id'])
return
if item['refresh'] is False:
with kodidb.GetKodiDB('video') as kodi_db:
artworks = kodi_db.get_art(kodi_id, kodi_type)
# Check if we even need to get additional art
for key in v.ALL_KODI_ARTWORK:
if key not in artworks:
break
else:
LOG.debug('Already got all fanart for Plex id %s',
item['plex_id'])
done = True
return
xml = PF.GetPlexMetadata(item['plex_id'])
if xml is None:
LOG.error('Could not get metadata for %s. Skipping that item '
'for now', item['plex_id'])
return
elif xml == 401:
LOG.error('HTTP 401 returned by PMS. Too much strain? '
'Cancelling sync for now')
return
api = API(xml[0])
if artworks is None:
artworks = api.artwork()
# Get additional missing artwork from fanart artwork sites
artworks = api.fanart_artwork(artworks)
with getattr(itemtypes,
v.ITEMTYPE_FROM_PLEXTYPE[item['plex_type']])() as itm:
itm.set_fanart(artworks, kodi_id, kodi_type)
# Additional fanart for sets/collections
if api.plex_type() == v.PLEX_TYPE_MOVIE:
for _, setname in api.collection_list():
LOG.debug('Getting artwork for movie set %s', setname)
with kodidb.GetKodiDB('video') as kodi_db:
setid = kodi_db.create_collection(setname)
external_set_artwork = api.set_artwork()
if (external_set_artwork and
utils.settings('PreferKodiCollectionArt') == 'false'):
kodi_artwork = api.artwork(kodi_id=setid,
kodi_type=v.KODI_TYPE_SET)
for art in kodi_artwork:
if art in external_set_artwork:
del external_set_artwork[art]
with itemtypes.Movies() as movie_db:
movie_db.artwork.modify_artwork(external_set_artwork,
setid,
v.KODI_TYPE_SET,
movie_db.kodicursor)
done = True
finally:
if done is True:
LOG.debug('Done getting fanart for Plex id %s', item['plex_id'])
with plexdb.Get_Plex_DB() as plex_db:
plex_db.set_fanart_synched(item['plex_id'])

View file

@ -131,9 +131,9 @@
<category label="30544"><!-- artwork -->
<setting id="enableTextureCache" label="30512" type="bool" default="true" /> <!-- Cache all artwork for a smooth Kodi experience -->
<setting id="PreferKodiCollectionArt" label="30543" type="bool" default="true" /><!-- Prefer Kodi artwork for collections -->
<setting id="FanartTV" label="30539" type="bool" default="false" /><!-- Download additional art from FanArtTV -->
<setting label="39222" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect/?mode=fanart)" option="close" visible="eq(-1,true)" subsetting="true" /> <!-- Look for missing fanart on FanartTV now -->
<setting id="PreferKodiCollectionArt" label="30543" type="bool" default="true" visible="eq(-1,true)" subsetting="true" /><!-- Prefer Kodi artwork for collections -->
<setting label="39222" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect/?mode=fanart)" option="close" visible="eq(-2,true)" subsetting="true" /> <!-- Look for missing fanart on FanartTV now -->
<setting id="imageSyncNotifications" label="30008" type="bool" default="true" /><!-- Enable notifications for image caching -->
<setting id="imageSyncDuringPlayback" label="30009" type="bool" default="true" /><!-- Enable image caching during Kodi playback (restart Kodi!) -->
<setting label="39020" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect/?mode=texturecache)" option="close" /> <!-- Cache all images to Kodi texture cache now -->