PlexKodiConnect/resources/lib/artwork.py

140 lines
4.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
2017-12-09 05:43:06 +11:00
from logging import getLogger
2017-12-10 00:35:08 +11:00
import requests
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
2018-06-22 03:24:37 +10:00
LOG = getLogger('PLEX.artwork')
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)
BATCH_SIZE = 500
2018-11-06 04:00:01 +11:00
def double_urlencode(text):
2019-03-30 20:32:56 +11:00
return utils.quote_plus(utils.quote_plus(text))
def double_urldecode(text):
2019-03-30 20:32:56 +11:00
return utils.unquote(utils.unquote(text))
2018-11-06 01:23:51 +11:00
class ImageCachingThread(backgroundthread.KillableThread):
def __init__(self):
super(ImageCachingThread, self).__init__()
2019-02-08 22:04:24 +11:00
self.suspend_points = [(self, '_suspended')]
if not utils.settings('imageSyncDuringPlayback') == 'true':
2019-02-08 22:04:24 +11:00
self.suspend_points.append((app.APP, 'is_playing_video'))
def should_suspend(self):
return any(getattr(obj, attrib) for obj, attrib in self.suspend_points)
2018-11-06 01:23:51 +11:00
2019-02-08 23:52:33 +11:00
@staticmethod
def _url_generator(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:
texture_db = KodiTextureDB(kodiconn=kodidb.kodiconn,
artconn=kodidb.artconn,
lock=False)
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
def run(self):
2018-11-06 00:18:52 +11:00
LOG.info("---===### Starting ImageCachingThread ###===---")
app.APP.register_caching_thread(self)
try:
self._run()
2019-02-03 06:22:06 +11:00
except Exception:
utils.ERROR()
finally:
app.APP.deregister_caching_thread(self)
LOG.info("---===### Stopped ImageCachingThread ###===---")
def _loop(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):
if self.should_suspend() or self.should_cancel():
return False
cache_url(url)
# Toggles Image caching completed to Yes
utils.settings('plex_status_image_caching', value=utils.lang(107))
return True
def _run(self):
while True:
if self._loop():
break
if self.wait_while_suspended():
break
2018-11-06 04:00:01 +11:00
def cache_url(url):
2019-03-30 20:32:56 +11:00
url = double_urlencode(url)
2018-11-06 04:00:01 +11:00
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))
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