A Reddit Wallpaper download script

Fri, 27th May 2016

I like having new wallpapers, daily. As I most often got them from /r/wallpapers, I decided to write a small script to download them to use on my desktop. The script is rather straight forward. It will download the RSS feed from /r/wallpapers and extract the URLs of the first two jpgs that are listed within, which are then downloaded using wget. It will then resize them to better fit the desktop (some wallpapers are 4K, so I just resize them to my native resolution), and that's that.

Okay, there are a few extras: it will first check to see if we're online (by pinging Google), it will then only download new wallpapers if it's been at least 6 hours since the last check. Wallpapers older than two days old will be deleted before the new ones are fetched. Additionally, the user agent is set to be the latest version of Chrome, as Reddit has checks against rogue bots (note: if you run this script too often, your ip address could be banned - best to only run it once every few hours).

You can view the code below. Either copy and paste that into a file, or download the wallpaper.sh file at the bottom of this page. Remember to tweak it to suit your own needs (as a minimum, update the paths), and then chmod +x it before running.

#!/bin/bash -e

TMP="/tmp/wallpapers"
DIR="/path/to/wallpapers"
LASTDL="$DIR/.lastDownload"
UA=`/opt/google/chrome/chrome -version`

if ping -q -c 1 www.google.com

then

if [ ! -f $LASTDL ] || [ `find "$LASTDL" -mmin +360` ]

then

find "$DIR" -name '*.jpg' -mtime +2 -exec rm {} \;

mkdir -p $TMP

FILES=$(curl -A "$UA" https://www.reddit.com/r/wallpapers.rss | xmllint --format - | grep -Eo 'http[s]?:[^"]*jpg' | grep -v thumbs | head -2)

for FILE in $FILES
do
wget -c $FILE -P $TMP
done

touch $LASTDL

mogrify -resize 1920x "$TMP/*.jpg"

mv $TMP/*.jpg $DIR

fi

fi
Downloads: wallpaper.sh (0Kb)
 
code 

Related News

Orb source code
Mon, 26th April 2021

Lasagne Monsters source code
Tue, 20th April 2021

3 Guys Apocalypse source code
Sun, 11th April 2021

Scraping Amazon's Wishlist
Sat, 21st May 2016

Resizing an array in C
Thu, 19th May 2016

Desktop site