Tweeter Mark 2½

Those who code in languages like C, Java or C++ are accustomed to working on codebases that sometimes have millions of lines. Thats why professional editors have a feature called folding which lets you toggle visibility of selected sections in a file.

Humble scripters like yours truly get lost when some code grows beyond 100 lines. Then it’s time to segment and conquer. For version 2½ of Tweeter we do just that. And eliminate a security concern at the same time.

Prerequisites

The script calls several commands and applications. Namely wc, expr, sqlite3 and most importantly curl.

The first two are standard *nix commands and you will most probably have them available if your system was installed and configured correctly. sqlite3 and curl can be found in any good distros repos. Use your package manager to find and get them.

Split it up

In todays episode we make use of bash functions and thereby seperate authentication credentials from the main body of our script.

Before we dive in lets create a directory where we store our modules. If you are the only user on that machine you can skip the chmod step and leave permissions for that directory at whatever your systems umask allows.

you@yourmachine:~$ mkdir twitterbox  && chmod 700 twitterbox 

So from now when you read "save as …" this means "save in twitterbox as …". Of course you can call that directory whatever you like. Only remember how you called it.

OK. The first module that gets put into twitterbox holds our logon credentials for Twitter.

readcreds()
{
TNAME="your_twitter_screen_name"
TWORD="your_twitter_password"
}

Save as creds.lib and set permissions to 600.

Next we separate the transport, charcounter and croak from the main body like so:

transporter()
{
# the next 2 lines should really be one line only
# but this template is so narrow that the full line overflows
REP=$(curl -s -S -u "$TNAME":"$TWORD" -d status="$tweet" http://twitter.com
/statuses/update.json)
EXP=$(expr match "$REP" ".*id\":\([0-9]*\)")
sqlite3 ~/twitterbox/tweets<<EOF
insert into mems values("$EXP","$tweet");
EOF
}

charcounter()
{
a=$(echo -n "$tweet" | wc -m)
}

telloff()
{
b=$(($a - 140))
echo "$b extraneous chars in: $tweet"
}

Save as twitfuncs.lib

Now that everything that does anything is safely tucked away in twitterbox we turn to the main body of the script.

#!/bin/bash

# store path to our functions in handy variables
credsfile=/home/$USER/twitterbox/creds.lib
funcsfile=/home/$USER/twitterbox/twitfuncs.lib

# check if $credsfile exists and is owned by current user
# if yes source and continue
# else croak and bail out
if [[ -O $credsfile ]]
then
. $credsfile
. $funcsfile
else
echo "No credentials found. Bye bye!"
exit;
fi

# lets assume all went swimmingly
# and we can read our credentials
readcreds

# ask the user for her message to the world
read -e -p "Tweet: " tweet

# send message through charcounter
# if within 140 chars shoot to twitter
# else croak and dump tweet back to stdout
charcounter
if [[ $a -le 140 ]]
then
transporter
else
telloff
fi
exit; 

This version of our script can safely be used in a multi-user environment. So save it as tweeter or something equally silly, set permissions to 711 and move to /usr/local/bin with cp -p.

Whats’s next?

The new modular design is the first step towards adding more functionality. In our next episode Tweeter will learn how to send long URLs to bitly for shortening and include the short URL in your tweet. It will also team up with some javascript and learn how to download and format timelines for output to the terminal or a Vim buffer.

Advertisements

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 Bash 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