Tuesday, September 16, 2008

Twittering with a shell script (or Zen Twitter)

If I wanted to update my status on twitter then I used to have to open my web browser, go to twitter.com, login, then update my status.

Thanks to chimeric I now have an easier way of updating my status on twitter by simply just running the script from my menu that opens a dialogue box for me, which I use to enter my status then click OK ... and that's it!

It's basically just a shell script that uses cURL and Zenity, which has been named Zen Twitter ... either in the sense of simplicity or the fact that it uses Zenity, or maybe both.

This is how the shell script looks, simply just copy/paste into a text editor, enter your user name and password, then save as zentwitter.sh.

#!/bin/bash
# @author Michael Klier 
# @www    http://www.chimeric.de/pojects/ZenTwitter

# trivial twitter status update client

# set username/password
USER="user name"
PASS="password"

# main script
URL="https://twitter.com/statuses/update.xml"
ZENITY=/usr/bin/zenity
CURL=/usr/bin/curl

# check if zenity is installed
if [[ ! -x $ZENITY ]];
then
   echo "ERROR: Zenity is missing!"
   exit 1
fi

# check if curl is installed
if [[ ! -x $CURL ]];
then
   $ZENITY --error --text="ERROR: curl is missing" --title="ZenTwitter"
   exit 1
fi

# get tweet
tweet=$(${ZENITY} --entry --text="Gimme your tweet:" --title="ZenTwitter")

if [[ "$?" == 1 ]];
then
   exit 0
fi

while [[ $(echo "${tweet}" | tr -d ' ') == "" ]];
do
   tweet=$(${ZENITY} --entry --text="D'oh! You entered nothing! Try again or cancel:" --title="ZenTwitter")
   if [[ "$?" == 1 ]];
   then
       exit 0
   fi
done

# update status
$CURL -u ${USER}:${PASS} -d status="${tweet}" ${URL} -k

if [[ "$?" == 0 ]];
then
   $ZENITY --info --text="Updated successful!" --title="ZenTwitter"
   exit 0
else
   $ZENITY --error --text="Something went wrong!\nRun ZenTwitter.sh from a commandline for more information!" --title="ZenTwitter"
   exit 1
fi

Remember to insert a carriage return at the end of this script.

0 comments: