2009-10-29

imgur

It's a bit like sprunge -- a pastebin for images. I wrote a bash script similar to my sprunge one to upload an image (given by filename) and return (and put on the clipboard) its new URL. The URL to delete it is given on stderr.

#!/bin/bash
apikey="<API KEY>"
if [ $# -lt 1 ]; then
 echo "no file specified" >&2
 exit 127
fi
response=$(curl -F "key=$apikey" -F "image=@$1" http://imgur.com/api/upload.xml 2>/dev/null)
if [ $? -ne 0 ]; then
 echo "request failed" >&2
 exit 1
fi
url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
echo $url
echo "Delete page: $deleteurl" >&2
if [ $DISPLAY ]; then
 { which xsel >/dev/null 2>/dev/null && echo -n $url | xsel; } \
  || { which xclip >/dev/null 2>/dev/null && echo -n $url | xclip; } \
  || echo "haven't copied to the clipboard: no xsel or xclip" >&2
else
 echo "haven't copied to the clipboard: no \$DISPLAY" >&2
fi

Update: a later version of this script is now featured on the imgur site. At the moment it's at the bottom of the tools page.

No comments:

Post a Comment