Reduce boilerplate

This commit is contained in:
tomkat83 2016-07-22 16:55:57 +02:00
parent bc7be04fc2
commit 3407d47f9c

View file

@ -1299,6 +1299,7 @@ def getVideoFiles(plexId, params):
xbmcplugin.endOfDirectory(int(sys.argv[1]))
@utils.CatchExceptions(warnuser=False)
def getExtraFanArt(plexid, plexPath):
"""
Get extrafanart for listitem
@ -1314,51 +1315,45 @@ def getExtraFanArt(plexid, plexPath):
utils.logMsg(title, 'Could not get a plexid, aborting', -1)
return xbmcplugin.endOfDirectory(int(sys.argv[1]))
try:
# We need to store the images locally for this to work
# because of the caching system in xbmc
fanartDir = utils.tryDecode(xbmc.translatePath(
"special://thumbnails/plex/%s/" % plexid))
if not xbmcvfs.exists(fanartDir):
# Download the images to the cache directory
xbmcvfs.mkdirs(utils.tryEncode(fanartDir))
xml = PlexFunctions.GetPlexMetadata(plexid)
if xml is None:
utils.logMsg('Could not download metadata for %s' % plexid, -1)
return xbmcplugin.endOfDirectory(int(sys.argv[1]))
# We need to store the images locally for this to work
# because of the caching system in xbmc
fanartDir = utils.tryDecode(xbmc.translatePath(
"special://thumbnails/plex/%s/" % plexid))
if not xbmcvfs.exists(fanartDir):
# Download the images to the cache directory
xbmcvfs.mkdirs(utils.tryEncode(fanartDir))
xml = PlexFunctions.GetPlexMetadata(plexid)
if xml is None:
utils.logMsg('Could not download metadata for %s' % plexid, -1)
return xbmcplugin.endOfDirectory(int(sys.argv[1]))
API = PlexAPI.API(xml[0])
backdrops = API.getAllArtwork()['Backdrop']
for count, backdrop in enumerate(backdrops):
# Same ordering as in artwork
if os.path.supports_unicode_filenames:
fanartFile = os.path.join(fanartDir,
"fanart%.3d.jpg" % count)
else:
fanartFile = os.path.join(
utils.tryEncode(fanartDir),
utils.tryEncode("fanart%.3d.jpg" % count))
li = xbmcgui.ListItem("%.3d" % count, path=fanartFile)
xbmcplugin.addDirectoryItem(
handle=int(sys.argv[1]),
url=fanartFile,
listitem=li)
xbmcvfs.copy(backdrop, fanartFile)
else:
utils.logMsg(title, "Found cached backdrop.", 1)
# Use existing cached images
dirs, files = xbmcvfs.listdir(fanartDir)
for file in files:
fanartFile = os.path.join(fanartDir, utils.tryDecode(file))
li = xbmcgui.ListItem(file, path=fanartFile)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
url=fanartFile,
listitem=li)
except Exception as e:
utils.logMsg(title, "Error getting extrafanart: %s" % e, -1)
import traceback
utils.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
API = PlexAPI.API(xml[0])
backdrops = API.getAllArtwork()['Backdrop']
for count, backdrop in enumerate(backdrops):
# Same ordering as in artwork
if os.path.supports_unicode_filenames:
fanartFile = os.path.join(fanartDir,
"fanart%.3d.jpg" % count)
else:
fanartFile = os.path.join(
utils.tryEncode(fanartDir),
utils.tryEncode("fanart%.3d.jpg" % count))
li = xbmcgui.ListItem("%.3d" % count, path=fanartFile)
xbmcplugin.addDirectoryItem(
handle=int(sys.argv[1]),
url=fanartFile,
listitem=li)
xbmcvfs.copy(backdrop, fanartFile)
else:
utils.logMsg(title, "Found cached backdrop.", 1)
# Use existing cached images
dirs, files = xbmcvfs.listdir(fanartDir)
for file in files:
fanartFile = os.path.join(fanartDir, utils.tryDecode(file))
li = xbmcgui.ListItem(file, path=fanartFile)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
url=fanartFile,
listitem=li)
xbmcplugin.endOfDirectory(int(sys.argv[1]))