Vitor Pamplona

Innovation on Vision: Imaging , Enhancement and Simulation

Twitter from Command Line using Shell Script

It turns out that it is still possible to tweet from command line without designing an oAuth client. I am using only shell script, curl and sed. The script works just like a browser, using cookies to keep users authenticated. You can download this script at github

#!/bin/sh

# Behaves like a browser.
COOKIE_LINE="--cookie /tmp/cookiesShtw.txt --cookie-jar /tmp/cookiesShtw.txt --user-agent Mozilla"

# ./twitter login <user> <passwd>
# Ret = Logged in
f_login() {
shift
# Submit login through form post.
curl -s $COOKIE_LINE --data "username=$1" --data "password=$2" --data 'commit=Sign In' \
https://mobile.twitter.com/session
echo "\nLogged in"
}

# ./twitter say <what is happening>
# Ret = Done
f_say () {
shift

# Encode text
R=`echo "$@" | sed -e 's/ /\%20/g'`

# Get authentication code.
auth=`curl -s $COOKIE_LINE http://mobile.twitter.com`
auth=`echo $auth | sed -n -e "s/.*<form action=\"http:\/\/mobile.twitter.com\/\"\(.*\)<input type=\"submit\" value=\"Tweet\" class=\"tweet-btns\"\/>.*/\1/p"`
auth=`echo $auth | sed -n -e "s/.*<input name=\"authenticity_token\" type=\"hidden\" value=\"\(.*\)\" \/>.*/\1/p"`

# Submit tweet through form post.
curl -s $COOKIE_LINE --data "tweet[text]=$R" --data "authenticity_token=$auth" \
--data 'submit=Tweet' https://mobile.twitter.com
echo "\nDone"
}

# ./twitter see
# Ret = Formatted timeline.
f_see () {
# Get the timeline
R=`curl -s $COOKIE_LINE http://mobile.twitter.com`

# Format timeline: time \n person \n message \n\n.
R=`echo "$R" | sed -n -e "s/.*<strong><a href=\"http:\/\/mobile.twitter.com\/\(.*\)\">\(.*\)<\/a><\/strong>.*/\1/p" \
-e "s/.*class=\"status_link\">\(.*\)<\/a>.*/\1/p" \
-e "s/.*<span class=\"status\">\(.*\)<\/span>.*/\1\n------------\n/p"`

echo "$R"
}

# Check option and take action
case $1 in
"login" ) f_login "$@" ;;
"say") f_say "$@" ;;
"see") f_see ;;
*) echo "Usage: $0 [login <user> <passwd> | say <text> | see ]"
esac 

You can download this script at github

Posted in Mar 7, 2011 by Vitor Pamplona - Edit - History

Showing Comments

Vitor,
this is a great script. The only " problem " I see in it is that it displays much less tweets than I see in the browser interface when I go to twitter.com with Firefox or other browser. I need to figure out how to fix this. Ideas? Thanks

- - Marco

- - Posted in Jun 19, 2011 by 151.25.12.146

is possible to auto log-in into twitter from my web application. i want to set twitter cookies in my borwser from my application

- - arivarasan

- - Posted in Sep 22, 2011 by 115.248.201.61

Hey Vitor, Works nicely, I'm testing to put y a bot's tweet line the results of execution of a statistical package. I'll give you my comments on how I deploy this. Thanks a lot.

- - Diego Diaz

- - Posted in Nov 17, 2011 by 200.35.79.55

thanks for sharing...

- - 395

- - Posted in Feb 21, 2012 by 182.186.131.94

Hey vitor, since today Twitter refreshed its web interface and this script it's no longer working. could you please tell me how make it work again?

- - Diego Díaz

- - Posted in May 8, 2012 by 200.35.79.55

Add New Comment

Your Name:


Write the code showed above on the text below.