Skip to content

Instantly share code, notes, and snippets.

@kalkun
Last active January 2, 2016 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalkun/8378148 to your computer and use it in GitHub Desktop.
Save kalkun/8378148 to your computer and use it in GitHub Desktop.
A desktop wallpaper selector for gnome-3 gsettings. It selects the most upvoted image from http://reddit.com/r/wallpapers with precedence to direct links to images rather than album links.
#!/bin/bash
# Syntax:
#
# getwallpaper [subreddit] [new/rising/hour/day/month/year/all]
#
# By default getwallpaper will fetch images that are hot from r/wallpapers
# If the first option is used but not the second then
# an image from r/[subreddit] sorted after hot, will be fetched.
#
# Example usage:
#
# getwallpaper art new
#
# This will get newly posted images from r/art
# Otherwise:
#
# getwallpaper art hour
#
# Will get top voted images from r/art in the latest hour
## change this to change the download folder
## need to be from root in gnome-3
location=~/Pictures/RWallpapers/
## change this to change the location of the logfile
logfile=~/tmp/log
## change this to change location of dump file
dump=~/tmp/dump
stamp=$(date +%F%t%R )
echo Starting script getwallpapers at $stamp >> $logfile
## choose one of these lines
if [ $# -eq 1 ]; then
ImGur=$(curl -Gs "http://www.reddit.com/r/$1")
elif [ $# -eq 2 ]; then
if [ $2 = "new" -o $2 = "rising" ]; then
ImGur=$(curl -Gs "http://www.reddit.com/r/$1/$2")
else
ImGur=$(curl -Gs --data "sort=top&t=$2" "http://www.reddit.com/r/$1/top/")
fi
else
ImGur=$(curl -Gs "http://www.reddit.com/r/wallpapers")
fi
ImGur1=$(echo -e $ImGur | egrep -o "http://imgur.com/[0-9a-zA-Z]*(/[0-9a-zA-Z])*" | egrep -m 1 ^.*$)
ImGur2=$(echo -e $ImGur | egrep -io "http://([0-9a-gi-z]|h[-0-9a-su-z_\./]|ht[-0-9a-su-z_\./]|htt[-0-9a-oq-z_\./]|[-_\./0-9])*(jpg|jpeg)" | sed '/^.*thumb.*$/ d' | cut -c 8- | egrep -m 1 "^.*$")
#un-/comment to dump raw scraped data
echo $ImGur > $dump
if [ -n "$ImGur1" -o -n "$ImGur2" ]; then
if [ -z $ImGur2 ]; then
DlLink=$(curl -Gs $ImGur1 | egrep -o "<img.*>" | sed '/<img.*thumb.*>/ d' | egrep -o "i.imgur\.com.*\.[a-zA-Z]{1,4}" | egrep -m 1 ^.*$)
else
DlLink=$ImGur2
fi
else
echo failed to get an imgur link from the original reddit feed >> $logfile
fi
## The next two lines is because Cron needs strict environment variables to be
## set so the following lines is finding the process ID of the current
## gnome-session and then setting just that for the cron environment.
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
# check if DlLink is not empty before applying new background
if [ -n "$DlLink" ]; then
name=$( echo $DlLink | sed 's/\//_/g' )
if [ ! -e ~/Pictures/RWallpapers/$name ]; then
echo -e Downloading from: "\n"http://$DlLink >> $logfile
wget -O $location$name "http://$DlLink"
gsettings set org.gnome.desktop.background picture-uri "file://$location$name"
echo -e using: "\nfile:///home/jesper/Pictures/RWallpapers/$name" >> $logfile
else
echo -e Nothing new "\n"Link to picture: "\n"http://$DlLink >> $logfile
echo -e Still using: "\nfile:///home/jesper/Pictures/RWallpapers/$name" >> $logfile
gsettings set org.gnome.desktop.background picture-uri "file://$location$name"
fi
else
echo failed to read link. DlLink is empty >> $logfile
fi
echo -------------------------------------------------------------------------------- >> $logfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment