Handle data URLs for covers in Get Books results

This commit is contained in:
Kovid Goyal 2015-12-20 10:37:57 +05:30
parent 21346e3e20
commit dbf36e96e7

View File

@ -6,7 +6,7 @@ __license__ = 'GPL 3'
__copyright__ = '2011, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
import traceback
import traceback, base64
from contextlib import closing
from threading import Thread
from Queue import Queue
@ -139,6 +139,8 @@ class CoverThreadPool(GenericDownloadThreadPool):
self.tasks.put((search_result, update_callback, timeout))
GenericDownloadThreadPool.add_task(self)
def decode_data_url(url):
return base64.standard_b64decode(url.partition(',')[2])
class CoverThread(Thread):
@ -159,6 +161,9 @@ class CoverThread(Thread):
try:
result, callback, timeout = self.tasks.get()
if result and result.cover_url:
if result.cover_url.startswith('data:'):
result.cover_data = decode_data_url(result.cover_url)
else:
with closing(self.br.open(result.cover_url, timeout=timeout)) as f:
result.cover_data = f.read()
result.cover_data = scale_image(result.cover_data, 64, 64)[2]