Skip to content

Instantly share code, notes, and snippets.

@Rainnwind
Forked from kalkun/getwallpaper
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rainnwind/7c6e782078c894e46c49 to your computer and use it in GitHub Desktop.
Save Rainnwind/7c6e782078c894e46c49 to your computer and use it in GitHub Desktop.
Originally created by kalkun
#!/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=~/background/images/
## change this to change the location of the logfile
logfile=~/background/log/log
## change this to change location of dump file
dump=~/background/dump/tmp
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 "$location$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"
gsettings set org.gnome.desktop.background picture-options "scaled"
echo -e using: "\nfile://location$name" >> $logfile
else
echo -e Nothing new "\n"Link to picture: "\n"http://$DlLink >> $logfile
echo -e Still using: "\nfile://location$name" >> $logfile
gsettings set org.gnome.desktop.background picture-uri "file://$location$name"
gsettings set org.gnome.desktop.background picture-options "scaled"
fi
else
echo failed to read link. DlLink is empty >> $logfile
fi
echo -------------------------------------------------------------------------------- >> $logfile
#!/bin/bash
#Checks for folder existence
#Creates folder if it does not exist
function checkFolder
{
if [ -e "$1" ]; then
printf "Excellent - $1 directory already exists\n"
else
mkdir "$1"
if [ -e "$1" ]; then
printf "Excellent - $1 was created successfully\n"
else
print "Failed to create $1 - Aborting\n"
exit
fi
fi
}
function checkDependency
{
res="$(which $1)"
if [ "$res" != "" ]; then
printf "Excellent - $1 is already installed\n"
else
printf "Excellent - I get to install $1\n"
printf " --- --- \n"
sudo apt-get install $1
printf " --- --- \n"
fi
}
printf "Excellent - Checking for folders\n"
base=~/background
baseimg="$base/images/"
baselog="$base/log/"
basedump="$base/dump/"
cronTmp="$base/crontmp"
src="https://gist.githubusercontent.com/kalkun/8378148/raw"
dest="/usr/bin/background"
checkFolder $base
checkFolder $baseimg
checkFolder $baselog
checkFolder $basedump
checkDependency curl
checkDependency wget
printf "Excellent - Will now attempt to download background\n"
if sudo wget -O $dest $src; then
printf "Excellent - background collected!\n"
sudo chmod +x $dest
if sudo [ "crontab -e" ]; then
printf "Excellent - You have a crontab\n"
c=`crontab -l`
if [[ $c == *background* ]]; then
printf "Excellent - Your crontab already contains background\n"
else
printf "Excellent - I get to setup your crontab\n"
crontab -l > $cronTmp
echo "*/15 * * * * background" >> $cronTmp
crontab $cronTmp
rm $cronTmp
fi
else
printf "Failed to find your crontab\n"
fi
printf "Excellent - Executing background for the first time\n"
background
else
printf "Failed to download background\n"
fi
if [ $0 == "./installer.sh" ]; then
rm $0
else
printf "Failed to remove script\n"
fi
@Rainnwind
Copy link
Author

This version of getwallpaper is developed for Ubuntu
The easiest way to install is via the terminal by copying the following lines into your terminal

wget -O installer.sh https://gist.githubusercontent.com/Rainnwind/7c6e782078c894e46c49/raw/Installer &&
chmod +x installer.sh &&
./installer.sh

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