Jim Lawless' Blog


Twimmando: A Command-line Twitter Client

Originally published on: Sun, 24 May 2009 22:19:46 +0000

4/24/2010 - Please note: Twimmando may no longer function properly in a short while. Please see: http://www.mailsend-online.com/blog/twimmando-no-more.html

I joined Twitter about a week ago. I was aware of Twitter, but wasn't sure it'd be useful. I was pleasantly surprised to find a simple means of communicating with the masses.

As I looked through the Twitter documentation, I found reference to a RESTful API that was simple enough to use with cURL. In fact, most of the API samples illustrate a particular API function by using a cURL command-line.

I decided to tinker with the REST API and had a few programs that I had wanted to write, but I decided to start with a test harness of sorts: a simple command-line interface ( like a poor-man's version of cURL ... which is free and ubiquitous, so one would have to be *very* poor to use mine. )

I created Twimmando a simple command-line program written in csharp, compiled for .NET 2.0.

Twimmando has a simple command-line interface: Twimmando v1.01 by Jim Lawless (@lawlessGuy) Usage: twimmando [options] Where options are: -uri resourceURI ; such as /statuses/public_timeline.xml -u username ; Twitter password -p password ; Twitter user name -head ; send HTTP HEAD request, default is GET -post ; send HTTP POST request, default is GET -f name value ; add POST data name/value pair to request

For a list of REST API URI's, please see this document:

http://apiwiki.twitter.com/Twitter-API-Documentation

Let's just start by downloading the public timeline of Twitter posts anonymously as XML. twimmando -uri /statuses/public_timeline.xml

You'll see a stream of XML go by in your display containing tweets (newest first) from everyone ... not just the friends you're following.

We can also download this data in JSON ( JavaScript Object Notation ) format or RSS ( Really Simple Syndication ) format by the two examples respectively:

twimmando -uri /statuses/public_timeline.json

twimmando -uri /statuses/public_timeline.rss

At this point, you could capture the information in a file in one of the above formats and can begin processing it with some other utility.

In order to invoke the REST API so that it functions under your Twitter user ID, you have to supply your user ID and password on the request.

The following command will download the list ( newest first ) of tweets that your friends have made. twimmando -uri /statuses/friends_timeline.xml -u ID -p PASS

...where ID is your Twitter user ID and PASS is your Twitter password.

All of these documents are downloaded via the HTTP GET verb. I added support for the HTTP HEAD verb so that one could note the characteristics of a particular document and only process it if new information was present. It doesn't appear as if the Last-Modified header shows the date/time of the last tweet in the group. One might have to use the Content-Length header to determine whether or not a download would yield updated information.

To see the headers, add the parameter -head to your command-line above: twimmando -head -uri /statuses/friends_timeline.xml -u ID -p PASS

To post a new update to Twitter, one must use the -post parameter and a field/value pair for the field "status". twimmando -uri /statuses/update.json -f status "Hello from Twimmando!" -post -u ID -p PASS

The new tweet Hello from Twimmando! should show up in your personal history.

Note that Twimmando doesn't check the size of the status field to ensure that it posts within the 140-character limit.

I chose csharp as an implementation language as I'm planning to write something a little bigger than Twimmando and csharp was actually my choice for that project.

I began having difficulty when making a POST to the Twitter server. I had thought that I wasn't sending up credentials properly ... I received an HTTP 417 error. The exception condition arose because the HttpWebRequest class was sending up an Expect: 100 continue header with each POST.

To suppress that request header, I added the line below:


webRequest.ServicePoint.Expect100Continue = false;

To add the credentials ( if present ) to a request, I used the snippet below:


if(config.Password!=null) {
   webRequest.Credentials =
      new NetworkCredential(config.UserName, config.Password);
}

To URL-encode the POST data string, I used a utility method from a class that is more likely to be associated with a .NET server process; System.Web.HttpUtility.UrlEncode().

I had originally intended to use a cookie storage file so that one would only have to authenticate once and would then simply serialize the cookies and deserialize them for each subsequent request.

I took a merry ride through the internals of CookieContainer and CookieCollection and found that the domain associated with the session cookie was ".twitter.com" ( note the leading dot ). When I changed the domain to "twitter.com", it appeared as though Twimmando was sending up the correct session cookie, but I was still encountering authentication errors.

I finally pulled out all of the cookie support, for now. I'll revisit this issue as time permits.

I tried running Twimmando under the Mono framework ( under Windows ). The EXE / assembly that I had compiled using the Microsoft .NET 2.x compiler ran, but then seemed to hang at the end of the process. I'm not sure why, yet.

You may download the source and .NET 2.0 EXE for Twimmando here:

http://www.mailsend-online.com/wp/twimmando.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless.



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: A DSL in JavaScript
Next post:WSH2EXE - The Final Chapter


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

A Simple ROT13 Macro

Screen Capture from Multiple Monitors in Java

Extending SpiderMonkey JavaScript on Windows

We've Moved!

Directory Traversal in Rhino JavaScript

A Review of Kevin Mitnick's Book Ghost in the Wires

WSH2EXE - The Final Chapter

Windows Text to Speech in WSH JavaScript

WSH2EXE part 1

Choose your own Adventure with Sinatra


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service