From b68d7b8f6dc3b3600e1fc123fb4ea592c965ca05 Mon Sep 17 00:00:00 2001 From: mdwalters Date: Sat, 1 Jul 2023 00:34:32 -0400 Subject: [PATCH] feat: community icons! --- src/main.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main.py b/src/main.py index 076f17a..877ae7d 100644 --- a/src/main.py +++ b/src/main.py @@ -2,11 +2,12 @@ import sys import gi import json import requests +import urllib.request gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") -from gi.repository import Gtk, Adw +from gi.repository import Gtk, Adw, Gdk, Gio class LemonadeWindow(Gtk.ApplicationWindow): def __init__(self, *args, **kwargs): @@ -66,8 +67,8 @@ class LemonadeWindow(Gtk.ApplicationWindow): self.refresh() def refresh(self, *args): - self.list = requests.get("https://lemmy.ml/api/v3/community/list?sort=Hot").json() - for post in self.list["communities"]: + self.communities = requests.get("https://lemmy.ml/api/v3/community/list?sort=Hot").json() + for community in self.communities["communities"]: box = Gtk.Box( orientation=Gtk.Orientation.HORIZONTAL, margin_top = 20, @@ -78,13 +79,24 @@ class LemonadeWindow(Gtk.ApplicationWindow): self.listbox.append(box) label = Gtk.Label.new() + avatar = Adw.Avatar.new(40, community["community"]["title"], True) - if not "description" in post["community"]: - label.set_markup(f"""{post["community"]["title"]} c/{post["community"]["name"]}""") + if "icon" in community["community"]: + try: + urllib.request.urlretrieve(community["community"]["icon"], ".comic.png") + except urllib.error.HTTPError as e: + print(e) + + avatar.set_custom_image(Gdk.Texture.new_from_file(Gio.File.new_for_path("./.comic.png"))) else: - split = post["community"]["description"].split("\n")[0] - label.set_markup(f"""{post["community"]["title"]} c/{post["community"]["name"]} -{split}""") + pass + + if not "description" in community["community"]: + label.set_markup(f"""{community["community"]["title"]} c/{community["community"]["name"]}""") + else: + split = community["community"]["description"].split("\n")[0] + label.set_markup(f"""{community["community"]["title"]} c/{community["community"]["name"]} +{split}""") label.props.margin_start = 5 label.props.hexpand = True @@ -96,6 +108,7 @@ class LemonadeWindow(Gtk.ApplicationWindow): # refresh_button.connect("clicked", self.refresh) # refresh_button.set_tooltip_text("Refresh") # box.append(refresh_button) + box.append(avatar) box.append(label) class Lemonade(Adw.Application):