Skip to content

Instantly share code, notes, and snippets.

@robotamer
Created January 13, 2015 01:28
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 robotamer/f41d21f69db938a7152d to your computer and use it in GitHub Desktop.
Save robotamer/f41d21f69db938a7152d to your computer and use it in GitHub Desktop.
Randomly picks one of the amazing photos at http://imgur.com/r/earthporn
#!/bin/bash
# Usage: earthporn [+format] [-time] [logfile]
# Dependencies: bash, wget, mktemp... and well, date and sleep
imgfile=~/.earthporn
# Help and version functions
help () {
echo "Randomly picks one of the amazing photos at http://imgur.com/r/earthporn"
echo "and sets it as the background image."
echo
echo "Usage: $0 [+format] [-time] [logfile] [-]"
echo " $0 {-h|-?|--help}"
echo " $0 {-v|-V|--version}"
echo
echo "Format string:"
echo " %d Timestamp"
echo " %c/%C Caption (normal / without '[...]')"
echo " %u/%U URL (page / image)"
echo " %f Local wallpaper file ($imgfile)"
echo " %t/%n Tab / newline"
echo " %. A literal '%'"
exit 0
}
version () {
echo "earthporn script v0.1" # couldn't think of anything better
exit 0
}
# Function to change/refresh the wallpaper, if needed
environ=''
get_environ () {
if [ gnome = "$DESKTOP_SESSION" ]; then # !!TODO!! gnome-fallback
environ=gnome
case "$(gnome-session --version)" in
'gnome-session 2'*) gconftool-2 --type=string --set \
/desktop/gnome/background/picture_filename "$imgfile" ;;
'gnome-session 3'*) gsettings set org.gnome.desktop.background \
picture-uri file://"$imgfile" ;; # UNTESTED!
esac
elif [ kde = "$DESKTOP_SESSION" ]; then
environ=kde
: ##!!TODO!!##
elif [ LXDE = "$XDG_CURRENT_DESKTOP" ]; then
environ=lxde
#~ elif [ something_xfce ]; then
#~ environ=xfce
#~ : ##!!TODO!!##
else
environ='-'
echo "Please set your wallpaper as $imgfile" >&2
fi
}
refresh_background () {
[ -z "$environ" ] && get_environ
case "$environ" in
gnome) touch "$imgfile" ;;
#~ kde) : ;; ##!!TODO!!##
lxde) pcmanfm -w "$imgfile" ;;
#~ xfce) : ;; ##!!TODO!!##
*) echo "New wallpaper available" >&2 ;;
esac
}
# Parse command line options
time=''
format=''
logfile=''
for arg; do
case "$arg" in
-h|--help|'-?') help ;;
-v|-V|--version) version ;;
-*[!0-9.smhd]*) echo 'Invalid option `'"$arg'" >&2; exit 1 ;;
-) format='%U'; imgfile='' ;;
-*) time="${arg#-}" ;;
+*) format="${arg#+}"; format="${format//\%\%/%.}" ;;
*) logfile="$arg" ;;
esac
done
# Main loop, to be executed multiple times iff there's a -<time> option
while true; do
images=()
while read -r line; do
[[ $line = *'<img alt="" src="'*'" title="'* ]] && images+=("$line")
done < <( wget -qO- http://imgur.com/r/earthporn/top/week )
if [[ ${#images[@]} = 0 ]]; then
echo 'Download error! (no images found)' >&2
exit 2
# !!TODO!! something more elegant if in loop mode
fi
image="${images[RANDOM % ${#images[@]}]}"
# Get image URL
imurl="${image#*src=\"}"
imurl="${imurl%%\"*}"
imurl="${imurl/b./.}"
# Download image
if [ -n "$imgfile" ]; then
tmpfile="$(mktemp)"
if wget "$imurl" -q -O "$tmpfile"; then
mv "$tmpfile" "$imgfile"
refresh_background
else
echo 'Download error! (image download failed)' >&2
rm "$tmpfile"
exit 2
# !!TODO!! something more elegant if in loop mode
fi
fi
# Get metadata
if [ -n "$logfile" -o -n "$format" ]; then
# Get image page
pageurl="${imurl%.*}"
pageurl="http://imgur.com/r/earthporn/${pageurl##*/}"
# Get photo description (title; caption)
caption="${image#*title=\"}"
caption="${caption%%<p>*}"
# Get timestamp
timestamp="$(date +%F,%T 2>/dev/null || echo '????-??-??,??:??:??')"
fi
# Write to logfile
[ -n "$logfile" ] && echo "$timestamp $pageurl $caption" >> "$logfile"
# Print info
if [ -n "$format" ]; then
caption="${caption//\%/%.}"
caption_stripped="${caption/\[*\]/}"
print="$format"
print="${print//\%d/$timestamp}"
print="${print//\%c/$caption}"
print="${print//\%C/$caption_stripped}"
print="${print//\%u/$pageurl}"
print="${print//\%U/$imurl}"
print="${print//\%f/$imgfile}"
print="${print//\%t/$'\t'}"
print="${print//\%n/$'\n'}"
print="${print//\%./%}"
echo "$print"
fi
[ -z "$time" ] && exit 0 # if no time specified, do stuff once and exit
sleep "$time" || exit 1 # time might be wrong. Better to just exit then.
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment