IdentiBash


I was reading my rss feeds today and ran across this great article telling how to use wget or curl to make a twitter post. Well I just happen to use identica (free and all) server that my friend Dann set up, so that stuff didn’t quite work. Drat. Well, when I saw Dann pop online I suggested that he check into it because it sure would be cool to be able to microblog straight from the command line. After all, commandline=good! Dann said he would look into it.

Well, as it turns out, I am terrible at waiting and after cruising the net for some more specific information about how identica does things I stuck together a nice little bash script myself to read and post to that particular identica server. Here’s what the code looks like:


#!/bin/bash
# Based in part on article found at http://www.ibm.com/developerworks/linux/library/l-friendfeed/index.html

maxitems=5
user=johnhancock
pass=ubersupersecretandstuff
puburl="http://stream.tllts.org/identica/api/statuses/public_timeline.xml"
posturl="http://stream.tllts.org/identica/api/statuses/update.xml"

function readpub()
{
clear
curl -s $puburl > /tmp/tweet.tmp
count=0
echo "-------------------------------------------"
while read line
do
if $(echo "${line}" | grep -q "text>")
then
echo "${line}" | sed 's///g' | sed 's/<\/text>//g'
fi

if $(echo "${line}" | grep -q "created_at>")
then
echo -n "${line}" | sed 's// @: /g' | sed 's/<\/created_at>//g'
fi

if $(echo "${line}" | grep -q "screen_name>")
then
echo "${line}" | sed 's// by: /g' | sed 's/<\/screen_name>//g'
fi

if $(echo "${line}" | grep -q "")
then
echo "-------------------------------------------"
count=$((count + 1))
if [ $count -eq $maxitems ]
then
break
fi
fi
done < /tmp/tweet.tmp } function postmsg() { curl -s -u ${user}:${pass} -d status="$msg" ${posturl} 2>&1>/dev/null
}

getopts "rp:" flag
case "${flag}" in
r) readpub ;;
p) msg=${OPTARG}; postmsg; readpub ;;
esac

If you would like to use this for identica or twitter for that matter there are only a couple changes you need to make. You have to change the variables in the beginning of the script to reflect your own settings. User is obviously your username and I understand that some services make you use your email address for this. Pass is simply your password. Note, however, that if your username or password contain wierd characters like spaces, you probably want to enclose them with double quotes. The maxitems variable tells the script how many posts to display. Five items seems to fit nicely into a standard xterminal window. Lastly, you need to alter the URL’s there, or simply join the identica server in our own little corner of the web. The url of the article included in the bash source code will give you the correct url’s for twitter and if you are using identica, you should be able to figure it out from the url’s I have provided in the script that connect to the server I use.

That’s really about it. The script is easy to use. Use the dash r option to read the recent tweets. Use the -p “some text here” option to post “some text here” to your account. Do note, however, that at this time the program does not do any urlen/decoding conversions, so watch your punctuation. If your post does not seem to actually post, that’s probably why.

As always, let me know what you think and if there is any interest, I’ll be happy to post up a project page for it and we can make it work a bit better. In fact, I keep thinking that it’d work really well as a command line php script, but that limits the userbase a lot more than bash does 🙂

Leave a Reply

You must be logged in to post a comment.