Wednesday, January 14, 2009

Google data twitters

I long had a hunch that Google Data API is not the only Atom-based API out there, but never had the time to investigate. Now I just had a look at twitter (although I'm not a user -- yet), and it turns out the XML combinators I wrote some time ago for Google Data work out-of-the box! I was so happy to notice, I had to twitter blog about it.

GData-Scala-client is a library for using Google services programmatically. The core of this library is a collection of combinators for XML serialization, together with picklers for atom and other common data. Since Twitter offers an atom-based feed API, I decided to give it a shot. I fired a Scala interpreter:


scala> import com.google.gdata._
import com.google.gdata._

scala> val service = new Service("acme-twitter", "tw") {}
service: com.google.gdata.Service = $anon$1@dfee1


Make sure you start scala including the path to your downloaded gdata-scala-client jar. The Service class we created takes care of sending simple queries over HTTP and applying a pickler on the returned data. The two parameters are the application and the service name, but they have no meaning outside Google's realm, so we can be creative. Now let's make a simple query:



scala> import data._
import data._

cala> val atomFeed = new StdAtomFeed
atomFeed: com.google.gdata.data.StdAtomFeed = com.google.gdata.data.StdAtomFeed@11db25

scala> val f = service.query("http://twitter.com/statuses/public_timeline.atom", atomFeed.feedPickler)
res1: atomFeed.Feed =
Authors:
Id: tag:twitter.com,2007:Status
Title: (None,Twitter public timeline)
Updated: 2009-01-14T21:04:11.000Z
Entries: Entry:
Authors: (Robert Basic,Some(http://robertbasic.com),None)
Id: Some(tag:twitter.com,2007:http://twitter.com/robertbasic/statuses/1119276026)
Title: (None,robertbasic: @bojanpejic thanks mate :) you'll see it tomorrow in action ;))
...


Surprise! It really worked! (at least, that was my reaction). Let's see what we had to do: create an instance of StdAtomFeed, which is a class defining the standard contents of an atom feed, together with the right pickler (serialization objects). Next, we issued a query, passing the URL and the pickler to deserialize the response.

We can further play with the Atom feed, inspecting its entries, or learning more about its contents:


scala> f.updated
res10: com.google.gdata.data.util.DateTime = 2009-01-14T21:04:11.000Z

scala> f.entries.filter (_.authors.exists(_.name.startsWith("K") ))
res17: List[atomFeed.Entry] =
List(Entry:
Authors: (Katie,None,None)
Id: Some(tag:twitter.com,2007:http://twitter.com/myheartradio/statuses/1119276031)
Title: (None,myheartradio: @E_Steve Chapter Six...I'm not too far into it yet.)
Updated: 2009-01-14T21:04:11.000Z)


That's about how far I got. Authentication does not yet work, as Google Services use a different protocol. You can find out more about what's available here, but overall it's good news and I hope others will find this library useful as well.

0 comments: