Cache cover art thumbnails in SQLite database

This commit is contained in:
Thomas Perl 2016-11-20 11:54:05 +01:00
parent 22c67ef5cd
commit f6384f3013
3 changed files with 45 additions and 5 deletions

View File

@ -441,6 +441,7 @@ class PodcastChannelProxy(object):
self.auth_password = None
self.pause_subscription = False
self.sync_to_mp3_player = False
self.cover_thumb = None
self.auto_archive_episodes = False
def get_statistics(self):
@ -452,6 +453,9 @@ class PodcastChannelProxy(object):
return Model.sort_episodes_by_pubdate((e for c in self.channels
for e in c.get_all_episodes()), True)
def save(self):
pass
class PodcastListModel(gtk.ListStore):
C_URL, C_TITLE, C_DESCRIPTION, C_PILL, C_CHANNEL, \
@ -598,12 +602,38 @@ class PodcastListModel(gtk.ListStore):
return pixbuf
def _get_cached_thumb(self, channel):
if channel.cover_thumb is None:
return None
try:
loader = gtk.gdk.PixbufLoader('png')
loader.write(channel.cover_thumb)
loader.close()
return loader.get_pixbuf()
except Exception, e:
logger.warn('Could not load cached cover art for %s', channel.url, exc_info=True)
channel.cover_thumb = None
channel.save()
return None
def _save_cached_thumb(self, channel, pixbuf):
bufs = []
pixbuf.save_to_callback(lambda buf, data: data.append(buf), 'png', {}, bufs)
channel.cover_thumb = buffer(''.join(bufs))
channel.save()
def _get_cover_image(self, channel, add_overlay=False):
if self._cover_downloader is None:
return None
pixbuf = self._cover_downloader.get_cover(channel, avoid_downloading=True)
pixbuf_overlay = self._resize_pixbuf(channel.url, pixbuf)
pixbuf_overlay = self._get_cached_thumb(channel)
if pixbuf_overlay is None:
pixbuf = self._cover_downloader.get_cover(channel, avoid_downloading=True)
pixbuf_overlay = self._resize_pixbuf(channel.url, pixbuf)
self._save_cached_thumb(channel, pixbuf_overlay)
if add_overlay and channel.pause_subscription:
pixbuf_overlay = self._overlay_pixbuf(pixbuf_overlay, self.ICON_DISABLED)
pixbuf_overlay.saturate_and_pixelate(pixbuf_overlay, 0.0, False)
@ -800,6 +830,8 @@ class PodcastListModel(gtk.ListStore):
# Resize and add the new cover image
pixbuf = self._resize_pixbuf(channel.url, pixbuf)
self._save_cached_thumb(channel, pixbuf)
if channel.pause_subscription:
pixbuf = self._overlay_pixbuf(pixbuf, self.ICON_DISABLED)
pixbuf.saturate_and_pixelate(pixbuf, 0.0, False)

View File

@ -681,6 +681,7 @@ class PodcastChannel(PodcastModelObject):
self.download_folder = None
self.pause_subscription = False
self.sync_to_mp3_player = True
self.cover_thumb = None
self.section = _('Other')
self._common_prefix = None

View File

@ -66,9 +66,10 @@ PodcastColumns = (
'payment_url',
'download_strategy',
'sync_to_mp3_player',
'cover_thumb',
)
CURRENT_VERSION = 5
CURRENT_VERSION = 6
# SQL commands to upgrade old database versions to new ones
@ -96,7 +97,12 @@ UPGRADE_SQL = [
# Version 5: Per-podcast MP3 player device synchronization option
(4, 5, """
ALTER TABLE podcast ADD COLUMN sync_to_mp3_player INTEGER NOT NULL DEFAULT 1
""")
"""),
# Version 6: Add thumbnail for cover art
(5, 6, """
ALTER TABLE podcast ADD COLUMN cover_thumb BLOB NULL DEFAULT NULL
"""),
]
def initialize_database(db):
@ -119,7 +125,8 @@ def initialize_database(db):
section TEXT NOT NULL DEFAULT '',
payment_url TEXT NULL DEFAULT NULL,
download_strategy INTEGER NOT NULL DEFAULT 0,
sync_to_mp3_player INTEGER NOT NULL DEFAULT 1
sync_to_mp3_player INTEGER NOT NULL DEFAULT 1,
cover_thumb BLOB NULL DEFAULT NULL
)
""")