Skip to content

Instantly share code, notes, and snippets.

@rkumar
Created January 1, 2010 15:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rkumar/267130 to your computer and use it in GitHub Desktop.
Save rkumar/267130 to your computer and use it in GitHub Desktop.
a command-line tumblr client using bash shell
#!/bin/bash
## vim:ts=4:sw=4:tw=100:ai:nowrap:formatoptions=croqln:
#*******************************************************#
# tumblr.sh #
# written by Rahul Kumar #
# 2010/01/01 #
# Post, edit, delete, read tumblr.com #
#*******************************************************#
# References: http://www.tumblr.com/api#api_write
# DONE : add read
# DONE : add --tags
# DONE : add --format (html, markdown)
APPNAME=$( basename $0 )
# Thanks to Andrea Olivato for basic example of post from :
# http://quickies.andreaolivato.net/post/108364451/post-to-tumblr-via-bash-using-curl
# enter your username and pass here to avoid entering repeatedly
# or enter in your environment or pass on command line
#TMB_USER='me@gmail.com';
#TMB_PASS='secret';
OUTFILE="~/tmp/tumblr.out"
# response of curl goes to this file
OUTFILE="tumblr.out"
# operation is logged to LOGFILE so you can delete or edit using post-id
LOGFILE="/Users/rahul/tumblr.log"
# delim in LOGFILE
DELIMITER='þ'
POSTID=
# ---------------------------------------------------------------------------- #
# post
# post some content (alias regular)
# @param : title title of post
# @param : content content of post
# @return : 0 or 1
# ---------------------------------------------------------------------------- #
post ()
{
errmsg="Usage: $APPNAME post <title> <content>
Options include:
--date | --format | --tags | --private
"
TMB_TITLE="$1" # title of post
[[ -z "$TMB_TITLE" ]] && { echo "Error: TITLE blank. $errmsg" 1>&2; exit 1; }
shift
TMB_CONTENT="$@" # content of post
[[ -z "$TMB_CONTENT" ]] && { echo "Error: CONTENT blank. $errmsg" 1>&2; exit 1; }
TMB_TYPE='regular';
[[ ! -z "$TMP_ID" ]] && { POSTID="&post-id=$TMP_ID"; }
curl -s \
-o $OUTFILE \
-d "email=$TMB_USER&password=$TMB_PASS&type=$TMB_TYPE&title=$TMB_TITLE&body=$TMB_CONTENT${TMB_EXTRA}&generator=tumblr.sh${POSTID}" \
"http://www.tumblr.com/api/write"
if [ $? -eq 0 ]; then
logme "post" "$TMB_TITLE"
fi
}
link ()
{
errmsg="Usage: $APPNAME link <url> [name] [description]
Options include:
--date | --format | --tags | --private
"
TMB_URL="$1" # title of post
[[ -z "$TMB_URL" ]] && { echo "Error: URL blank. $errmsg" 1>&2; exit 1; }
shift
TMB_NAME="$1" # content of post
[[ -z "$TMB_NAME" ]] && { echo "Warning: NAME blank. $errmsg" 1>&2;
echo
echo -n "Do you wish to continue without name?" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) echo "Going ahead.." ;;
*) echo "$APPNAME exiting."; exit -1;;
esac
}
TMB_DESCRIPTION="$2" # content of post
TMB_TYPE='link';
curl -s \
-o $OUTFILE \
-d "email=$TMB_USER&password=$TMB_PASS&type=$TMB_TYPE&url=$TMB_URL&name=$TMB_NAME&description=$TMB_DESCRIPTION${TMB_EXTRA}&generator=tumblr.sh" \
"http://www.tumblr.com/api/write"
if [ $? -eq 0 ]; then
logme "link" "$TMB_URL"
fi
}
quote ()
{
errmsg="Usage: $APPNAME quote <quote> [source]"
TMB_QUOTE="$1" # title of post
[[ -z "$TMB_QUOTE" ]] && { echo "Error: QUOTE blank. $errmsg" 1>&2; exit 1; }
shift
TMB_SOURCE="$@" # content of post
[[ -z "$TMB_SOURCE" ]] && { echo "Warning: SOURCE blank. $errmsg" 1>&2;
echo
echo -n "Do you wish to continue without SOURCE?" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) echo "Going ahead.." ;;
*) echo "$APPNAME exiting."; exit -1;;
esac
}
TMB_TYPE='quote';
curl -s \
-o $OUTFILE \
-d "email=$TMB_USER&password=$TMB_PASS&type=$TMB_TYPE&quote=$TMB_QUOTE&source=$TMB_SOURCE${TMB_EXTRA}&generator=tumblr.sh" \
"http://www.tumblr.com/api/write"
if [ $? -eq 0 ]; then
logme "quote" "$TMB_QUOTE"
fi
}
# ---------------------------------------------------------------------------- #
# delete
# delete a post
# @param : post-id post identifier
# @return : 0 or 1
# ---------------------------------------------------------------------------- #
delete ()
{
errmsg="Usage: $APPNAME delete <post_id> "
post_id="$1" # post identifier
[[ -z "$post_id" ]] && { echo "Error: post_id blank. $errmsg" 1>&2; exit 1; }
curl -s \
-o $OUTFILE \
-d "email=$TMB_USER&password=$TMB_PASS&type=$TMB_TYPE&post-id=$post_id&generator=tumblr.sh" \
"http://www.tumblr.com/api/delete"
if [ $? -eq 0 ]; then
logme "delete" "$post_id"
fi
}
_read ()
{
errmsg="Usage: $APPNAME read <URL> "
errmsg+="
read options:
--type|--private|--id|--tagged|--start|--num|--filter|--search|--chrono
e.g.,
tumblr.sh --type link read rkonrails"
URL="$1" # url to view
[[ -z "$URL" ]] && { echo "Error: URL blank. $errmsg" 1>&2; exit 1; }
[[ ! -z "$TMB_USER" && ! -z "$TMB_PASS" ]] && {
AUTH_STRING="email=$TMB_USER&password=$TMB_PASS&"
}
curl -s \
-o $OUTFILE \
-d "${AUTH_STRING}$TMB_EXTRA" \
"http://${URL}.tumblr.com/api/read"
}
help ()
{
echo "$APPNAME Actions are post/regular, link, quote, delete"
echo " To edit, pass --post-id <ID> along with other arguments"
echo " You may enter your username and password in the environment"
echo " or the top of this file as TMB_USER and TMB_PASS. "
echo
echo " See http://www.tumblr.com/api#api_write"
exit 0;
}
logme ()
{
now=$( date '+%Y-%m-%d %H:%M' )
echo "$now${DELIMITER}$1${DELIMITER}$2" >> $LOGFILE
}
# ADD methods before this
TMB_TYPE='regular';
TMB_EXTRA=""
while [[ $1 = -* ]]; do
case "$1" in # remove _
-u|--user)
TMB_USER="$2" #
shift 2
;;
-p|--pass)
TMB_PASS="$2" #
shift 2
;;
-postid|--post-id|--postid)
TMB_ID="$2"
shift 2
;;
-tag|--tag|--tags)
TMB_TAGS="$2"
TMB_EXTRA+="&tags=$2"
shift 2
;;
-format|--format)
TMP_FORMAT="$2"
TMB_EXTRA+="&format=$2"
shift 2
;;
--date|--type|--private|--id|--tagged|--start|--num|--filter|--search|--chrono)
# other than date, all the above are read options
opt=${1:2}
TMB_EXTRA+="&${opt}=$2"
shift 2
;;
-h|--help)
help # a function ;-)
# no shifting needed here, we'll quit!
exit
;;
*)
echo "Error: Unknown option: $1" >&2 #
exit 1
;;
esac
done
[[ -z "$TMB_USER" ]] && {
echo -n "Enter username: "
read TMB_USER
}
[[ -z "$TMB_PASS" ]] && {
echo -n "Enter password: "
read TMB_PASS
}
[[ -z "$TMB_USER" || -z "$TMB_PASS" ]] && { echo "Exiting."; exit 1; }
action=$( echo "$1" | tr 'A-Z' 'a-z' )
shift
[[ -z "$action" ]] && { action="help"; }
case $action in
"post" | "regular")
post "$@" ;;
"quote")
quote "$@" ;;
"link" )
link "$@" ;;
"pic" | "photo" | "image" )
photo "$@" ;;
"del" | "delete" )
delete "$@" ;;
"r" | "read" )
_read "$@" ;;
"help")
help;;
* )
#guess_error "$@"
echo "Action ($action) incorrect. Actions include post, quote, link, delete, photo, help"
help
;;
esac
cat "$OUTFILE"
echo
@rkumar
Copy link
Author

rkumar commented Nov 8, 2011

markdown no longer works, use type as html. The api has changed and the new one requires OAuth authentication.

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