dotfiles/bin/wallpaper

93 lines
2.5 KiB
Python
Executable file

#!/usr/bin/env python3
from datetime import datetime
from pathlib import Path
import random
import subprocess
import sys
import os
HYPRPAPER_CNF = "~/.config/hypr/hyprpaper.conf"
WALLPAPER_DIR = "~/pictures/wallpapers"
WALLPAPER_DIR_WINTER = "~/pictures/wallpapers/winter"
WINTER_MONTHS = [
11, # November
12, # December
1, # January
2, # February
]
def get_available_wallpapers():
w_dir = (
WALLPAPER_DIR_WINTER if datetime.now().month in WINTER_MONTHS else WALLPAPER_DIR
)
w_dir = Path(w_dir).expanduser()
wallpapers = []
for f in w_dir.iterdir():
if f.is_file(follow_symlinks=False):
wallpapers.append(f)
if f.is_symlink():
wallpapers.append(f.resolve())
return [str(p) for p in wallpapers]
def update_config():
wallpapers = get_available_wallpapers()
config_path = Path(HYPRPAPER_CNF).expanduser()
with open(config_path, "w") as f:
current_wallpaper = random.choice(wallpapers)
for w_path in wallpapers:
print(f"preload = {w_path}", file=f)
print(f"wallpaper = ,{current_wallpaper}", file=f)
print("splash = false", file=f)
def cmd_init():
update_config()
subprocess.run(["killall", "hyprpaper"])
subprocess.Popen(["hyprpaper"])
def cmd_next_wallpaper():
wallpapers = []
config_path = Path(HYPRPAPER_CNF).expanduser()
with open(config_path, "r") as f:
for line in f:
if line.startswith("preload = "):
w_path = Path(line[10:].strip("\n"))
if w_path.is_file(follow_symlinks=False):
wallpapers.append(str(w_path))
new_wallpaper = random.choice(wallpapers)
subprocess.run(["hyprctl", "hyprpaper", "wallpaper", f",{new_wallpaper}"])
def usage():
print("Usage:", file=sys.stderr)
print(" wallpaper <command>", file=sys.stderr)
print("", file=sys.stderr)
print("Available commands:", file=sys.stderr)
print(" init (re)start the daemon", file=sys.stderr)
print(" change change the wallpaper", file=sys.stderr)
print(" next alias for change", file=sys.stderr)
sys.exit(1)
def main():
if len(sys.argv) != 2:
usage()
cmd = sys.argv[1]
commands = {
# (re)start the daemon
"init": cmd_init,
# change the wallpaper
"change": cmd_next_wallpaper,
"next": cmd_next_wallpaper,
}
if cmd not in commands:
usage()
commands[cmd]()
if __name__ == "__main__":
main()