Advertisement
Guest User

mememaker main script

a guest
Mar 20th, 2012
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.34 KB | None | 0 0
  1. #!/bin/bash
  2. interactive=0
  3. noupload=0
  4. function imgur {
  5. # imgur script by Bart Nagel <bart@tremby.net>
  6. # version 2
  7. # I release this as public domain. Do with it what you will.
  8.  
  9. # Required: curl
  10. #
  11. # Instructions:
  12. # Put it somewhere in your path and maybe rename it:
  13. #   mv ~/Downloads/imgur.sh ~/bin/imgur
  14. # Make it executable:
  15. #   chmod +x ~/bin/imgur
  16. # Stick your API key in the top:
  17. #   vim ~/bin/imgur
  18. # Upload an image:
  19. #   imgur images/hilarious/manfallingover.jpg
  20. # The URL will be displayed (and the delete page's URL will be displayed on
  21. # stderr). If you have xsel or xclip the URL will also be put on the X
  22. # selection, which you can usually paste with a middle click.
  23.  
  24. # API Key provided by Alan@imgur.com
  25. apikey="b3625162d3418ac51a9ee805b1840452"
  26.  
  27. # function to output usage instructions
  28. function usage {
  29.     echo "Usage: $(basename $0) <filename>
  30. Upload an image to imgur and output its new URL to stdout. Its delete page is
  31. output to stderr.
  32. If xsel or xclip is available, the URL is put on the X selection for easy
  33. pasting." >&2
  34. }
  35.  
  36. # check API key has been entered
  37. if [ "$apikey" = "Your API key" ]; then
  38.     echo "You first need to edit the script and put your API key in the variable near the top." >&2
  39.     exit 15
  40. fi
  41.  
  42. # check arguments
  43. if [ "$1" = "-h" -o "$1" = "--help" ]; then
  44.     usage
  45.     exit 0
  46. elif [ $# -ne 1 ]; then
  47.     if [ $# == 0 ]; then
  48.         echo "No file specified" >&2
  49.     else
  50.         echo "Unexpected arguments" >&2
  51.     fi
  52.     usage
  53.     exit 16
  54. elif [ ! -f "$1" ]; then
  55.     echo "File \"$1\" not found" >&2
  56.     exit 1
  57. fi
  58.  
  59. # check curl is available
  60. which curl >/dev/null 2>/dev/null || {
  61.     echo "Couln't find curl, which is required." >&2
  62.     exit 17
  63. }
  64.  
  65. # upload the image
  66. response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$1" \
  67.     http://imgur.com/api/upload.xml 2>/dev/null)
  68. # the "Expect: " header is to get around a problem when using this through the
  69. # Squid proxy. Not sure if it's a Squid bug or what.
  70. if [ $? -ne 0 ]; then
  71.     echo "Upload failed" >&2
  72.     exit 2
  73. elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
  74.     echo "Error message from imgur:" >&2
  75.     echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2
  76.     exit 3
  77. fi
  78.  
  79. # parse the response and output our stuff
  80. url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
  81. deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
  82. if [[ "$interactive" == "1" ]]
  83. then
  84. echo
  85. echo Imgur link:
  86. echo $url
  87. echo
  88. echo Delete: $deleteurl
  89. echo All delete urls are saved in ~/deleteurls.txt
  90. else
  91. echo $url
  92. fi
  93. echo "$url $deleteurl" >> ~/deleteurls.txt
  94. }
  95. function creatememe (){
  96.     if [[ "$interactive" == "1" ]]
  97.     then
  98.         echo Press enter to skip a caption.
  99.         if [[ -z $top ]]; then
  100.             echo -n Top caption:
  101.             read top
  102.         fi
  103.         if [[ -z $bottom ]]; then
  104.             echo -n Bottom caption:
  105.             read bottom
  106.         fi
  107.     fi
  108.     width=`identify -format %w meme.jpg`
  109.     convert -background '#0008' -fill white -gravity center -size ${width}x30 \
  110.               caption:"$bottom" \
  111.               meme.jpg +swap -gravity south -composite  out.png
  112.     convert -background '#0008' -fill white -gravity center -size ${width}x30 \
  113.               caption:"$top" \
  114.               out.png +swap -gravity north -composite  out.png
  115.     if [[ "$noupload" == "1" ]]
  116.     then
  117.         cp out.png "$file"
  118.     else
  119.         imgur out.png
  120.     fi
  121.     rm out.png
  122.     rm meme.jpg
  123. }
  124. function checkarg(){
  125.     if [[ "${1:0:1}" == "-" || $1 = "" ]]; then
  126.         cont=0;
  127.     else
  128.         cont=1;
  129.     fi
  130. }
  131. function memeselect {
  132.                 if [[ "$meme" == "split" ]]
  133.                 then
  134.                 echo Please select Top Meme
  135.                 echo -n meme1 \#:
  136.                 read meme1
  137.                 echo
  138.                 echo Please select bottomMeme
  139.                 echo -n meme2 \#:
  140.                 read meme2
  141.                 convert -crop 100%x50% ./memes/$meme1.jpg "$meme1"%d.jpg
  142.                 convert -crop 100%x50% ./memes/$meme2.jpg "$meme2"%d.jpg
  143.                 #montage -mode concatenate +adjoin -tile 1x2 "$meme1"0.jpg "$meme2"1.jpg meme.jpg
  144.                 convert -append -adaptive-resize "480x480" "$meme1"0.jpg "$meme2"1.jpg meme.jpg
  145.                 rm  "$meme1"0.jpg "$meme2"1.jpg  "$meme1"1.jpg "$meme2"0.jpg
  146.                 else
  147.                 cp ./memes/$meme.jpg ./meme.jpg
  148.                 fi
  149. }
  150. cd `dirname $0`
  151. while :
  152. do
  153.     case $1 in
  154.         -h | --help)
  155.             echo -e "-h --help          display this message"
  156.             echo -e "-s --split #1 #2   create a split meme with #1 on top and #2 on bottom"
  157.             echo -e "-m --meme #1 #2    create a meme with #1 on top and #2 on bottom"
  158.             echo -e "-l --list      list all the avalable memes"
  159.             echo -e "-t --top <text>    top text"
  160.             echo -e "-b --bottom <text> bottom text"
  161.             echo -e "-i --interactive   run in interactive mode"
  162.             echo -e "-f --file <file>   output to <file>, don't upload."
  163.             echo
  164.             echo Examples:
  165.             echo Make a meme with top text and bottom text and upload.
  166.             echo mememaker.sh -m 1 -t \"top text" -b \"bottom text"
  167.             echo
  168.             echo Make a split meme with top text and bottom text and upload.
  169.             echo mememaker.sh -s 1 2 -t \"top text" -b \"bottom text"
  170.             echo
  171.             echo Make a split meme with top text and bottom text and save it as file.png.
  172.             echo mememaker.sh -s 1 2 -t \"top text" -b \"bottom text" -f \"file.png\"
  173.             echo
  174.             echo Make a meme in interactive mode.
  175.             echo mememaker.sh -i
  176.             echo
  177.             echo Mkae a meme in interactive mode and save it as file.png.
  178.             echo mememaker.sh -i -f \"file.png\"
  179.             echo
  180.             echo Tips:
  181.             echo imgur delete links are in ~/deleteurls.txt
  182.             echo You don\'t need to specify both captions, you can skip one.
  183.             exit 0
  184.             ;;
  185.         -l | --list)
  186.             cat ./list.txt
  187.             exit 0;;
  188.         -s | --split)
  189.             if [[ -n $2 ]];then
  190.                 convert -crop 100%x50% ./memes/$2.jpg "$2"%d.jpg
  191.                 convert -crop 100%x50% ./memes/$3.jpg "$3"%d.jpg
  192.                 convert -append -adaptive-resize "480x480" "$2"0.jpg "$3"1.jpg meme.jpg
  193.                 rm  "$2"0.jpg "$2"1.jpg  "$3"1.jpg "$3"0.jpg
  194.                 shift 3
  195.             else
  196.                 echo "Check your arguments. --help for help. -i for interactive." >&2
  197.                 exit 0
  198.             fi;;
  199.         -m | --meme)
  200.             if [[ -n $2 ]];then
  201.                 cp ./memes/$2.jpg ./meme.jpg
  202.                 shift 2
  203.             else
  204.                 echo "Check your arguments. --help for help. -i for interactive." >&2
  205.                 exit 0
  206.             fi;;
  207.         -t | --top)
  208.             shift
  209.             top=$1
  210.             shift
  211.             checkarg
  212.             while [[ $cont -eq 1 ]]; do
  213.                 top="$top $1"
  214.                 shift
  215.                 checkarg
  216.             done
  217.             cont=1;;
  218.         -b | --bottom)
  219.             shift
  220.             bottom=$1
  221.             shift
  222.             checkarg
  223.             while [[ $cont -eq 1 ]]; do
  224.                 bottom="$bottom $1"
  225.                 shift
  226.                 checkarg
  227.             done
  228.             cont=1;;
  229.         -i | --interactive)
  230.             if [[ -n $2 ]] && [[ "$2" != "-"* ]]
  231.             then
  232.                 echo "Check your arguments. --help for help. -i for interactive." >&2
  233.                 exit 0
  234.             else
  235.                 cat ./list.txt
  236.                 echo
  237.                 echo split: Create a split meme.
  238.                 echo
  239.                 echo Please select a meme.
  240.                 echo -n meme \#:
  241.                 read meme
  242.                 echo
  243.                 memeselect
  244.                 interactive=1
  245.                 shift
  246.             fi;;
  247.         -f | --file)
  248.             if [[ -n $2 ]];then
  249.                 shift
  250.                 noupload=1
  251.                 file="$1"
  252.                 shift
  253.                 checkarg
  254.                 while [[ $cont -eq 1 ]]; do
  255.                     file="$file $1"
  256.                     shift
  257.                     checkarg
  258.                 done
  259.                 if [[ -e "$file" ]]
  260.                 then
  261.                     echo "Check your arguments. $file exists." >&2
  262.                     exit 0
  263.                 fi
  264.                 cont=1
  265.             else
  266.                 echo "Check your arguments. --help for help. -i for interactive." >&2
  267.                 exit 0
  268.             fi
  269.             ;;
  270.         --)
  271.             shift
  272.             break
  273.             ;;
  274.         -*)
  275.             echo "Check your arguments. $1 is unknown." >&2
  276.             break
  277.             ;;
  278.         *)
  279.             break
  280.             ;;
  281.     esac
  282. done
  283. [[ -f ./meme.jpg ]] && creatememe || echo "Check your arguments. --help for help. -i for interactive."
  284. # vim:set ft=sh:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement