Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tmbo command-line uploader
#!/bin/sh
## tmbo.sh -- my attempt at posting from the command line
## tmbo: lnk, twitter: @oogali
##
BASE_URL=thismight.be/offensive
TMBO_CONFIG=${HOME}/.tmbo
TMPPATH=/tmp
usage() {
echo "$0 <options> <image to upload> [filename]"
echo -e " -f\tspecify the path to a different configuration file (default: ${HOME}/.tmbo)"
echo
echo "tags:"
#echo -e " -c\t[cc], attach a comment with this upload [NOT WORKING YET]"
echo -e " -m\t[mp], maxxer post"
echo -e " -n\t[nsfw], not safe for work"
echo -e " -r\t[tiar], this is a repost"
echo -e " -s\t[scroller], scrolllllllller"
echo -e " -t\t[tmbo], this might be offensive"
exit 1
}
create_tmpfile() {
tmpfile=`mktemp ${TMPPATH}/tmbo.XXXXXXXX`
if [ -z "${tmpfile}" ] || [ ! -f "${tmpfile}" ]; then
echo "$0: could not create temporary file"
cleanup_and_exit 1
fi
return 0
}
cleanup_and_exit() {
rm -f ${cookie_jar} ${srv_output} ${downloaded}
echo "${comment}" | grep -q "^${TMPPATH}/"
if [ $? -eq 0 ]; then
rm -f ${comment}
fi
if [ $# -eq 2 ]; then
echo "$0: $2"
fi
exit $1
}
do_curl() {
${CURL} ${CURL_ARGS} "$@"
if [ $? -ne 0 ]; then
echo "$0: could not connect to remote url"
fi
return $?
}
get_uploaded_url() {
url=`grep 'Your upload can be viewed' ${srv_output} | sed 's/.*href="\.\/\(.*\)">here.*/\1/g'`
if [ -z "${url}" ]; then
cleanup_and_exit 1 "your upload failed for some reason, i dunno."
fi
url="http://${BASE_URL}/${url}"
echo "${url}"
return 0
}
## is curl installed?
CURL=`which curl 2>/dev/null`
if [ -z "${CURL}" ]; then
echo "$0: curl is not installed"
exit 1
fi
CURL_ARGS="-s -f"
## is file installed?
FILE=`which file 2>/dev/null`
if [ -z "${FILE}" ]; then
echo "$0: file is not installed"
exit 1
fi
## check if we're on OS X, different arg for mime type
uname -a | grep -q 'Darwin' && FILE_ARGS="-bI" || FILE_ARGS="-bi"
## set skip args for 'which', based on platform
uname -a | grep -q 'Linux' && WHICH_ARGS="--skip-alias" || WHICH_ARGS=""
## parse command line arguments
while getopts "cf:mnrst" o ; do
case ${o} in
c)
tags="${tags}[cc]"
comment=1
;;
f)
TMBO_CONFIG=${OPTARG}
;;
m)
tags="${tags}[mp]"
;;
n)
nsfw=1
;;
r)
tags="${tags}[tiar]"
;;
s)
tags="${tags}[scroller]"
;;
t)
tmbo=1
;;
*)
usage
;;
esac
done
## discard parsed options and check argument count
shift $((${OPTIND} - 1))
if [ $# -lt 1 ]; then
usage
fi
## check to see if our configuration file exists
if [ ! -f "${TMBO_CONFIG}" ]; then
echo "$0: no configuration file found"
exit 1
fi
## read our config
. ${TMBO_CONFIG}
## is our username and password set?
if [ -z "${TMBO_USERNAME}" ] || [ -z "${TMBO_PASSWORD}" ]; then
echo "$0: missing username or password. check configuration file"
exit 1
fi
## install our signal handler for SIGINT, SIGTERM
trap 'cleanup_and_exit 1 "interrupted"' INT TERM
## store our image
image=${1}
## is our image a remote url? if so, download it
echo "${image}" | grep "^http[s]*://"
if [ $? -eq 0 ]; then
## create temporary file and download image
create_tmpfile
downloaded=${tmpfile}
do_curl -o ${downloaded} ${image} || cleanup_and_exit 1
## check mime type to make sure this is an image
${FILE} ${FILE_ARGS} ${downloaded} | grep -q '^image/'
if [ $? -ne 0 ]; then
cleanup_and_exit 1 "downloaded file is not a valid image, try again."
fi
## store our downloaded image name for later
image=${downloaded}
else
if [ ! -f "${image}" ]; then
cleanup_and_exit 1 "specified image file does not exist."
fi
fi
## if we're not given a filename, use the existing filename
if [ $# -eq 1 ]; then
filename=`basename ${1}`
else
filename=${2}
fi
## create cookie jar and server output files
create_tmpfile
cookie_jar=${tmpfile}
create_tmpfile
srv_output=${tmpfile}
## change curl args to use cookie jar and fail on http error responses
CURL_ARGS="-b ${cookie_jar} -c ${cookie_jar} -o ${srv_output} -i -s -f"
## create a new session and attempt to log in
do_curl "http://${BASE_URL}" || cleanup_and_exit 1
do_curl -d "username=${TMBO_USERNAME}&password=${TMBO_PASSWORD}" "https://${BASE_URL}/logn.php" || cleanup_and_exit 1
## check for 302/redirect on success
grep -q '^Location: /offensive/?c=' ${srv_output}
if [ $? -ne 0 ]; then
cleanup_and_exit 1 "could not log into tmbo, check your credentials."
fi
## check for remaining upload count
do_curl "http://${BASE_URL}/index.php?c=upload" || cleanup_and_exit 1
remaining_uploads=`grep "You have [0-9]* uploads left" "${srv_output}" | sed 's/.*You have \([0-9]*\) uploads left.*/\1/g'`
if [ -z "${remaining_uploads}" ] || [ "${remaining_uploads}" -eq 0 ]; then
cleanup_and_exit 1 "sorry, no more uploads today."
fi
## if we have tags, append a space to the end of the string
if [ ! -z "${tags}" ]; then
tags="${tags} "
fi
## is this not safe for work?
if [ ! -z "${nsfw}" ]; then
postargs="${postargs}-F 'nsfw=1' "
fi
## is this offensive?
if [ ! -z "${tmbo}" ]; then
postargs="${postargs}-F 'tmbo=1' "
fi
## before we upload, do we have a comment to get?
if [ ! -z "${comment}" ]; then
## create comment file
create_tmpfile
comment=${tmpfile}
## if not defined, find a valid editor
if [ -z "${EDITOR}" ]; then
EDITOR=`which ${WHICH_ARGS} vim 2>/dev/null` || \
EDITOR=`which ${WHICH_ARGS} vi 2>/dev/null` || \
EDITOR=`which ${WHICH_ARGS} nvi 2>/dev/null` || \
EDITOR=`which ${WHICH_ARGS} nano 2>/dev/null` || \
EDITOR=`which ${WHICH_ARGS} pico 2>/dev/null` || \
EDITOR=`which ${WHICH_ARGS} emacs 2>/dev/null` || \
echo "$0: could not find a valid comment editor... skipping"
fi
## do we have a valid editor?
if [ ! -z "${EDITOR}" ]; then
${EDITOR} ${comment}
if [ -z "`cat ${comment}`" ]; then
echo "$0: blank comment file, do you want to continue? (yes/no)"
read blank_confirm
if [ "${blank_confirm}" != "yes" ]; then
cleanup_and_exit 1
fi
fi
else
comment=""
fi
fi
## upload! and check for success...
do_curl -F "c=upload" -F "filename=${filename}" ${postargs} -F "image=@${image};filename=${filename}" "http://${BASE_URL}/index.php" || cleanup_and_exit 1
grep -q 'Your upload can be viewed' ${srv_output}
if [ $? -ne 0 ]; then
## no, do we need to resample?
grep -q 'Compress my file.' ${srv_output}
if [ $? -eq 0 ]; then
## resample. always.
do_curl -d "c=upload" -d "resample=Compress%20my%20file%2e" "http://${BASE_URL}/index.php" || cleanup_and_exit 1
## display url if our resample+upload successful
get_uploaded_url
if [ $? -ne 0 ]; then
cleanup_and_exit 1 "an unknown error occurred on upload, sorry dude."
fi
else
get_uploaded_url
fi
fi
## XXX: comment posting code goes here
## logout and cleanup
do_curl https://${BASE_URL}/logout.php || cleanup_and_exit 1
cleanup_and_exit 0
@numist
Copy link

numist commented Jan 10, 2011

it would be more appropriate and less fragile to use the API. see http://thismight.be/offensive/api.html for documentation. if you find any bugs send them my way and I'll fix them in short order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment