Skip to content

the thinkpod

where I think out loud

Menu
  • Timeline
Menu

Having Your Shell Tweet

Posted on October 16, 2012 by milad

Have you ever wanted to have your *nix shell access Twitter?

Using the RESTful API, you can achieve this. However, you would need to set up your instance as an app and then configure the API keys and all the what-nots. I personally try to avoid such hassle whenever possible.

So, I got this shell script for you (which is not originally mine, but has undergone a fair bit of modification by me) which achieves this:

#!/bin/bash
#Twitter status update bot by http://360percents.com, http://mmnaseri.com
#Author: Luka Pusic <pusic93@gmail.com>
#Author: Mohammad Milad Naseri <m.m.naseri@gmail.com>

#REQUIRED PARAMS
username="TWITTER USERNAME"
password="TWITTER PASSWORD"
tweet="$*" #must be less than 140 chars

#SETUP
if test ! -e ~/.tweet;
then
    mkdir ~/.tweet
fi

#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests

if [ $(echo "${tweet}" | wc -c) -gt 140 ]; then
    echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
fi

if [ "$tweet" == "" ]; then
echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
fi

touch ~/.tweet/cookie.txt #create a temp. cookie file

#INITIAL PAGE
sleep $sleeptime
initpage=`curl -s -b ~/.tweet/cookie.txt -c ~/.tweet/cookie.txt -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new"`
token=`echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" />.*//'`

#LOGIN
sleep $sleeptime
loginpage=`curl -s -b ~/.tweet/cookie.txt -c ~/.tweet/cookie.txt -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session"`

#HOME PAGE
sleep $sleeptime
homepage=`curl -s -b ~/.tweet/cookie.txt -c ~/.tweet/cookie.txt -L -A "$uagent" "http://mobile.twitter.com/"`

#TWEET
sleep $sleeptime
tweettoken=`echo "$homepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" />.*//' | tail -n 1`
update=`curl -s -b ~/.tweet/cookie.txt -c ~/.tweet/cookie.txt -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/"`

#LOGOUT
logout=`curl -s -b ~/.tweet/cookie.txt -c ~/.tweet/cookie.txt -L -A "$uagent" "http://mobile.twitter.com/session/destroy"`

rm ~/.tweet/cookie.txt

What this script does is fairly simple:

It opens up a a connection the mobile version of the twitter, submits a fake Login form, gains access to the page, and then posts all the input parameters collectively as your Tweet.

The problem currently is that you have to write in your twitter credentials inside the file. I might change that one day.

Dependencies

You will need to install “curl” on your machine to be able to run this.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I’m a tinkerer and engineer. Here, I write about all sorts of things.

Tags

administration algorithms application bash command blog book client clrs code cormen courses Data Structures domain eyeos firefox google google interview google interview series 1 google interview series 2 hello world host interview interview preparation java job learning linux mac phone screen poem Poetry preparation programming Projects repository Review script shell spring synopsis system management user management video windows 7 writing

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2021 the thinkpod | Built using WordPress and Responsive Blogily theme by Superb