|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# imgur script by Bart Nagel <[email protected]> |
| 4 | +# version 4 |
| 5 | +# I release this into the public domain. Do with it what you will. |
| 6 | + |
| 7 | +# version 4a |
| 8 | +# Updated by J. Roberts <[email protected]> |
| 9 | +# Upload URL changed, output HTML changed |
| 10 | +# Output changed to one line |
| 11 | + |
| 12 | +# Required: curl |
| 13 | +# |
| 14 | +# Optional: xsel or xclip for automatically putting the URLs on the X selection |
| 15 | +# for easy pasting |
| 16 | +# |
| 17 | +# Instructions: |
| 18 | +# Put it somewhere in your path and maybe rename it: |
| 19 | +# mv ~/Downloads/imgurbash.sh ~/bin/imgur |
| 20 | +# Make it executable: |
| 21 | +# chmod +x ~/bin/imgur |
| 22 | +# Optional, since Alan kindly provided an API key for this script: stick your |
| 23 | +# API key in the top: |
| 24 | +# vim ~/bin/imgur |
| 25 | +# Upload an image: |
| 26 | +# imgur images/hilarious/manfallingover.jpg |
| 27 | +# Upload multiple images: |
| 28 | +# imgur images/delicious/cake.png images/exciting/bungeejump.jpg |
| 29 | +# The URLs will be displayed (and the delete page's URLs will be displayed on |
| 30 | +# stderr). If you have xsel or xclip the URLs will also be put on the X |
| 31 | +# selection, which you can usually paste with a middle click. |
| 32 | + |
| 33 | +# API Key provided by [email protected] |
| 34 | +apikey="b3625162d3418ac51a9ee805b1840452" |
| 35 | + |
| 36 | +# function to output usage instructions |
| 37 | +function usage { |
| 38 | + echo "Usage: $(basename $0) <filename> [<filename> [...]]" >&2 |
| 39 | + echo "Upload images to imgur and output their new URLs to stdout. Each one's" >&2 |
| 40 | + echo "delete page is output to stderr between the view URLs." >&2 |
| 41 | + echo "If xsel or xclip is available, the URLs are put on the X selection for" >&2 |
| 42 | + echo "easy pasting." >&2 |
| 43 | +} |
| 44 | + |
| 45 | +# check API key has been entered |
| 46 | +if [ "$apikey" = "Your API key" ]; then |
| 47 | + echo "You first need to edit the script and put your API key in the variable near the top." >&2 |
| 48 | + exit 15 |
| 49 | +fi |
| 50 | + |
| 51 | +# check arguments |
| 52 | +if [ "$1" = "-h" -o "$1" = "--help" ]; then |
| 53 | + usage |
| 54 | + exit 0 |
| 55 | +elif [ $# == 0 ]; then |
| 56 | + echo "No file specified" >&2 |
| 57 | + usage |
| 58 | + exit 16 |
| 59 | +fi |
| 60 | + |
| 61 | +# check curl is available |
| 62 | +type curl >/dev/null 2>/dev/null || { |
| 63 | + echo "Couln't find curl, which is required." >&2 |
| 64 | + exit 17 |
| 65 | +} |
| 66 | + |
| 67 | +clip="" |
| 68 | +errors=false |
| 69 | + |
| 70 | +# loop through arguments |
| 71 | +while [ $# -gt 0 ]; do |
| 72 | + file="$1" |
| 73 | + shift |
| 74 | + |
| 75 | + # check file exists |
| 76 | + if [ ! -f "$file" ]; then |
| 77 | + echo "file '$file' doesn't exist, skipping" >&2 |
| 78 | + errors=true |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
| 82 | + # upload the image |
| 83 | + response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$file" \ |
| 84 | + http://api.imgur.com/2/upload 2>/dev/null) |
| 85 | + # the "Expect: " header is to get around a problem when using this through |
| 86 | + # the Squid proxy. Not sure if it's a Squid bug or what. |
| 87 | + if [ $? -ne 0 ]; then |
| 88 | + echo "Upload failed" >&2 |
| 89 | + errors=true |
| 90 | + continue |
| 91 | + elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then |
| 92 | + echo "Error message from imgur:" >&2 |
| 93 | + echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2 |
| 94 | + errors=true |
| 95 | + continue |
| 96 | + fi |
| 97 | + |
| 98 | + # parse the response and output our stuff |
| 99 | + url=$(echo $response | sed -r 's/.*<original>(.*)<\/original>.*/\1/') |
| 100 | + deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/') |
| 101 | + echo $url $deleteurl |
| 102 | + |
| 103 | + # append the URL to a string so we can put them all on the clipboard later |
| 104 | + clip="$clip$url |
| 105 | +" |
| 106 | +done |
| 107 | + |
| 108 | +# put the URLs on the clipboard if we have xsel or xclip |
| 109 | +if [ $DISPLAY ]; then |
| 110 | + { type xsel >/dev/null 2>/dev/null && echo -n $clip | xsel; } \ |
| 111 | + || { type xclip >/dev/null 2>/dev/null && echo -n $clip | xclip; } \ |
| 112 | + || echo "Haven't copied to the clipboard: no xsel or xclip" >&2 |
| 113 | +else |
| 114 | + echo "Haven't copied to the clipboard: no \$DISPLAY" >&2 |
| 115 | +fi |
| 116 | + |
| 117 | +if $errors; then |
| 118 | + exit 1 |
| 119 | +fi |
0 commit comments