No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imgurup 3.3KB

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