2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-12-09 05:43:06 +11:00
|
|
|
from logging import getLogger
|
2016-09-17 22:02:50 +10:00
|
|
|
from urllib import quote_plus, unquote
|
2017-12-10 00:35:08 +11:00
|
|
|
import requests
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-11-09 07:22:16 +11:00
|
|
|
from .kodi_db import KodiVideoDB, KodiMusicDB, KodiTextureDB
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import app, backgroundthread, utils
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG = getLogger('PLEX.artwork')
|
2016-03-24 02:57:49 +11:00
|
|
|
|
2018-03-02 17:36:45 +11:00
|
|
|
# Disable annoying requests warnings
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
2018-11-06 04:00:01 +11:00
|
|
|
|
|
|
|
# Potentially issues with limited number of threads Hence let Kodi wait till
|
|
|
|
# download is successful
|
|
|
|
TIMEOUT = (35.1, 35.1)
|
2018-12-31 07:30:08 +11:00
|
|
|
BATCH_SIZE = 500
|
2018-11-06 04:00:01 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-17 22:02:50 +10:00
|
|
|
def double_urlencode(text):
|
|
|
|
return quote_plus(quote_plus(text))
|
|
|
|
|
|
|
|
|
|
|
|
def double_urldecode(text):
|
|
|
|
return unquote(unquote(text))
|
|
|
|
|
|
|
|
|
2018-11-06 01:23:51 +11:00
|
|
|
class ImageCachingThread(backgroundthread.KillableThread):
|
2019-01-31 06:36:52 +11:00
|
|
|
def __init__(self):
|
|
|
|
super(ImageCachingThread, self).__init__()
|
2019-02-08 22:04:24 +11:00
|
|
|
self.suspend_points = [(self, '_suspended')]
|
2019-01-31 06:36:52 +11:00
|
|
|
if not utils.settings('imageSyncDuringPlayback') == 'true':
|
2019-02-08 22:04:24 +11:00
|
|
|
self.suspend_points.append((app.APP, 'is_playing_video'))
|
2019-01-31 06:36:52 +11:00
|
|
|
|
2018-11-06 01:23:51 +11:00
|
|
|
def isSuspended(self):
|
2019-02-08 22:04:24 +11:00
|
|
|
return any(getattr(obj, txt) for obj, txt in self.suspend_points)
|
2018-11-06 01:23:51 +11:00
|
|
|
|
2018-12-31 07:30:08 +11:00
|
|
|
def _url_generator(self, kind, kodi_type):
|
|
|
|
"""
|
|
|
|
Main goal is to close DB connection between calls
|
|
|
|
"""
|
|
|
|
offset = 0
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
batch = []
|
|
|
|
with kind(texture_db=True) as kodidb:
|
2019-01-23 20:00:49 +11:00
|
|
|
texture_db = KodiTextureDB(kodiconn=kodidb.kodiconn,
|
|
|
|
artconn=kodidb.artconn,
|
|
|
|
lock=False)
|
2018-12-31 07:30:08 +11:00
|
|
|
for i, url in enumerate(kodidb.artwork_generator(kodi_type,
|
|
|
|
BATCH_SIZE,
|
|
|
|
offset)):
|
|
|
|
if texture_db.url_not_yet_cached(url):
|
|
|
|
batch.append(url)
|
|
|
|
if len(batch) == BATCH_SIZE:
|
|
|
|
break
|
|
|
|
offset += i
|
|
|
|
for url in batch:
|
|
|
|
yield url
|
|
|
|
if i + 1 < BATCH_SIZE:
|
|
|
|
break
|
2018-11-06 01:23:51 +11:00
|
|
|
|
2016-09-17 19:24:12 +10:00
|
|
|
def run(self):
|
2018-11-06 00:18:52 +11:00
|
|
|
LOG.info("---===### Starting ImageCachingThread ###===---")
|
2019-01-31 06:36:52 +11:00
|
|
|
app.APP.register_caching_thread(self)
|
2018-12-31 07:30:08 +11:00
|
|
|
try:
|
|
|
|
self._run()
|
2019-02-03 06:22:06 +11:00
|
|
|
except Exception:
|
2018-12-31 07:30:08 +11:00
|
|
|
utils.ERROR()
|
|
|
|
finally:
|
2019-01-31 06:36:52 +11:00
|
|
|
app.APP.deregister_caching_thread(self)
|
2018-12-31 07:30:08 +11:00
|
|
|
LOG.info("---===### Stopped ImageCachingThread ###===---")
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
kinds = [KodiVideoDB]
|
|
|
|
if app.SYNC.enable_music:
|
|
|
|
kinds.append(KodiMusicDB)
|
|
|
|
for kind in kinds:
|
|
|
|
for kodi_type in ('poster', 'fanart'):
|
|
|
|
for url in self._url_generator(kind, kodi_type):
|
2019-01-31 06:36:52 +11:00
|
|
|
if self.wait_while_suspended():
|
2018-12-31 07:30:08 +11:00
|
|
|
return
|
|
|
|
cache_url(url)
|
|
|
|
# Toggles Image caching completed to Yes
|
|
|
|
utils.settings('plex_status_image_caching', value=utils.lang(107))
|
2016-09-17 18:58:08 +10:00
|
|
|
|
2016-09-17 19:24:12 +10:00
|
|
|
|
2018-11-06 04:00:01 +11:00
|
|
|
def cache_url(url):
|
|
|
|
url = double_urlencode(utils.try_encode(url))
|
|
|
|
sleeptime = 0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
requests.head(
|
|
|
|
url="http://%s:%s/image/image://%s"
|
2018-11-19 00:59:17 +11:00
|
|
|
% (app.CONN.webserver_host,
|
|
|
|
app.CONN.webserver_port,
|
2018-11-06 04:00:01 +11:00
|
|
|
url),
|
2018-11-19 00:59:17 +11:00
|
|
|
auth=(app.CONN.webserver_username,
|
|
|
|
app.CONN.webserver_password),
|
2018-11-06 04:00:01 +11:00
|
|
|
timeout=TIMEOUT)
|
|
|
|
except requests.Timeout:
|
|
|
|
# We don't need the result, only trigger Kodi to start the
|
|
|
|
# download. All is well
|
|
|
|
break
|
|
|
|
except requests.ConnectionError:
|
2018-11-19 00:59:17 +11:00
|
|
|
if app.APP.stop_pkc:
|
2018-11-06 04:00:01 +11:00
|
|
|
# Kodi terminated
|
|
|
|
break
|
|
|
|
# Server thinks its a DOS attack, ('error 10053')
|
|
|
|
# Wait before trying again
|
|
|
|
if sleeptime > 5:
|
|
|
|
LOG.error('Repeatedly got ConnectionError for url %s',
|
|
|
|
double_urldecode(url))
|
|
|
|
break
|
|
|
|
LOG.debug('Were trying too hard to download art, server '
|
|
|
|
'over-loaded. Sleep %s seconds before trying '
|
|
|
|
'again to download %s',
|
|
|
|
2**sleeptime, double_urldecode(url))
|
2018-11-21 02:58:25 +11:00
|
|
|
app.APP.monitor.waitForAbort((2**sleeptime))
|
2018-11-06 04:00:01 +11:00
|
|
|
sleeptime += 1
|
|
|
|
continue
|
|
|
|
except Exception as err:
|
|
|
|
LOG.error('Unknown exception for url %s: %s'.
|
|
|
|
double_urldecode(url), err)
|
|
|
|
import traceback
|
|
|
|
LOG.error("Traceback:\n%s", traceback.format_exc())
|
|
|
|
break
|
|
|
|
# We did not even get a timeout
|
|
|
|
break
|