2017-04-03 01:02:41 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from logging import getLogger
|
|
|
|
from threading import Thread
|
|
|
|
from Queue import Empty
|
|
|
|
|
|
|
|
from xbmc import sleep
|
|
|
|
|
2017-05-15 00:41:22 +10:00
|
|
|
from utils import ThreadMethodsAdditionalStop, ThreadMethods, window, \
|
|
|
|
ThreadMethodsAdditionalSuspend
|
2017-04-03 01:02:41 +10:00
|
|
|
import plexdb_functions as plexdb
|
|
|
|
import itemtypes
|
|
|
|
import variables as v
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
log = getLogger("PLEX."+__name__)
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2017-05-15 00:41:22 +10:00
|
|
|
@ThreadMethodsAdditionalSuspend('suspend_LibraryThread')
|
|
|
|
@ThreadMethodsAdditionalStop('plex_shouldStop')
|
|
|
|
@ThreadMethods
|
2017-04-03 01:02:41 +10:00
|
|
|
class Process_Fanart_Thread(Thread):
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Catch all exceptions and log them
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.__run()
|
|
|
|
except Exception as e:
|
|
|
|
log.error('Exception %s' % e)
|
|
|
|
import traceback
|
|
|
|
log.error("Traceback:\n%s" % traceback.format_exc())
|
|
|
|
|
|
|
|
def __run(self):
|
|
|
|
"""
|
|
|
|
Do the work
|
|
|
|
"""
|
|
|
|
log.debug("---===### Starting FanartSync ###===---")
|
2017-05-15 00:41:22 +10:00
|
|
|
threadStopped = self.threadStopped
|
|
|
|
threadSuspended = self.threadSuspended
|
2017-04-03 01:02:41 +10:00
|
|
|
queue = self.queue
|
2017-05-15 00:41:22 +10:00
|
|
|
while not threadStopped():
|
2017-04-03 01:02:41 +10:00
|
|
|
# In the event the server goes offline
|
2017-05-15 00:41:22 +10:00
|
|
|
while threadSuspended() or window('plex_dbScan'):
|
2017-04-03 01:02:41 +10:00
|
|
|
# Set in service.py
|
2017-05-15 00:41:22 +10:00
|
|
|
if threadStopped():
|
2017-04-03 01:02:41 +10:00
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
log.info("---===### Stopped FanartSync ###===---")
|
|
|
|
return
|
|
|
|
sleep(1000)
|
|
|
|
# grabs Plex item from queue
|
|
|
|
try:
|
|
|
|
item = queue.get(block=False)
|
|
|
|
except Empty:
|
|
|
|
sleep(200)
|
|
|
|
continue
|
|
|
|
|
|
|
|
log.debug('Get additional fanart for Plex id %s' % item['plex_id'])
|
|
|
|
with getattr(itemtypes,
|
|
|
|
v.ITEMTYPE_FROM_PLEXTYPE[item['plex_type']])() as cls:
|
|
|
|
result = cls.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()
|
|
|
|
log.debug("---===### Stopped FanartSync ###===---")
|