scripts/change-wallpaper.sh

74 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# 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"
)
# 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
# Download a new random wallpaper
new_wallpaper=$(download_random_wallpaper)
if [ -n "$new_wallpaper" ] && [ -f "$new_wallpaper" ]; then
echo "Changing wallpaper to $new_wallpaper"
echo "$new_wallpaper" > "$HOME/.wallpaper"
# Get the topic used for this wallpaper
topic=$(cat "$WALLPAPER_DIR/.last_topic")
# 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