imgurbash.sh (2790B)
1 #!/bin/bash 2 3 # imgur script by Bart Nagel <bart@tremby.net> 4 # version 2 5 # I release this as public domain. Do with it what you will. 6 7 # Required: curl 8 # 9 # Optional: xsel or xclip for automatically putting the URL on the X selection 10 # for easy pasting 11 # 12 # Instructions: 13 # Put it somewhere in your path and maybe rename it: 14 # mv ~/Downloads/imgur.sh ~/bin/imgur 15 # Make it executable: 16 # chmod +x ~/bin/imgur 17 # Stick your API key in the top: 18 # vim ~/bin/imgur 19 # Upload an image: 20 # imgur images/hilarious/manfallingover.jpg 21 # The URL will be displayed (and the delete page's URL will be displayed on 22 # stderr). If you have xsel or xclip the URL will also be put on the X 23 # selection, which you can usually paste with a middle click. 24 25 # API Key provided by Alan@imgur.com 26 apikey="b3625162d3418ac51a9ee805b1840452" 27 28 # function to output usage instructions 29 function usage { 30 echo "Usage: $(basename $0) <filename> 31 Upload an image to imgur and output its new URL to stdout. Its delete page is 32 output to stderr. 33 If xsel or xclip is available, the URL is put on the X selection for easy 34 pasting." >&2 35 } 36 37 # check API key has been entered 38 if [ "$apikey" = "Your API key" ]; then 39 echo "You first need to edit the script and put your API key in the variable near the top." >&2 40 exit 15 41 fi 42 43 # check arguments 44 if [ "$1" = "-h" -o "$1" = "--help" ]; then 45 usage 46 exit 0 47 elif [ $# -ne 1 ]; then 48 if [ $# == 0 ]; then 49 echo "No file specified" >&2 50 else 51 echo "Unexpected arguments" >&2 52 fi 53 usage 54 exit 16 55 elif [ ! -f "$1" ]; then 56 echo "File \"$1\" not found" >&2 57 exit 1 58 fi 59 60 # check curl is available 61 which curl >/dev/null 2>/dev/null || { 62 echo "Couln't find curl, which is required." >&2 63 exit 17 64 } 65 66 # upload the image 67 response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$1" \ 68 http://imgur.com/api/upload.xml 2>/dev/null) 69 # the "Expect: " header is to get around a problem when using this through the 70 # Squid proxy. Not sure if it's a Squid bug or what. 71 if [ $? -ne 0 ]; then 72 echo "Upload failed" >&2 73 exit 2 74 elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then 75 echo "Error message from imgur:" >&2 76 echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2 77 exit 3 78 fi 79 80 # parse the response and output our stuff 81 url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/') 82 deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/') 83 echo $url 84 echo "Delete page: $deleteurl" >&2 85 86 # put the URL on the clipboard if we have xsel or xclip 87 if [ $DISPLAY ]; then 88 { which xsel >/dev/null 2>/dev/null && echo -n $url | xsel; } \ 89 || { which xclip >/dev/null 2>/dev/null && echo -n $url | xclip; } \ 90 || echo "Haven't copied to the clipboard: no xsel or xclip" >&2 91 else 92 echo "Haven't copied to the clipboard: no \$DISPLAY" >&2 93 fi