Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 9th, 2012  |  syntax: Bash  |  size: 3.14 KB  |  views: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. # imgur script by Bart Nagel <bart@tremby.net>
  4. # modified by haarp
  5. # version 3
  6. # I release this as public domain. Do with it what you will.
  7.  
  8. # Required: curl
  9. #
  10. # Optional: xsel or xclip for automatically putting the URL on the X clipboard 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 and the delete page's URL will be displayed. If you have xsel or xclip the URL
  22. # will also be put on the X clipboard, which you can usually paste with a middle click.
  23.  
  24. # function to have zenity behave like echo
  25. function echozenity {
  26.         zenity --info --no-wrap --text="$1"
  27. }
  28.  
  29. # If we're running on from a file browser use zenity, otherwise echo
  30. if [ $TERM = linux ]; then
  31.         echo="echozenity"
  32. else
  33.         echo="echo"
  34. fi
  35.  
  36. # API Key provided by Alan@imgur.com
  37. apikey="b3625162d3418ac51a9ee805b1840452"
  38.  
  39. # check API key has been entered
  40. if [ "$apikey" = "Your API key" ]; then
  41.         $echo "You first need to edit the script and put your API key in the variable near the top." >&2
  42.         exit 15
  43. fi
  44.  
  45. # check arguments (zenity doesn't like <> )
  46. usage="Usage: $(basename $0) [filename]
  47. Upload an image to imgur and output its new URL and its delete page.
  48. If xsel or xclip is available, the URL is put on the X clipboard."
  49.  
  50. if [ "$1" = "-h" -o "$1" = "--help" ]; then
  51.         $echo "$usage"
  52.         exit 0
  53. elif [ $# -ne 1 ]; then
  54.         if [ $# == 0 ]; then
  55.                 $echo "No file specified.
  56. $usage" >&2
  57.         else
  58.                 $echo "Unexpected arguments.
  59. $usage" >&2
  60.         fi
  61.         exit 16
  62. elif [ ! -f "$1" ]; then
  63.         $echo "File \"$1\" not found." >&2
  64.         exit 1
  65. fi
  66.  
  67. # check curl is available
  68. which curl >/dev/null 2>/dev/null || {
  69.         $echo "Couln't find curl, which is required." >&2
  70.         exit 17
  71. }
  72.  
  73. # upload the image
  74. response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$1" \
  75.         http://imgur.com/api/upload.xml 2>/dev/null)
  76. # the "Expect: " header is to get around a problem when using this through the
  77. # Squid proxy. Not sure if it's a Squid bug or what.
  78. if [ $? -ne 0 ]; then
  79.         $echo "Upload failed!" >&2
  80.         exit 2
  81. elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
  82.         $echo "Error message from imgur:
  83. $(echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/')" >&2
  84.         exit 3
  85. fi
  86.  
  87. # parse the response
  88. url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
  89. deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
  90.  
  91. # put the URL on the clipboard if we have xsel or xclip
  92. if [ $DISPLAY ]; then
  93.         { which xsel >/dev/null 2>/dev/null && echo -n $url | xsel && clip="The image URL has been put into the clipboard."; } \
  94.         || { which xclip >/dev/null 2>/dev/null && echo -n $url | xclip -selection clipboard && clip="The image URL has been put into the clipboard."; } \
  95.         || $echo "Haven't copied to the clipboard: no xsel or xclip" >&2 || clip=""
  96. else
  97.         $echo "Haven't copied to the clipboard: no \$DISPLAY" >&2
  98.         clip=""
  99. fi
  100.  
  101. # output our stuff
  102. $echo "$url
  103. Delete page: $deleteurl
  104. $clip"
clone this paste RAW Paste Data