Algabe

imgur

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