Rewire image cache thread to service.py

This commit is contained in:
tomkat83 2017-01-20 17:21:51 +01:00
parent 3616c25a98
commit e818e23149
2 changed files with 43 additions and 36 deletions

View file

@ -1,17 +1,17 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################### ###############################################################################
import json
import logging import logging
from json import dumps, loads
import requests import requests
import os from os import path as os_path
from urllib import quote_plus, unquote from urllib import quote_plus, unquote
from threading import Thread from threading import Thread
import Queue from Queue import Queue, Empty
import xbmc from xbmc import executeJSONRPC, sleep, translatePath
import xbmcgui from xbmcgui import Dialog
import xbmcvfs from xbmcvfs import listdir, delete
from utils import window, settings, language as lang, kodiSQL, tryEncode, \ from utils import window, settings, language as lang, kodiSQL, tryEncode, \
tryDecode, IfExists, ThreadMethods, ThreadMethodsAdditionalStop tryDecode, IfExists, ThreadMethods, ThreadMethodsAdditionalStop
@ -25,6 +25,8 @@ log = logging.getLogger("PLEX."+__name__)
############################################################################### ###############################################################################
ARTWORK_QUEUE = Queue()
def setKodiWebServerDetails(): def setKodiWebServerDetails():
""" """
@ -41,8 +43,8 @@ def setKodiWebServerDetails():
"setting": "services.webserver" "setting": "services.webserver"
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_query)) result = executeJSONRPC(dumps(web_query))
result = json.loads(result) result = loads(result)
try: try:
xbmc_webserver_enabled = result['result']['value'] xbmc_webserver_enabled = result['result']['value']
except (KeyError, TypeError): except (KeyError, TypeError):
@ -58,7 +60,7 @@ def setKodiWebServerDetails():
"value": 8080 "value": 8080
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_port)) result = executeJSONRPC(dumps(web_port))
xbmc_port = 8080 xbmc_port = 8080
web_user = { web_user = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
@ -69,7 +71,7 @@ def setKodiWebServerDetails():
"value": True "value": True
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_user)) result = executeJSONRPC(dumps(web_user))
xbmc_username = "kodi" xbmc_username = "kodi"
# Webserver already enabled # Webserver already enabled
web_port = { web_port = {
@ -80,8 +82,8 @@ def setKodiWebServerDetails():
"setting": "services.webserverport" "setting": "services.webserverport"
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_port)) result = executeJSONRPC(dumps(web_port))
result = json.loads(result) result = loads(result)
try: try:
xbmc_port = result['result']['value'] xbmc_port = result['result']['value']
except (TypeError, KeyError): except (TypeError, KeyError):
@ -94,8 +96,8 @@ def setKodiWebServerDetails():
"setting": "services.webserverusername" "setting": "services.webserverusername"
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_user)) result = executeJSONRPC(dumps(web_user))
result = json.loads(result) result = loads(result)
try: try:
xbmc_username = result['result']['value'] xbmc_username = result['result']['value']
except TypeError: except TypeError:
@ -108,8 +110,8 @@ def setKodiWebServerDetails():
"setting": "services.webserverpassword" "setting": "services.webserverpassword"
} }
} }
result = xbmc.executeJSONRPC(json.dumps(web_pass)) result = executeJSONRPC(dumps(web_pass))
result = json.loads(result) result = loads(result)
try: try:
xbmc_password = result['result']['value'] xbmc_password = result['result']['value']
except TypeError: except TypeError:
@ -135,8 +137,8 @@ class Image_Cache_Thread(Thread):
# Hence let Kodi wait till download is successful # Hence let Kodi wait till download is successful
timeout = (35.1, 35.1) timeout = (35.1, 35.1)
def __init__(self, queue): def __init__(self):
self.queue = queue self.queue = ARTWORK_QUEUE
Thread.__init__(self) Thread.__init__(self)
def threadSuspended(self): def threadSuspended(self):
@ -158,11 +160,11 @@ class Image_Cache_Thread(Thread):
# Abort was requested while waiting. We should exit # Abort was requested while waiting. We should exit
log.info("---===### Stopped Image_Cache_Thread ###===---") log.info("---===### Stopped Image_Cache_Thread ###===---")
return return
xbmc.sleep(1000) sleep(1000)
try: try:
url = queue.get(block=False) url = queue.get(block=False)
except Queue.Empty: except Empty:
xbmc.sleep(1000) sleep(1000)
continue continue
sleep = 0 sleep = 0
while True: while True:
@ -187,7 +189,7 @@ class Image_Cache_Thread(Thread):
'over-loaded. Sleep %s seconds before trying ' 'over-loaded. Sleep %s seconds before trying '
'again to download %s' 'again to download %s'
% (2**sleep, double_urldecode(url))) % (2**sleep, double_urldecode(url)))
xbmc.sleep((2**sleep)*1000) sleep((2**sleep)*1000)
sleep += 1 sleep += 1
continue continue
except Exception as e: except Exception as e:
@ -201,45 +203,43 @@ class Image_Cache_Thread(Thread):
queue.task_done() queue.task_done()
log.debug('Cached art: %s' % double_urldecode(url)) log.debug('Cached art: %s' % double_urldecode(url))
# Sleep for a bit to reduce CPU strain # Sleep for a bit to reduce CPU strain
xbmc.sleep(sleep_between) sleep(sleep_between)
log.info("---===### Stopped Image_Cache_Thread ###===---") log.info("---===### Stopped Image_Cache_Thread ###===---")
class Artwork(): class Artwork():
enableTextureCache = settings('enableTextureCache') == "true" enableTextureCache = settings('enableTextureCache') == "true"
if enableTextureCache: if enableTextureCache:
queue = Queue.Queue() queue = ARTWORK_QUEUE
download_thread = Image_Cache_Thread(queue)
download_thread.start()
def fullTextureCacheSync(self): def fullTextureCacheSync(self):
""" """
This method will sync all Kodi artwork to textures13.db This method will sync all Kodi artwork to textures13.db
and cache them locally. This takes diskspace! and cache them locally. This takes diskspace!
""" """
if not xbmcgui.Dialog().yesno( if not Dialog().yesno(
"Image Texture Cache", lang(39250)): "Image Texture Cache", lang(39250)):
return return
log.info("Doing Image Cache Sync") log.info("Doing Image Cache Sync")
# ask to rest all existing or not # ask to rest all existing or not
if xbmcgui.Dialog().yesno( if Dialog().yesno(
"Image Texture Cache", lang(39251), ""): "Image Texture Cache", lang(39251), ""):
log.info("Resetting all cache data first") log.info("Resetting all cache data first")
# Remove all existing textures first # Remove all existing textures first
path = tryDecode(xbmc.translatePath("special://thumbnails/")) path = tryDecode(translatePath("special://thumbnails/"))
if IfExists(path): if IfExists(path):
allDirs, allFiles = xbmcvfs.listdir(path) allDirs, allFiles = listdir(path)
for dir in allDirs: for dir in allDirs:
allDirs, allFiles = xbmcvfs.listdir(path+dir) allDirs, allFiles = listdir(path+dir)
for file in allFiles: for file in allFiles:
if os.path.supports_unicode_filenames: if os_path.supports_unicode_filenames:
xbmcvfs.delete(os.path.join( delete(os_path.join(
path + tryDecode(dir), path + tryDecode(dir),
tryDecode(file))) tryDecode(file)))
else: else:
xbmcvfs.delete(os.path.join( delete(os_path.join(
tryEncode(path) + dir, tryEncode(path) + dir,
file)) file))
@ -439,10 +439,10 @@ class Artwork():
else: else:
# Delete thumbnail as well as the entry # Delete thumbnail as well as the entry
thumbnails = tryDecode( thumbnails = tryDecode(
xbmc.translatePath("special://thumbnails/%s" % cachedurl)) translatePath("special://thumbnails/%s" % cachedurl))
log.debug("Deleting cached thumbnail: %s" % thumbnails) log.debug("Deleting cached thumbnail: %s" % thumbnails)
try: try:
xbmcvfs.delete(thumbnails) delete(thumbnails)
except Exception as e: except Exception as e:
log.error('Could not delete cached artwork %s. Error: %s' log.error('Could not delete cached artwork %s. Error: %s'
% (thumbnails, e)) % (thumbnails, e))

View file

@ -46,6 +46,7 @@ import PlexAPI
from PlexCompanion import PlexCompanion from PlexCompanion import PlexCompanion
from monitor_kodi_play import Monitor_Kodi_Play from monitor_kodi_play import Monitor_Kodi_Play
from playback_starter import Playback_Starter from playback_starter import Playback_Starter
from artwork import Image_Cache_Thread
############################################################################### ###############################################################################
@ -76,6 +77,7 @@ class Service():
playqueue_running = False playqueue_running = False
kodimonitor_running = False kodimonitor_running = False
playback_starter_running = False playback_starter_running = False
image_cache_thread_running = False
def __init__(self): def __init__(self):
@ -153,6 +155,8 @@ class Service():
self.plexCompanion = PlexCompanion(self) self.plexCompanion = PlexCompanion(self)
self.playqueue = Playqueue(self) self.playqueue = Playqueue(self)
self.playback_starter = Playback_Starter(self) self.playback_starter = Playback_Starter(self)
if settings('enableTextureCache') == "true":
self.image_cache_thread = Image_Cache_Thread()
plx = PlexAPI.PlexAPI() plx = PlexAPI.PlexAPI()
@ -211,6 +215,9 @@ class Service():
if not self.playback_starter_running: if not self.playback_starter_running:
self.playback_starter_running = True self.playback_starter_running = True
self.playback_starter.start() self.playback_starter.start()
if not self.image_cache_thread_running:
self.image_cache_thread_running = True
self.image_cache_thread.start()
else: else:
if (self.user.currUser is None) and self.warn_auth: if (self.user.currUser is None) and self.warn_auth:
# Alert user is not authenticated and suppress future # Alert user is not authenticated and suppress future