feat: add new feature for opening logs and current posts

This commit is contained in:
Kat Inskip 2025-02-18 17:27:08 -08:00
parent c3ff5ef5e4
commit 8d685de972
Signed by: kat
GPG key ID: 465E64DECEA8CF0F
3 changed files with 65 additions and 21 deletions

View file

@ -54,7 +54,7 @@ def main():
else: else:
count = args.count count = args.count
files = source_handlers[args.source](count, args.tags) files, posts = source_handlers[args.source](count, args.tags)
if not args.environment: if not args.environment:
set_environment_wallpapers(environment, files, displays) set_environment_wallpapers(environment, files, displays)

View file

@ -16,19 +16,21 @@ from konawall.custom_print import kv_print
from humanfriendly import format_timespan from humanfriendly import format_timespan
class Konawall(wx.adv.TaskBarIcon): class Konawall(wx.adv.TaskBarIcon):
def __init__(self, version, file_logger): def __init__(self, version, file_logger, log_path):
super().__init__() super().__init__()
# Prevents it from closing before it has done any work on macOS # Prevents it from closing before it has done any work on macOS
if wx.Platform == "__WXMAC__" or wx.Platform == "__WXGTK__": if wx.Platform == "__WXMAC__" or wx.Platform == "__WXGTK__":
self.hidden_frame = wx.Frame(None) self.hidden_frame = wx.Frame(None)
self.hidden_frame.Hide() self.hidden_frame.Hide()
self.log_path = log_path
self.wallpaper_rotation_counter = 0 self.wallpaper_rotation_counter = 0
self.file_logger = file_logger self.file_logger = file_logger
self.version = version self.version = version
self.title_string = f"Konawall - {version}" self.title_string = f"Konawall - {version}"
self.description_string = "A hopefully cross-platform service for fetching wallpapers and setting them." self.description_string = "A hopefully cross-platform service for fetching wallpapers and setting them."
self.loaded_before = False self.loaded_before = False
self.current = []
print(self.IsAvailable()) print(self.IsAvailable())
print(self.IsOk()) print(self.IsOk())
@ -43,15 +45,22 @@ class Konawall(wx.adv.TaskBarIcon):
# Reload (actually load) the config and modules. # Reload (actually load) the config and modules.
if wx.Platform == "__WXGTK__": if wx.Platform == "__WXGTK__":
from xdg_base_dirs import xdg_config_home from xdg_base_dirs import xdg_config_home
self.config_path = os.path.join(xdg_config_home(), "konawall", "config.toml") self.config_path = os.path.join(xdg_config_home(), "konawall")
if wx.Platform == "__WXMAC__": if wx.Platform == "__WXMAC__":
config_path_string = "~/Library/Application Support/konawall/config.toml" config_path_string = "~/Library/Application Support/konawall/"
self.config_path = os.path.expanduser(config_path_string) self.config_path = os.path.expanduser(config_path_string)
elif wx.Platform == "__WXMSW__": elif wx.Platform == "__WXMSW__":
config_path_string = "%APPDATA%\\konawall\\config.toml" config_path_string = "%APPDATA%\\konawall"
self.config_path = os.path.expandvars(config_path_string) self.config_path = os.path.expandvars(config_path_string)
else: else:
self.config_path = os.path.join(os.path.expanduser("~"), ".config", "konawall", "config.toml") try:
from xdg_base_dirs import xdg_config_home
self.config_path = os.path.join(xdg_config_home(), "konawall")
except:
self.config_path = os.path.join(os.path.expanduser("~"), ".config", "konawall")
if not os.path.exists(self.config_path):
os.makedirs(self.config_path)
self.config_path = os.path.join(self.config_path, "config.toml")
self.reload_config() self.reload_config()
self.import_modules() self.import_modules()
@ -61,12 +70,14 @@ class Konawall(wx.adv.TaskBarIcon):
# Set up the taskbar icon, menu, bindings, ... # Set up the taskbar icon, menu, bindings, ...
icon = self.generate_icon() icon = self.generate_icon()
self.SetIcon(icon, self.title_string) self.SetIcon(icon, self.title_string)
if self.environment in ["hyprland", "gnome", "kde"]: if self.environment in ["hyprland", "gnome"]:
import pystray import pystray
def setup(self): def setup(self):
self.visible = True self.visible = True
self.external_icon = pystray.Icon("Konawall - {version}", icon=self.generate_icon_bitmap(), menu=pystray.Menu( self.external_icon = pystray.Icon("Konawall - {version}", icon=self.generate_icon_bitmap(), menu=pystray.Menu(
pystray.MenuItem("Rotate", self.rotate_wallpapers), pystray.MenuItem("Rotate", self.rotate_wallpapers),
pystray.MenuItem("Open URL for image", self.open_url),
pystray.MenuItem("Open log", self.open_log),
pystray.MenuItem("Toggle Rotation", self.toggle_timed_wallpaper_rotation, checked=lambda item: self.rotate), pystray.MenuItem("Toggle Rotation", self.toggle_timed_wallpaper_rotation, checked=lambda item: self.rotate),
pystray.MenuItem("Quit", self.close_program_menu_item) pystray.MenuItem("Quit", self.close_program_menu_item)
)) ))
@ -78,6 +89,13 @@ class Konawall(wx.adv.TaskBarIcon):
# Run the first time, manually # Run the first time, manually
self.rotate_wallpapers(None) self.rotate_wallpapers(None)
def open_url(self, evt=None):
for post in self.current:
subprocess.call(["xdg-open", f'https://konachan.com/post/show/{post["id"]}'])
def open_log(self, evt=None):
subprocess.call(["xdg-open", self.log_path])
# wxPython requires a wx.Bitmap, so we generate one from a PIL.Image # wxPython requires a wx.Bitmap, so we generate one from a PIL.Image
def generate_icon_bitmap(self): def generate_icon_bitmap(self):
width = 128 width = 128
@ -197,6 +215,21 @@ class Konawall(wx.adv.TaskBarIcon):
"Toggle the automatic wallpaper rotation timer" "Toggle the automatic wallpaper rotation timer"
) )
create_separator(self.menu)
create_menu_item(
self.menu,
"Open wallpaper URLs",
self.open_url,
"Open wallpaper URLs via xdg-open"
)
create_menu_item(
self.menu,
"Open log",
self.open_log,
"Open konawall log in editor"
)
create_separator(self.menu) create_separator(self.menu)
# Interactive config editing # Interactive config editing
@ -212,6 +245,7 @@ class Konawall(wx.adv.TaskBarIcon):
self.reload_config_menu_item, self.reload_config_menu_item,
"Reload the config file from disk" "Reload the config file from disk"
) )
create_separator(self.menu)
# Exit # Exit
create_menu_item( create_menu_item(
self.menu, self.menu,
@ -307,7 +341,7 @@ class Konawall(wx.adv.TaskBarIcon):
def rotate_wallpapers(self, event): def rotate_wallpapers(self, event):
displays = screeninfo.get_monitors() displays = screeninfo.get_monitors()
count = len(displays) count = len(displays)
files = source_handlers[self.source](count, self.tags) files, self.current = source_handlers[self.source](count, self.tags)
set_environment_wallpapers(self.environment, files, displays) set_environment_wallpapers(self.environment, files, displays)
# For macOS # For macOS
@ -350,16 +384,23 @@ def main():
version = "testing version" version = "testing version"
if wx.Platform == "__WXGTK__": if wx.Platform == "__WXGTK__":
from xdg_base_dirs import xdg_config_home from xdg_base_dirs import xdg_data_home
log_path = os.path.join(xdg_config_home(), "konawall", "log.toml") log_path = os.path.join(xdg_data_home(), "konawall")
if wx.Platform == "__WXMAC__": if wx.Platform == "__WXMAC__":
log_path_string = "~/Library/Application Support/konawall/log.toml" log_path_string = "~/Library/Application Support/konawall"
log_path = os.path.expanduser(log_path_string) log_path = os.path.expanduser(log_path_string)
elif wx.Platform == "__WXMSW__": elif wx.Platform == "__WXMSW__":
log_path_string = "%APPDATA%\\konawall\\log.toml" log_path_string = "%APPDATA%\\konawall"
log_path = os.path.expandvars(log_path_string) log_path = os.path.expandvars(log_path_string)
else: else:
log_path = os.path.join(os.path.expanduser("~"), ".config", "konawall", "log.toml") try:
from xdg_base_dirs import xdg_data_home
log_path = os.path.join(xdg_data_home(), "konawall")
except:
log_path = os.path.join(os.path.expanduser("~"), ".config", "konawall")
if not os.path.exists(log_path):
os.makedirs(log_path)
log_path = os.path.join(log_path, "konawall.log")
file_logger = logging.FileHandler(log_path, mode="a") file_logger = logging.FileHandler(log_path, mode="a")
console_logger = logging.StreamHandler() console_logger = logging.StreamHandler()
logging.basicConfig( logging.basicConfig(
@ -372,7 +413,7 @@ def main():
) )
app = wx.App(redirect=False) app = wx.App(redirect=False)
app.SetExitOnFrameDelete(False) app.SetExitOnFrameDelete(False)
Konawall(version, file_logger) Konawall(version, file_logger, log_path)
app.MainLoop() app.MainLoop()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -26,7 +26,7 @@ def request_posts(count: int, tags: list) -> list:
# Check if the request was successful # Check if the request was successful
logging.debug("Status code: " + str(response.status_code)) logging.debug("Status code: " + str(response.status_code))
# List of URLs to download # List of URLs to download
post_urls: list = [] posts: list = []
if response.status_code == 200: if response.status_code == 200:
# Get the JSON data from the response # Get the JSON data from the response
json = response.json() json = response.json()
@ -39,11 +39,11 @@ def request_posts(count: int, tags: list) -> list:
kv_print("Tags", post["tags"]) kv_print("Tags", post["tags"])
kv_print("URL", post["file_url"]) kv_print("URL", post["file_url"])
# Append the URL to the list # Append the URL to the list
post_urls.append(post["file_url"]) posts.append(post)
else: else:
# Raise an exception if the request failed # Raise an exception if the request failed
RequestFailed(response.status_code) RequestFailed(response.status_code)
return post_urls return posts
""" """
Download a number of images from Konachan given a list of tags and a count Download a number of images from Konachan given a list of tags and a count
@ -55,8 +55,11 @@ Download a number of images from Konachan given a list of tags and a count
def handle(count: int, tags: list) -> list: def handle(count: int, tags: list) -> list:
logging.debug(f"handle_konachan() called with count={count}, tags=[{', '.join(tags)}]") logging.debug(f"handle_konachan() called with count={count}, tags=[{', '.join(tags)}]")
# Get a list of URLs to download # Get a list of URLs to download
post_urls: list = request_posts(count, tags) posts: list = request_posts(count, tags)
urls: list = []
# Download the images # Download the images
files = download_files(post_urls) for post in posts:
urls.append(post["file_url"])
files = download_files(urls)
# Return the downloaded files # Return the downloaded files
return files return files, posts