2017-04-03 01:02:41 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from logging import getLogger
|
|
|
|
from threading import Thread
|
|
|
|
from Queue import Empty
|
2018-05-30 15:53:30 +10:00
|
|
|
import xbmc
|
2017-04-03 01:02:41 +10:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
from .. import utils
|
|
|
|
from .. import plexdb_functions as plexdb
|
|
|
|
from .. import itemtypes
|
|
|
|
from .. import artwork
|
|
|
|
from .. import variables as v
|
|
|
|
from .. import state
|
2017-04-03 01:02:41 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2017-04-03 01:02:41 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
@utils.thread_methods(add_suspends=['SUSPEND_LIBRARY_THREAD',
|
|
|
|
'DB_SCAN',
|
|
|
|
'STOP_SYNC',
|
|
|
|
'SUSPEND_SYNC'])
|
2018-04-18 04:18:25 +10:00
|
|
|
class ThreadedProcessFanart(Thread):
|
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
|
|
|
|
Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Do the work
|
|
|
|
"""
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.debug("---===### Starting FanartSync ###===---")
|
2018-02-12 00:57:39 +11:00
|
|
|
stopped = self.stopped
|
|
|
|
suspended = self.suspended
|
2017-04-03 01:02:41 +10:00
|
|
|
queue = self.queue
|
2018-05-16 04:46:16 +10:00
|
|
|
counter = 0
|
|
|
|
set_zero = False
|
2018-02-12 00:57:39 +11:00
|
|
|
while not stopped():
|
2017-04-03 01:02:41 +10:00
|
|
|
# In the event the server goes offline
|
2018-02-12 00:57:39 +11:00
|
|
|
while suspended():
|
2017-04-03 01:02:41 +10:00
|
|
|
# Set in service.py
|
2018-02-12 00:57:39 +11:00
|
|
|
if stopped():
|
2017-04-03 01:02:41 +10:00
|
|
|
# Abort was requested while waiting. We should exit
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.info("---===### Stopped FanartSync ###===---")
|
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:
|
|
|
|
item = queue.get(block=False)
|
|
|
|
except Empty:
|
2018-05-30 15:53:30 +10:00
|
|
|
if not set_zero and not xbmc.getCondVisibility(
|
2018-05-30 16:29:33 +10:00
|
|
|
'Window.IsVisible(DialogAddonSettings.xml)'):
|
2018-05-18 23:59:26 +10:00
|
|
|
# Avoid saving '0' all the time
|
|
|
|
set_zero = True
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.settings('fanarttv_lookups', value='0')
|
2018-05-30 15:53:30 +10:00
|
|
|
xbmc.sleep(200)
|
2017-04-03 01:02:41 +10:00
|
|
|
continue
|
2018-05-18 23:59:26 +10:00
|
|
|
set_zero = False
|
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
|
|
|
queue.task_done()
|
|
|
|
continue
|
|
|
|
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.debug('Get additional fanart for Plex id %s', item['plex_id'])
|
2017-04-03 01:02:41 +10:00
|
|
|
with getattr(itemtypes,
|
2018-04-18 04:18:25 +10:00
|
|
|
v.ITEMTYPE_FROM_PLEXTYPE[item['plex_type']])() as item_type:
|
|
|
|
result = item_type.getfanart(item['plex_id'],
|
|
|
|
refresh=item['refresh'])
|
2017-04-03 01:02:41 +10:00
|
|
|
if result is True:
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.debug('Done getting fanart for Plex id %s', item['plex_id'])
|
2017-04-03 01:02:41 +10:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
plex_db.set_fanart_synched(item['plex_id'])
|
2018-05-18 23:59:26 +10:00
|
|
|
# Update the caching state in the PKC settings. Avoid saving '0'
|
|
|
|
counter += 1
|
2018-05-30 15:53:30 +10:00
|
|
|
if (counter > 20 and not xbmc.getCondVisibility(
|
|
|
|
'Window.IsVisible(DialogAddonSettings.xml)')):
|
2018-05-18 23:59:26 +10:00
|
|
|
counter = 0
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.settings('fanarttv_lookups', value=str(queue.qsize()))
|
2017-04-03 01:02:41 +10:00
|
|
|
queue.task_done()
|
2018-04-18 04:18:25 +10:00
|
|
|
LOG.debug("---===### Stopped FanartSync ###===---")
|