May 1, 2009

Easythumb Wrapper for BASH

I recently wrote a splash page for my website that emulates Mac OS X. When you click on a link in the dock, a little browser window displays a thumbnail.

After some investigation of various thumbnailing options, I settled on bluga.net’s webthumb. Specifically his easythumb service.

I wanted to pull in new thumbnails once per day, but I really didn’t want to deal with PHP, so I whipped up a simple shell script. This relies on BASH, cURL, MD5 and Perl.

Setup should be self explanatory. Failure and success is completely silent, so it’s perfect for use in a crontab.

#!/usr/local/bin/bash
# ---
# Easythumb Wrapper for BASH by timb. (http://timb.us)
#
# Version 1.0
#
# Depends on cURL and MD5.
# 
# A list of URLs to thumbnail. Fields are separated by a pipe. Each URL should be on a new line.
# !!! Do *NOT* include 'http://' at the start of a URL. !!!
# Format:
# URL|filename|size|cache
# Example:
# www.example1.com/foo.html|/home/user/web/foo_thumb.jpg|medium2|1
# test.example2.net/bar.php|/home/user/web/bar_thumb.jpg|large|7
thumbnail_list=/home/user/thumbnail_list
# User Number
user=
# API Key
apikey=
# Path to cURL
curl=/path/to/curl
# Path to MD5
md5=/path/to/md5
# ---
cat $thumbnail_list | while read thumbnail; do
	url=`echo $thumbnail | awk 'BEGIN { FS = "|" } ; { print $1 }'`
	filename=`echo $thumbnail | awk 'BEGIN { FS = "|" } ; { print $2 }'`
	size=`echo $thumbnail | awk 'BEGIN { FS = "|" } ; { print $3 }'`
	cache=`echo $thumbnail | awk 'BEGIN { FS = "|" } ; { print $4 }'`
	date=`date +%Y%m%d`
	urlencode=`echo -n "$url" | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'`
	hash=`$md5 -q -s "$date$url$apikey"`
	$curl --silent --fail -o $filename \
	"http://webthumb.bluga.net/easythumb.php?user=$user&url=$urlencode&hash=$hash&size=$size&cache=$cache"
done