implement random wallpaper download from Wallhaven API with topic selection

This commit is contained in:
sudacode 2025-05-02 22:05:34 -07:00
parent 04a1729781
commit 2f66d3191c
Signed by: sudacode
SSH Key Fingerprint: SHA256:lT5C2bB398DcX6daCF/gYFNSTK3y+Du3oTGUnYzfTEw

View File

@ -1,26 +1,73 @@
#!/usr/bin/env bash
IMG="$1"
# Wallhaven API configuration
WALLHAVEN_API="https://wallhaven.cc/api/v1"
WALLPAPER_DIR="$HOME/Pictures/wallpapers"
TOPICS=(
"konosuba"
"bunny girl senpai"
"oshi no ko"
"kill la kill"
"lofi"
"eminence in shadow"
"mobuseka"
)
if [[ -f "$IMG" ]]; then
echo "Changing wallpaper to $IMG"
echo "$IMG" > "$HOME/.wallpaper"
hyprctl hyprpaper reload ,"$IMG"
notify-send -i hyprpaper -u normal "change-wallpaper.sh" "Wallpaper changed to ${IMG##*/variety/}"
exit 0
# Create wallpaper directory if it doesn't exist
mkdir -p "$WALLPAPER_DIR"
# Function to download a random wallpaper from Wallhaven
download_random_wallpaper() {
# Select a random topic
local random_topic="${TOPICS[$RANDOM % ${#TOPICS[@]}]}"
local query=$(echo "$random_topic" | sed 's/ /+/g')
echo "Searching for wallpapers related to: $random_topic" >&2
# Get wallpapers from Wallhaven API
local response=$(curl -s "$WALLHAVEN_API/search?q=$query&purity=100&categories=110&sorting=random")
# Get a random image URL from the response
local url=$(echo "$response" | jq -r '.data[0].path')
if [ -n "$url" ] && [ "$url" != "null" ]; then
local filename=$(basename "$url")
echo "Downloading: $filename" >&2
curl -s "$url" -o "$WALLPAPER_DIR/$filename"
if [ $? -eq 0 ]; then
echo "$WALLPAPER_DIR/$filename"
echo "$random_topic" > "$WALLPAPER_DIR/.last_topic"
return 0
fi
fi
echo "No wallpapers found for topic: $random_topic" >&2
return 1
}
# Handle direct image file input
if [[ -f "$1" ]]; then
echo "Changing wallpaper to $1"
echo "$1" > "$HOME/.wallpaper"
hyprctl hyprpaper reload ,"$1"
notify-send -i hyprpaper -u normal "change-wallpaper.sh" "Wallpaper changed to ${1##*/}"
exit 0
fi
WALLPAPER_DIR="$HOME/Pictures/variety/"
CURRENT_WALL=$(hyprctl hyprpaper listloaded)
# Download a new random wallpaper
new_wallpaper=$(download_random_wallpaper)
# Get a random wallpaper that is not the current one
WALLPAPER=$(find "$WALLPAPER_DIR" -type f ! -name "$(basename "$CURRENT_WALL")" ! -name "*.json" | shuf -n 1)
# remove double slashes that's there for some reason
WALLPAPER=$(printf "%s" "$WALLPAPER" | tr -s '/')
if [ -n "$new_wallpaper" ] && [ -f "$new_wallpaper" ]; then
echo "Changing wallpaper to $new_wallpaper"
echo "$new_wallpaper" > "$HOME/.wallpaper"
echo "Changing wallpaper to $WALLPAPER"
echo "$WALLPAPER" > "$HOME/.wallpaper"
# Get the topic used for this wallpaper
topic=$(cat "$WALLPAPER_DIR/.last_topic")
# Apply the selected wallpaper
hyprctl hyprpaper reload ,"$WALLPAPER"
notify-send -i hyprpaper -u normal "change-wallpaper.sh" "Wallpaper changed to ${WALLPAPER##*/variety/}"
# Apply the selected wallpaper
hyprctl hyprpaper reload ,"$new_wallpaper"
notify-send -i hyprpaper -u normal "change-wallpaper.sh" "Wallpaper changed to ${new_wallpaper##*/wallpapers/} ($topic)"
else
notify-send -i hyprpaper -u critical "change-wallpaper.sh" "Failed to download new wallpaper"
exit 1
fi