Skip to content

Instantly share code, notes, and snippets.

@taiwbi
Last active June 15, 2024 17:11
Show Gist options
  • Save taiwbi/ddd6cd85028f67d3a1ba6cac82c9c65d to your computer and use it in GitHub Desktop.
Save taiwbi/ddd6cd85028f67d3a1ba6cac82c9c65d to your computer and use it in GitHub Desktop.
Gets a random wallpaper from wallhaven.cc website and sets it as the current gnome background.
#!/bin/bash
# set variables
api_key="" # put your api key here
purity="110" # SFW, Sketchy, NSFW
sorting="toplist" # date_added relevance random views favorites toplist automaticlly
topRange="3d" # 1d 3d 1w 1M 3M 6M 1y
ratios="landscape" # landscape portrait 16x9 16x10 21x9 32x9 ...
categories="111" # General, Anime, People
query="" # Search for ... -
page=$((random_number % 5 + 1)) # Get wallpaper from first `n` pages (default: 5)
api_url="https://wallhaven.cc/api/v1/search?apikey=${api_key}&q=&categories=${categories}&purity=${purity}&sorting=${sorting}&topRange=${topRange}&ratios=${ratios}&atleast=1920x1080&page=${page}"
if [ -n "$query" ]; then
api_url="$api_url&q=$query"
fi
# make request to Wallhaven API and get a random wallpaper
json=$(curl -s $api_url)
# parse the JSON response to get a random wallpaper URL
url=$(echo $json | jq -r '.data[].path' | shuf -n 1)
# get the filename from the URL
filename=$(basename "$url")
# prints the choosen background and sends a notification
echo $filename
notify-send wallhaven $filename -i backgroundtool
dir='$HOME/Pictures'
if [ -f "$dir/$filename" ]; then
echo "File exists"
else
# download the wallpaper to the same directory as the script with its original filename
curl -s "$url" >"$dir/$filename"
fi
if [ -f "$dir/$filename" ]; then
# set the wallpaper as the current background in gnome
gsettings set org.gnome.desktop.background picture-uri "file://$dir/$filename"
gsettings set org.gnome.desktop.background picture-uri-dark "file://$dir/$filename"
fi
@taiwbi
Copy link
Author

taiwbi commented Mar 18, 2023

I coded it for personal use but it might help someone :)

@4ndril
Copy link

4ndril commented Jun 15, 2024

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment