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.

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Embedding JavaScript in a Batch File

Scott Ballantyne: Blazin' Into Forth

Command-Line Image Format Conversion

A Simple Associative Array Library in C

A Simple Media Control Interface Script Processor

Safe Scripting with Scroll Lock and Caps Lock

Computers I Have Known

Setting Text Color in a Batch File

Rhino JavaScript to EXE with launch4j

Flirting with Forth


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer