basic avatar image cache in place of handy avatar
This commit is contained in:
parent
e29c5d3e8d
commit
36bef985f4
1 changed files with 27 additions and 16 deletions
|
@ -27,7 +27,7 @@ from dateutil.tz import tzlocal
|
|||
gi.require_version('GdkPixbuf', '2.0')
|
||||
gi.require_version('Gdk', '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')
|
||||
from gi.repository import Handy
|
||||
|
@ -362,9 +362,9 @@ class PostItem(Gtk.ListBoxRow):
|
|||
|
||||
# header container
|
||||
self.h_box = Gtk.Box(orientation='horizontal')
|
||||
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 = Gtk.Image.new_from_pixbuf(self.get_avatar(48, post.user.content.avatar_image.link))
|
||||
#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 = 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.h_box.pack_start(self.avatar, False, False, 10)
|
||||
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)
|
||||
|
||||
def get_avatar(self, size, avatar):
|
||||
# TODO: needs a cache
|
||||
# TODO: can I make it not round?
|
||||
r = requests.get(avatar.link)
|
||||
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
|
||||
return pixbuf.scale_simple(size,new_height,GdkPixbuf.InterpType.BILINEAR)
|
||||
# TODO: age out cache
|
||||
cache_dir = os.path.join(GLib.get_user_cache_dir(), 'avatars')
|
||||
link_hash = avatar.link.rsplit('/', 1)[-1]
|
||||
file_hash = os.path.join(cache_dir, link_hash)
|
||||
if not os.path.exists(cache_dir):
|
||||
os.mkdir(cache_dir)
|
||||
|
||||
if os.path.exists(file_hash):
|
||||
scaled_img = GdkPixbuf.Pixbuf.new_from_file(file_hash)
|
||||
else:
|
||||
r = requests.get(avatar.link, stream=True)
|
||||
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):
|
||||
url = oembed.thumbnail_url
|
||||
|
|
Loading…
Reference in a new issue