2017-04-03 01:02:41 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-04-03 01:02:41 +10:00
|
|
|
from logging import getLogger
|
2018-05-30 15:53:30 +10:00
|
|
|
import xbmc
|
2017-04-03 01:02:41 +10:00
|
|
|
|
2018-07-27 21:38:41 +10:00
|
|
|
from ..plex_api import API
|
2018-10-24 19:57:52 +11:00
|
|
|
from ..plex_db import PlexDB
|
|
|
|
from .. import backgroundthread
|
|
|
|
from ..backgroundthread.Queue import Empty
|
|
|
|
from .. import utils, kodidb_functions as kodidb
|
2018-07-27 21:38:41 +10:00
|
|
|
from .. import itemtypes, artwork, plex_functions as PF, variables as v, state
|
2017-04-03 01:02:41 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2018-10-24 19:57:52 +11:00
|
|
|
LOG = getLogger('PLEX.library_sync.fanart')
|
2017-04-03 01:02:41 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2018-10-24 19:57:52 +11:00
|
|
|
class ThreadedProcessFanart(backgroundthread.KillableThread):
|
2017-04-03 01:02:41 +10:00
|
|
|
"""
|
|
|
|
Threaded download of additional fanart in the background
|
|
|
|
|
|
|
|
Input:
|
|
|
|
queue Queue.Queue() object that you will need to fill with
|
|
|
|
dicts of the following form:
|
|
|
|
{
|
|
|
|
'plex_id': the Plex id as a string
|
|
|
|
'plex_type': the Plex media type, e.g. 'movie'
|
|
|
|
'refresh': True/False if True, will overwrite any 3rd party
|
|
|
|
fanart. If False, will only get missing
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
def __init__(self, queue):
|
|
|
|
self.queue = queue
|
2018-10-24 19:57:52 +11:00
|
|
|
super(ThreadedProcessFanart, self).__init__()
|
|
|
|
|
|
|
|
def isCanceled(self):
|
|
|
|
return xbmc.abortRequested or state.STOP_PKC
|
|
|
|
|
|
|
|
def isSuspended(self):
|
|
|
|
return (state.SUSPEND_LIBRARY_THREAD or
|
|
|
|
state.DB_SCAN or
|
|
|
|
state.STOP_SYNC or
|
|
|
|
state.SUSPEND_SYNC)
|
2017-04-03 01:02:41 +10:00
|
|
|
|
|
|
|
def run(self):
|
2018-10-24 19:57:52 +11:00
|
|
|
LOG.info('---===### Starting FanartSync ###===---')
|
|
|
|
try:
|
|
|
|
self._run()
|
|
|
|
except:
|
|
|
|
utils.ERROR(txt='FanartSync crashed', notify=True)
|
|
|
|
raise
|
|
|
|
LOG.info('---===### Stopping FanartSync ###===---')
|
|
|
|
|
|
|
|
def _run(self):
|
2017-04-03 01:02:41 +10:00
|
|
|
"""
|
|
|
|
Do the work
|
|
|
|
"""
|
2018-10-24 19:57:52 +11:00
|
|
|
# First run through our already synced items in the Plex DB
|
|
|
|
for plex_type in (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW):
|
|
|
|
with PlexDB() as plexdb:
|
|
|
|
for plex_id in plexdb.fanart(plex_type):
|
|
|
|
if self.isCanceled():
|
|
|
|
break
|
|
|
|
while self.isSuspended():
|
|
|
|
if self.isCanceled():
|
|
|
|
break
|
|
|
|
xbmc.sleep(1000)
|
|
|
|
process_item(plexdb, {'plex_id': plex_id,
|
|
|
|
'plex_type': plex_type,
|
|
|
|
'refresh': False})
|
|
|
|
|
|
|
|
# Then keep checking the queue for new items
|
|
|
|
while not self.isCanceled():
|
2017-04-03 01:02:41 +10:00
|
|
|
# In the event the server goes offline
|
2018-10-24 19:57:52 +11:00
|
|
|
while self.isSuspended():
|
2017-04-03 01:02:41 +10:00
|
|
|
# Set in service.py
|
2018-10-24 19:57:52 +11:00
|
|
|
if self.isCanceled():
|
2017-04-03 01:02:41 +10:00
|
|
|
return
|
2018-05-30 15:53:30 +10:00
|
|
|
xbmc.sleep(1000)
|
2017-04-03 01:02:41 +10:00
|
|
|
# grabs Plex item from queue
|
|
|
|
try:
|
2018-07-27 21:38:41 +10:00
|
|
|
item = self.queue.get(block=False)
|
2017-04-03 01:02:41 +10:00
|
|
|
except Empty:
|
2018-10-24 19:57:52 +11:00
|
|
|
xbmc.sleep(1000)
|
2017-04-03 01:02:41 +10:00
|
|
|
continue
|
2018-07-27 21:38:41 +10:00
|
|
|
self.queue.task_done()
|
2018-06-22 03:24:37 +10:00
|
|
|
if isinstance(item, artwork.ArtworkSyncMessage):
|
2018-05-16 04:46:16 +10:00
|
|
|
if state.IMAGE_SYNC_NOTIFICATIONS:
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.dialog('notification',
|
|
|
|
heading=utils.lang(29999),
|
|
|
|
message=item.message,
|
|
|
|
icon='{plex}',
|
|
|
|
sound=False)
|
2018-05-13 23:22:03 +10:00
|
|
|
continue
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.debug('Get additional fanart for Plex id %s', item['plex_id'])
|
2018-10-24 19:57:52 +11:00
|
|
|
with PlexDB() as plexdb:
|
|
|
|
process_item(plexdb, item)
|
2018-07-27 21:38:41 +10:00
|
|
|
|
|
|
|
|
2018-10-24 19:57:52 +11:00
|
|
|
def process_item(plexdb, item):
|
2018-07-27 21:38:41 +10:00
|
|
|
done = False
|
|
|
|
try:
|
|
|
|
artworks = None
|
2018-10-24 19:57:52 +11:00
|
|
|
db_item = plexdb.item_by_id(item['plex_id'], item['plex_type'])
|
|
|
|
if not db_item:
|
2018-07-27 21:38:41 +10:00
|
|
|
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:
|
2018-10-24 19:57:52 +11:00
|
|
|
artworks = kodi_db.get_art(db_item['kodi_id'],
|
|
|
|
db_item['kodi_type'])
|
2018-07-27 21:38:41 +10:00
|
|
|
# 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)
|
2018-10-24 19:57:52 +11:00
|
|
|
with itemtypes.ITEMTYPE_FROM_PLEXTYPE[item['plex_type']] as context:
|
|
|
|
context.set_fanart(artworks,
|
|
|
|
db_item['kodi_id'],
|
|
|
|
db_item['kodi_type'])
|
2018-07-27 21:38:41 +10:00
|
|
|
# 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]
|
2018-10-24 19:57:52 +11:00
|
|
|
with itemtypes.Movie() as movie:
|
|
|
|
movie.artwork.modify_artwork(external_set_artwork,
|
|
|
|
setid,
|
|
|
|
v.KODI_TYPE_SET,
|
|
|
|
movie.kodicursor)
|
2018-07-27 21:38:41 +10:00
|
|
|
done = True
|
|
|
|
finally:
|
|
|
|
if done is True:
|
|
|
|
LOG.debug('Done getting fanart for Plex id %s', item['plex_id'])
|
2018-10-24 19:57:52 +11:00
|
|
|
plexdb.set_fanart_synced(item['plex_id'], item['plex_type'])
|