Skip to content

Instantly share code, notes, and snippets.

@arvigeus
Created September 19, 2024 14:34
Show Gist options
  • Save arvigeus/8305de0e53917e1fe48ec8ab6e7f3ad3 to your computer and use it in GitHub Desktop.
Save arvigeus/8305de0e53917e1fe48ec8ab6e7f3ad3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Wallhaven API base URL
API_URL="https://wallhaven.cc/api/v1/search"
# Array of tags (use the tag id)
TAGS=(
"id:7435" # Makoto Shinkai
"id:54397" # Kimi no Na Wa
"id:5309" # The Garden of Words
"id:4328" # 5 Centimeters Per Second
"id:1748" # Studio Ghibli
"id:49129" # Nier Automata
"id:19450" # Nier
"id:22263" # Life is Strange
"id:35" # Wall-E
"id:462" # Bioshock Infinite
"id:42523" # Erased
"id:151750" # Stellar Blade
"id:1750" # Firewatch
"id:2172" # Transistor
)
# Pick a random tag from the array
TAG_ID=${TAGS[$RANDOM % ${#TAGS[@]}]}
# Category options for wallpapers (1: general, 2: anime, 3: people)
CATEGORIES="111" # Include all categories
# Purity (1: SFW, 2: sketchy, 3: NSFW) - SFW only
PURITY="100"
# If you have an API key, replace 'your_api_key' with it, otherwise leave empty.
API_KEY="your_api_key"
# API request
if [ -z "$API_KEY" ]; then
# Without API Key
RESPONSE=$(curl -s "$API_URL?q=$TAG_ID&categories=$CATEGORIES&purity=$PURITY&sorting=random&page=1")
else
# With API Key
RESPONSE=$(curl -s "$API_URL?q=$TAG_ID&categories=$CATEGORIES&purity=$PURITY&sorting=random&page=1&apikey=$API_KEY")
fi
# Fetch a random wallpaper using the Wallhaven API
# response=$(curl -s "https://wallhaven.cc/api/v1/search?apikey=$API_KEY&sorting=random&categories=111&purity=100")
# https://wallhaven.cc/api/v1/search?sorting=hot&categories=111&purity=100&atLeast=1920x1080
# Extract the wallpaper URL and ID from the JSON response using jq
WALLPAPER_URL=$(echo "$RESPONSE" | jq -r '.data[0].path')
WALLPAPER_ID=$(echo "$RESPONSE" | jq -r '.data[0].id')
# Directory to save wallpapers
WALLPAPER_DIR="$HOME/Pictures/Wallpapers"
# Create the directory if it doesn't exist
mkdir -p "$WALLPAPER_DIR"
# Check if a valid URL was returned
if [ "$WALLPAPER_URL" != "null" ]; then
# Save the wallpaper using its ID as the filename
WALLPAPER_PATH="$WALLPAPER_DIR/${WALLPAPER_ID}.jpg"
wget -O "$WALLPAPER_PATH" "$WALLPAPER_URL"
echo "Downloaded: $WALLPAPER_URL"
# Set wallpaper on Gnome
if [ "$XDG_CURRENT_DESKTOP" = "GNOME" ]; then
gsettings set org.gnome.desktop.background picture-uri "file://$WALLPAPER_PATH"
echo "Wallpaper set on Gnome."
# Set wallpaper on KDE
elif [ "$XDG_CURRENT_DESKTOP" = "KDE" ]; then
dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript \
"string:var allDesktops = desktops(); for (i=0;i<allDesktops.length;i++) { d = allDesktops[i]; d.wallpaperPlugin = 'org.kde.image'; d.currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General'); d.writeConfig('Image', 'file://$WALLPAPER_PATH') }"
echo "Wallpaper set on KDE."
else
echo "Unsupported desktop environment. Wallpaper was downloaded but not set."
fi
else
echo "No wallpaper found or an error occurred."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment