Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

up2pcn
2012 year 5 month 5 day Saturday 03:50:32 MDT 

  1. #!/usr/bin/env bash
  2. #
  3. #   up2pcn
  4. #   uploads pics to pics.cheggit.nl using curl
  5. #   version 0.2 + Dumppix & freeimage.us patch
  6. #   Copyright (c) 2009-2011 by upgrader
  7. #   This program is free software; you can redistribute it and/or modify
  8. #   it under the terms of the GNU General Public License as published by
  9. #   the Free Software Foundation; either version 2 of the License, or
  10. #   (at your option) any later version.
  11. #
  12. #   This program is distributed in the hope that it will be useful,
  13. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #   GNU General Public License for more details.
  16. #   You should have received a copy of the GNU General Public License
  17. #   along with this program; if not, write to the Free Software
  18. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. #   USA.
  20. #
  21. #   Changelog :
  22. #   * 2009/11/15 -    First version
  23. #   * 2011/06/15 -    Fix - vars in bash4 - thanks vrak
  24. #                     Fix - find trouble - thanks happypenguin
  25. #                     Fix - minor code changes
  26. #                     New - added -s flag for purists - thanks vrak
  27. #
  28. #   Dumppix patch (ishirog)
  29. #   * 2011/07/27 -    New - added -d flag to support dummpix.com
  30. #
  31. #   freeimage.us patch (ishirog)
  32. #   * 2012/02/01 -    New - added -e flag to support freeimage.us
  33. #
  34. #   ToDo :
  35. #   * Add alternative imagehost (this will be a shitload of work)
  36.  
  37. set -e
  38.  
  39. # config vars. don't touch if you don't know what you're doing!
  40. ALLOWED_FILES="GIF\|JPG\|PNG\|SWF"
  41. QUIET="-#"
  42.  
  43. DEFAULTPICHOST="http://pics.cheggit.nl"
  44. DEFAULTSUBMIT="-F action=Upload\ Image"
  45. DEFAULTMAXSIZE="2000k"
  46. DEFAULTMAXFILES="7"
  47. DEFAULTGREPOPT='\[url=\([^[:space:]]*\)\[/url\]'
  48.  
  49. DUMPPIXPICHOST="http://dumppix.com/upload.php"
  50. DUMPPIXSUBMIT=""
  51. DUMPPIXMAXSIZE="8000k"
  52. DUMPPIXMAXFILES="20"
  53. DUMPPIXGREPOPT='^\[url.*'
  54.  
  55. EMPPICHOST="http://xxx.freeimage.us/upload.php"
  56. EMPSUBMIT="-F typ=m"
  57. EMPMAXSIZE="8000k"
  58. EMPMAXFILES="7"
  59. EMPGREPOPT='\"\[url=http.*thumb\.php.*]'
  60.  
  61. PICHOST=${DEFAULTPICHOST}
  62. MAXSIZE=${DEFAULTMAXSIZE}
  63. MAXFILES=${DEFAULTMAXFILES}
  64. SUBMIT=${DEFAULTSUBMIT}
  65.  
  66. # regexps to format (standard) output
  67. SEDOPT='s/\[url=//g;s/\]\(.*\)//g'
  68.  
  69. # help and usage text
  70. usage() {
  71.         local P=$(basename $0)
  72.         cat <<EOF
  73. This script uploads pictures to pics.cheggit.nl using cURL.
  74.  
  75. Usage: $P [options] <file(s) or directory>
  76.  
  77. Options:
  78.   The script will always return the URLs of the pictures uploaded. If you're
  79.   fine with that, you won't have to bother with the options. If you prefer the
  80.   Quickpaste option, you might want to take a closer look here.
  81.   -b           BBCode: [img]yourfile[/img]
  82.   -h           Show this text.
  83.   -l           HTML version of -b
  84.   -q           Quickpaste BBCode (thumbnail with link to original) for each
  85.                file.
  86.   -t           HTML version of -q.
  87.   -s           Script talks too much? Try this.
  88.   -d           Upload to dumppix.com
  89.   -c           Upload to pics.cheggit.nl (default)
  90.   -e           Upload to freeimage.us
  91.  
  92.   Options will probably be extended in future versions, so if you're missing
  93.   something, suggest it!
  94.  
  95. Examples:
  96.     Upload a single image:
  97.         \$ $P mypicture.png
  98.  
  99.     Upload all supported image files in a directory:
  100.         \$ $P ~/mypics
  101.  
  102.     Upload all .jpg files in directory:
  103.         \$ $P *.jpg
  104.  
  105.     For more examples and help, go to the cheggit forums!
  106.     <http://cheggit.net/forum/read.php?29,98046>.
  107. EOF
  108. }
  109.  
  110. # This is where we start working :)
  111.  
  112. # First, let's see if we've got all programs we require
  113. for prog in curl grep sed mktemp; do
  114.         if ! type -pf "$prog" ; then
  115.                 ERROR="Required program $prog not found!"
  116.         fi >/dev/null
  117. done
  118. if [ -n "$ERROR" ]
  119. then
  120.         echo $ERROR
  121.         exit 1
  122. fi
  123.  
  124. # process options
  125. if [ $# -eq 0 ]
  126. then
  127.         usage
  128.         exit 1
  129. fi
  130.  
  131. while getopts ":bhlqtscde" Option
  132. do
  133.         case $Option in
  134.                 b )     FORMAT=bbcode ;;
  135.                 l )     FORMAT=html ;;
  136.                 q )     FORMAT=quickbbcd ;;
  137.                 t )     FORMAT=quickhtml ;;
  138.                 s )     QUIET="-s" ;;
  139.                 c )     PICHOST=${DEFAULTPICHOST}; MAXSIZE=${DEFAULTMAXSIZE}; MAXFILES=${DEFAULTMAXFILES}; SUBMIT=${DEFAULTSUBMIT};;
  140.                 d )     PICHOST=${DUMPPIXPICHOST}; MAXSIZE=${DUMPPIXMAXSIZE}; MAXFILES=${DUMPPIXMAXFILES}; SUBMIT=${DUMPPIXSUBMIT};;
  141.                 e )     PICHOST=${EMPPICHOST}; MAXSIZE=${EMPMAXSIZE}; MAXFILES=${EMPMAXFILES}; SUBMIT=${EMPSUBMIT};;
  142.                 h )     usage ; exit 0 ;;
  143.                 * )     usage ; exit 1 ;;
  144.         esac
  145. done
  146. shift $(( $OPTIND - 1 ))
  147.  
  148.  
  149. if [ "${PICHOST}"   = "${DEFAULTPICHOST}" ]; then
  150.   case $FORMAT in
  151.       bbcode )    SEDOPT='s/\[url=/\[img\]/g;s/\]\[img\]\(.*\)/\[\/img\]/g' ;; #'
  152.       html   )    SEDOPT='s/\[url=/<img src=\"/g;s/\]\[img\]\(.*\)/\" \/>/g' ;;
  153.       quickbbcd)        SEDOPT='/^#/d'  ;;
  154.       quickhtml)        SEDOPT='s/\[url=/<a href=\"/g;s/\]\[img\]/\"><img src="/g;s/\[\/img\]\[\/url\]/\" \/><\/a>/g' ;;
  155.   esac
  156.  
  157.   GREPOPT="${DEFAULTGREPOPT}"
  158. elif [ "${PICHOST}" = "${DUMPPIXPICHOST}" ]; then
  159.   case $FORMAT in
  160.       bbcode )    SEDOPT='s/\[url=.*\[img]/\[img]/;s/\[\/url]//;s/_thumb\./\./' ;;
  161.       html   )    SEDOPT='s/\[url=.*\[img]/\[img]/;s/\[\/url]//;s/\[img]/<img src=\"/;s/\[\/img]/\" \/>/;s/_thumb\./\./' ;;
  162.       quickbbcd)        SEDOPT='/^#/d'  ;;
  163.       quickhtml)        SEDOPT='s/\[url=/<a href=\"/g;s/\]\[img\]/\"><img src="/g;s/\[\/img\]\[\/url\]/\" \/><\/a>/g' ;;
  164.   esac
  165.  
  166.   GREPOPT="${DUMPPIXGREPOPT}"
  167. elif [ "${PICHOST}" = "${EMPPICHOST}" ]; then
  168.   case $FORMAT in
  169.       bbcode )    SEDOPT='s/.//;s/^[^]]*]//;s/\[\/url.*//;s/thumb\.php/image.php/' ;;
  170.       html   )    SEDOPT='s/.//;s/^[^]]*]//;s/\[\/url.*//;s/thumb\.php/image.php/;s/\[img]/<img src=\"/;s/\[\/img]/\" \/>/' ;;
  171.       quickbbcd)        SEDOPT='s/.//;s/.*value=.//;s/\".*//'   ;;
  172.       quickhtml)        SEDOPT='s/.//;s/.*value=.//;s/\".*//;s/\[url=/<a href=\"/;s/\]/\">/;s/\[img]/<img src=\"/;s/\[\/img]/\" \/>/;s/\[\/url]/<\/a>/' ;;
  173.       *)                SEDOPT='s/.//;s/.*\[url=//;s/\].*//;s/share.php/image.php/' ;;
  174.   esac
  175.  
  176.   GREPOPT="${EMPGREPOPT}"
  177. fi
  178.  
  179. # define maxdepth for find that makes LIST.
  180. # set to 0 unless a directory is given
  181. MAXDEPTH=0
  182. if [ -d "$1" ]
  183. then
  184.         MAXDEPTH=1
  185. fi
  186.  
  187. # we don't want ifs to fuck us with whitespaces
  188. OLDIFS=$IFS
  189. IFS=$'\n'
  190. # make list array of files to upload, get only files with max filesize and allowed filetypes
  191. LIST=($(find "$@" -maxdepth $MAXDEPTH -size -$MAXSIZE -type f | grep -i $ALLOWED_FILES))
  192. ListLen=${#LIST[@]}
  193. # reset ifs to old var
  194. IFS=$OLDIFS
  195.  
  196. # locate curl and create tempfile
  197. CURL=$(which curl)
  198. TMPFILE=$(mktemp /tmp/up2pcn.XXXXXXXXXX) || exit 1
  199. EMPFILE=$(mktemp /tmp/up2pcn.XXXXXXXXXX) || exit 1
  200.  
  201. Loops=$[$ListLen/$MAXFILES]
  202. if [ $[$ListLen%$MAXFILES] != 0 ]
  203. then
  204.         Loops=$[$Loops+1]
  205. fi
  206.  
  207. # let's upload some files!
  208. if [ $QUIET = "-#" ]
  209. then
  210.         echo "Uploading $ListLen file(s) in $Loops cycle(s)..."
  211. fi
  212. Counter=0
  213.  
  214. FString=""
  215.  
  216. for file in "${LIST[@]}"
  217. do
  218.         if [ $Counter -eq $MAXFILES ]
  219.         then
  220.                 `eval $CURL -4 ${QUIET} -H "Expect:" ${FString} ${SUBMIT} ${PICHOST} >> ${TMPFILE}`
  221.  
  222.                 Counter=0
  223.                 FString=""
  224.                 sleep 1 # give the server a second to relax ;)
  225.  
  226.                 if [ "${PICHOST}" = "${EMPPICHOST}" ]; then
  227.                     echo >> ${TMPFILE}
  228.                 fi
  229.         fi     
  230.  
  231.         Counter=$[$Counter+1]
  232.  
  233.         if [ "${PICHOST}" = "${DEFAULTPICHOST}" ]; then
  234.                 FString=$FString"-F image_file$Counter=\"@$file\" "
  235.         elif [ "${PICHOST}" = "${DUMPPIXPICHOST}" ]; then
  236.                 FString=$FString"-F userfile[$Counter]=\"@$file\" "
  237.         elif [ "${PICHOST}" = "${EMPPICHOST}" ]; then
  238.                 FString=$FString"-F f_$Counter=\"@$file\" "
  239.         fi
  240. done
  241.  
  242. # upload the files that haven't been uploaded already
  243. if [ "$FString" != "" ]
  244. then
  245.         `eval $CURL -4 ${QUIET} -H "Expect:" ${FString} ${SUBMIT} ${PICHOST} >> ${TMPFILE}`
  246.         if [ "${PICHOST}" = "${EMPPICHOST}" ]; then
  247.             echo >> ${TMPFILE}
  248.         fi
  249. fi
  250.  
  251. # let's create some nice output!
  252. if [ $QUIET = "-#" ]
  253. then
  254.         echo "Done! Cheggit!"
  255. fi
  256.  
  257. if [ "${PICHOST}" = "${EMPPICHOST}" ]; then
  258.     cat $TMPFILE | sed -e 's/.*URL=//' -e 's/.>//' | while read i; do curl -s "$i" ; done > ${EMPFILE}
  259.     mv ${EMPFILE} ${TMPFILE}
  260. fi
  261.  
  262. cat $TMPFILE | grep -o "$GREPOPT" | sed "$SEDOPT"
  263.  
  264. rm -rf $TMPFILE
  265. exit $?

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation