๐ I've made a little Bash script that will download a random wallpaper from r/wallpapers and set it for you
๐ก Newskategorie: Linux Tipps
๐ Quelle: reddit.com
Title says it all. I thought someone here may find this useful. The script is made for KDE, but can be adapted to be used on any DE. Just replace the last command on the script with the command that replace your wallpaper on your desktop.
```
!/bin/bash
cron example:
0 * * * * ~/.local/bin/reddit-wallpaper
Cache file to save Reddit api response.
JSONCACHE="/tmp/reddit-wallpaper-cache.json"
Directory where wallpapers are going to be saved
WPATH=${WALLPAPER_PATH:-"$HOME/Pictures/Wallpapers"} mkdir -p $WPATH
We need to change our user agent so Reddit allow us to get the JSON without errors
USERAGENT="Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0"
APIURL="https://www.reddit.com/r/wallpapers.json?limit=100"
Remove cached response if older than 2 hours.
find $(dirname $JSONCACHE) -name "$(basename $JSONCACHE)" -mmin +120 -exec rm {} \; 2>/dev/null
exit on error
set -e
If the cache file doesnt exist, we create it.
if [ ! -f "$JSONCACHE" ]; then curl -H "User-Agent: $USERAGENT" $APIURL -s > $JSONCACHE fi
loop through the JSON until a valid image is found
while : ; do # Get a random item from the returned JSON WJSON=$(cat $JSONCACHE | jq -c '.data.children[].data' --raw-output | shuf -n 1) WDOMAIN=$(echo "$WJSON" | jq '.domain' --raw-output) echo $WDOMAIN if [ "$WDOMAIN" == "i.redd.it" ]; then break fi if [ "$WDOMAIN" == "reddit.com" ]; then break fi if [ "$WDOMAIN" == "i.imgur.com" ]; then break fi done
If the item is gallery, pick a random item from the gallery
WISGALLERY=$(echo "$WJSON" | jq '.is_gallery') if [ "$WISGALLERY" == "true" ]; then GITEM=$(echo "$WJSON" | jq -c '.media_metadata' --raw-output | jq -c 'to_entries[] | .value' | shuf -n 1) WMIME=$(echo "$GITEM" | jq '.m' --raw-output) WEXT=$(basename "$WMIME") WID=$(echo $GITEM | jq '.id' --raw-output) WURI="https://i.redd.it/$WID.$WEXT"
If the item is not gallery, obtain the image URL directly
else WURI=$(echo "$WJSON" | jq '.url' --raw-output) fi
echo $WURI
Get the image name + extension
WNAME=$(basename "$WURI")
FILEPATH="$WPATH/$WNAME"
If the image doesnt exists in our system, we download it.
if [ ! -f "$FILEPATH" ]; then curl "$WURI" -s > $FILEPATH fi
Set the image in our KDE system (Replace this to support other DE)
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript " 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://${FILEPATH}') }} " ``` The script stores the JSON response from reddit for 2 hours in the /tmp folder for faster operation. The script also verifies if the image already exists in your wallpapers folder and avoid to download it again if it exists.
[link] [comments] ...