basic avatar image cache in place of handy avatar

This commit is contained in:
Morgan McMillian 2020-12-27 15:30:00 -08:00
parent e29c5d3e8d
commit 36bef985f4

View file

@ -27,7 +27,7 @@ from dateutil.tz import tzlocal
gi.require_version('GdkPixbuf', '2.0') gi.require_version('GdkPixbuf', '2.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import GObject, GdkPixbuf, Gdk, Gtk, Gio from gi.repository import GObject, GdkPixbuf, Gdk, Gtk, Gio, GLib
gi.require_version('Handy', '1') gi.require_version('Handy', '1')
from gi.repository import Handy from gi.repository import Handy
@ -362,9 +362,9 @@ class PostItem(Gtk.ListBoxRow):
# header container # header container
self.h_box = Gtk.Box(orientation='horizontal') self.h_box = Gtk.Box(orientation='horizontal')
self.avatar = Handy.Avatar(size=48, text=post.user.username, show_initials=True) #self.avatar = Handy.Avatar(size=48, text=post.user.username, show_initials=True)
self.avatar.set_image_load_func(self.get_avatar, post.user.content.avatar_image) #self.avatar.set_image_load_func(self.get_avatar, post.user.content.avatar_image)
#self.avatar = Gtk.Image.new_from_pixbuf(self.get_avatar(48, post.user.content.avatar_image.link)) self.avatar = Gtk.Image.new_from_pixbuf(self.get_avatar(48, post.user.content.avatar_image))
self.source = Gtk.Label(label=post.source.name, xalign=1, yalign=0) self.source = Gtk.Label(label=post.source.name, xalign=1, yalign=0)
self.h_box.pack_start(self.avatar, False, False, 10) self.h_box.pack_start(self.avatar, False, False, 10)
self.h_box.pack_start(self.name_box, False, False, 0) self.h_box.pack_start(self.name_box, False, False, 0)
@ -433,18 +433,29 @@ class PostItem(Gtk.ListBoxRow):
self.box.pack_start(self.f_box, True, True, 10) self.box.pack_start(self.f_box, True, True, 10)
def get_avatar(self, size, avatar): def get_avatar(self, size, avatar):
# TODO: needs a cache # TODO: age out cache
# TODO: can I make it not round? cache_dir = os.path.join(GLib.get_user_cache_dir(), 'avatars')
r = requests.get(avatar.link) link_hash = avatar.link.rsplit('/', 1)[-1]
loader = GdkPixbuf.PixbufLoader() file_hash = os.path.join(cache_dir, link_hash)
loader.write(r.content) if not os.path.exists(cache_dir):
loader.close() os.mkdir(cache_dir)
pixbuf = loader.get_pixbuf()
old_width = avatar.width if os.path.exists(file_hash):
old_height = avatar.height scaled_img = GdkPixbuf.Pixbuf.new_from_file(file_hash)
ratio = old_width / old_height else:
new_height = size / ratio r = requests.get(avatar.link, stream=True)
return pixbuf.scale_simple(size,new_height,GdkPixbuf.InterpType.BILINEAR) loader = GdkPixbuf.PixbufLoader()
loader.write(r.content)
loader.close()
pixbuf = loader.get_pixbuf()
old_width = avatar.width
old_height = avatar.height
ratio = old_width / old_height
new_height = size / ratio
scaled_img = pixbuf.scale_simple(size,new_height,GdkPixbuf.InterpType.BILINEAR)
scaled_img.savev(file_hash, 'jpeg', ["quality"], ["100"])
return scaled_img
def get_oembed_thumb(self, oembed): def get_oembed_thumb(self, oembed):
url = oembed.thumbnail_url url = oembed.thumbnail_url