Skip to content

Instantly share code, notes, and snippets.

@kalkun
Last active January 3, 2016 00:59
Show Gist options
  • Save kalkun/8386313 to your computer and use it in GitHub Desktop.
Save kalkun/8386313 to your computer and use it in GitHub Desktop.
getwallpapers modified to work on mac
#!/bin/bash
# MAC OSX >= 10.9.x (Maverick or newer)
#
# Syntax:
#
# getwallpaper [subreddit [new/rising/hour/day/week/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
location=~/Pictures/RWallpapers/
## logfile location:
logfile=~/tmp/log
## enable logfile:
log=true
stamp=$(date +%F%t%R )
tmp="Starting script getwallpapers at $stamp"
echo $tmp
dlstatus=$tmp
## choose one of these lines
if [ $# -eq 1 ]; then
tmp="r/$1 sorted by hot"
echo $tmp
dlstatus="$dlstatus\n$tmp"
link="http://www.reddit.com/r/$1"
ImGur=$(curl -Gs $link)
elif [ $# -eq 2 ]; then
if [ $1 = "search" ]; then
tmp="searching for $2"
echo $tmp
dlstatus="$dlstatus\n$tmp"
link="http://www.reddit.com/search?q=$2"
ImGur=$(curl -Gs $link)
elif [ $2 = "new" -o $2 = "rising" -o $2 = "controversial" ]; then
tmp="r/$1 sorted by $2"
echo $tmp
dlstatus="$dlstatus\n$tmp"
link="http://www.reddit.com/r/$1/$2"
ImGur=$(curl -Gs $link)
else
tmp="r/$1 sorted by $2"
echo $tmp
dlstatus="$dlstatus\n$tmp"
link="http://www.reddit.com/r/$1/top/"
ImGur=$(curl -Gs --data "sort=top&t=$2" $link)
fi
else
tmp="r/wallpapers sorted by hot"
echo $tmp
dlstatus="$dlstatus\n$tmp"
link="http://www.reddit.com/r/wallpapers"
ImGur=$(curl -Gs $link)
fi
## dump scraped reddit data:
## un-/comment the following line to enable/disable
## a dump of the html content from reddit
#echo $ImGur > ~/tmp/dump.html
tmp="Reddit link: $link"
echo $tmp
dlstatus="$dlstatus\n$tmp"
ImGur1=$(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 "^.*$")
ImGur2=$(echo -e $ImGur | egrep -o "http://imgur.com/[0-9a-zA-Z]*(/[0-9a-zA-Z])*" | egrep -m 1 ^.*$)
if [ -n "$ImGur1" ]; then
tmp="Found a direct image link"
echo "$tmp : $ImGur1"
dlstatus="$dlstatus\n$tmp"
DlLink=$ImGur1
elif [ -n "$ImGur2" ]; then
tmp="No direct link found, trying: $ImGur2"
echo $tmp
dlstatus="$dlstatus\n$tmp"
DlLink=$(curl -Gs $ImGur2 | egrep -o "<img.*>" | sed '/<img.*thumb.*>/ d' | egrep -o "i.imgur\.com.*\.[a-zA-Z]{1,4}" | egrep -m 1 ^.*$)
else
tmp="No image link found"
echo $tmp
dlstatus="$dlstatus\n$tmp"
fi
# 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
tmp="Downloading from: \nhttp://$DlLink"
echo -e $tmp
dlstatus="$dlstatus\n$tmp"
curl "http://$DlLink" -o $location$name
tmp="using:\n$location$name"
echo -e $tmp
dlstatus="$dlstatus\n$tmp"
else
tmp="File already downloaded once"
tmp="$tmp\nStill using file:"
tmp="$tmp\n$location$name"
echo -e $tmp
dlstatus="$dlstatus\n$tmp"
fi
#sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$location$name'";
#killall Dock;
osascript -e "tell application \"System Events\" to set picture of every desktop to \"$location$name\""
else
tmp="Download link was empty, no images could be found"
echo $tmp
dlstatus="$dlstatus\n$tmp"
fi
if [ $log ]; then
echo -e $dlstatus >> $logfile
echo "--------------------------------------------------------------------------------" >> $logfile
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment