Writing a simple Twitter streaming client

July 8, 2009

I spent a little bit of time tonight and wrote a simple Twitter client, which utilizes the new Twitter streaming API. The new API from Twitter allows you to subscribe to an HTTP stream of public status updates from Twitter users. Using the Apache HttpComponents HttpClient and Json-lib I was able to quickly whip up a Java command line application. The application connects to the “spritzer”, which is more of a drip than a stream when compared to the other streams you can connect with: gardenhose and firehose (both of which require approval from Twitter before you can access them).

Twitter returns each status update on a single line in JSON format (you can also get a carriage-return-delimited XML format), making parsing relatively simple. I wrapped the InputStream I get back from HttpClient in a BufferedReader, allowing me to simply call BufferedReader.readLine() and pass the result to the Json-lib JSON parser.

In the end, my simple client produces the follow output, giving you a sense of what’s possible. I can imagine this API being useful for realtime data mining or visualizations.

Tweet from Shop @ HART market (HARTmarket)
  Vision Shop \ Fluoroperm 30 Contact Lenses http://cli.gs/JXLuD
--------------------------------------------------
Tweet from Hinrich von Donner (hinrichd)
  @ntvde Kommen bei Euch auch noch "normale" Nachrichten? Langsam nervt's
--------------------------------------------------
Tweet from Kelvin Cambridge (supersidekid)
  If a lifetime is how long you want me for then thats wats you will get http://bit.ly/SrRf7
 Maxwell called Lifetime
--------------------------------------------------
Tweet from VeolaJBanner (VeolaJBanner)
  Black
 pretty
lady
20 years old
 looking forward to
 hot chat
 at weekend http://tinyurl.com/pg5h9x
 - free online adult dating!
--------------------------------------------------
Tweet from Sonnie Tease (sonniestack)
  @admit_one checkers, where, haha.
--------------------------------------------------
Tweet from Shanda (ShandaP)
  Clearly i am on a mission today
--------------------------------------------------
Tweet from Krissi Balut (krissibalut)
  @Connor_Anderson  ferris wheel? Where?
--------------------------------------------------
Tweet from Justin Kerr-Stevens (jkerrstevens)
  RT @lovisatalk: RT @govloop:  GovLoop parties - DC w/ @corbett3000  http://bit.ly/Cho83 on 7/16;  San Fran http://bit.ly/c1jlu #gov20

Entry Filed under: Development. .

1 Comment

  • 1. Leah Culver  |  July 8, 2009 at 9:08 pm

    Very cool! I might use birddog for adding a Twitter channel to leafychat.com. Interesting.


Calendar

July 2009
M T W T F S S
« Apr   May »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Most Recent Posts