How to post to twitpic from the commandline

twitpic is a service that lets you share your pics on Twitter. Thanks to a straightfoward REST API it is very easy to upload pictures to the service and tweet at the same time.

No registration required. When you open a Twitter account twitpic is part of the bargain.

In order to avoid having to pass login creds to curl everytime I want to upload a pic, I stuck my username and password into a function that the script sources at runtime. Like so:

checkcreds()
{
TNAME="username"
TWORD="password"
}

This function sits in a directory called credentials in my HOME as creds.lib and gets sourced by other Twitter related stuff as well.


#!/bin/bash

# store some paths in handy variables
credsfile=/home/$USER/credentials/creds.lib
picsdir=/home/$USER/Photos/

# check if credentials can be had
if [[ -O $credsfile ]]
then
. $credsfile
else
exit;
fi

# read in the credentials
checkcreds

# prompt user for name of pic to upload
read -e -p "Pic: " pic

# prompt for tweet to accompany pic
read -e -p "Tweet: " words

# store everything in vars to make
# curl opts for multipart form a bit more manageable
cargo="media=@${picsdir}${pic}"
myname="username=${TNAME}"
pword="password=${TWORD}"
tweet="message=${words}"
target="http://twitpic.com/api/uploadAndPost"

# tell curl to send a multipart form to twitpic
# save returned XML in a variable
RET=$(curl -s -S -F "$myname" -F "$pword" -F "$cargo" -F "$tweet" ${target})

# append XML fragment to logfile
echo "${RET}" >> ~/twitter/twitpiclog

# get the hell outa Dodge
exit;

If nothing untoward gets in the way the pic is stored at twitpic and the accompanying tweet gets published on Twitter.

About these ads

About dozykraut

Proud member of Hillbilly's on Linux, promoting open source redneckism in remote parts of the Milky Way.
This entry was posted in Web and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s