Skip to content

Instantly share code, notes, and snippets.

@muddermanden
Forked from kalkun/getwallpaper [MAC]
Created May 23, 2014 07:04
Show Gist options
  • Save muddermanden/6013eff785468e93a53b to your computer and use it in GitHub Desktop.
Save muddermanden/6013eff785468e93a53b to your computer and use it in GitHub Desktop.
#!/bin/bash
# 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/
## change this to change the location of the logfile (disabled by default)
#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
# 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
curl "http://$DlLink" -o $location$name
echo -e \'tell application \"Finder\" to set desktop picture to file \"$location$name\" as POSIX file\' | xargs osascript -e
#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
echo -e \'tell application \"Finder\" to set desktop picture to file \"$location$name\" as POSIX file\' | xargs osascript -e
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