Looks like Twitter will be under the spotlight at JavaOne this year. Posts on this topic are popping everywhere. I already talked about the "Script Bowl" session but if you are attending JavaOne you should probably have a look to this post.
But back to Groovy, I propose to write a simple class to interface to Twitter and then build mashup. But first, let's draft some specs. All communication with the server goes through http and sticks to the REST approach. You can find most of what I used here and there. Before going further, you must be familiar with some of the Twitter's terminology:
1. Authentication. This is fairly easy since Twitter uses Basic Authentication. I propose to set an authenticator using a Groovy syntax ;-)
Authenticator.setDefault(
[getPasswordAuthentication : { return new PasswordAuthentication(name, password as char[]) } ] as Authenticator
)
2.Retrieve friends. Fairly easy as well, we just need to send a GET request to http://twitter.com/statuses/friends/${user.id}.xml and yes you have to add ?page=$page if you have plenty of them. In this request, user is a Twitter user returned by getUser().
3.Update your status. Last by not least, we need to update our status. This is done by sending a POST request to http://twitter.com/statuses/update.xml using a RESTful request.
An example is worth thousands of words:
Obviously this API is still in its infancy, I have been playing with it few hours. You will be able to get it here: http://code.google.com/p/groovy-twitter/
But back to Groovy, I propose to write a simple class to interface to Twitter and then build mashup. But first, let's draft some specs. All communication with the server goes through http and sticks to the REST approach. You can find most of what I used here and there. Before going further, you must be familiar with some of the Twitter's terminology:
- Tweet
Every person has a status. Updating the status with a new one is the same as sending a tweet. Tweets have to be smaller than 140 characters. - Friends
people you are subscribed to (following) - Followers
people who are subscribed to your tweets
- Direct message
a private message sent between two (or more) users. - Replies
you can reply to another users’ tweets using the@username
prefix in your own tweets.
- authenticate to the server using a given login (and associated password)
- retrieve the friends of that user
- update it's status
1. Authentication. This is fairly easy since Twitter uses Basic Authentication. I propose to set an authenticator using a Groovy syntax ;-)
Authenticator.setDefault(
[getPasswordAuthentication : { return new PasswordAuthentication(name, password as char[]) } ] as Authenticator
)
2.Retrieve friends. Fairly easy as well, we just need to send a GET request to http://twitter.com/statuses/friends/${user.id}.xml and yes you have to add ?page=$page if you have plenty of them. In this request, user is a Twitter user returned by getUser().
3.Update your status. Last by not least, we need to update our status. This is done by sending a POST request to http://twitter.com/statuses/update.xml using a RESTful request.
An example is worth thousands of words:
twitter = new Twitter("guillaume.alleon@gmail.com", "****")
twitter.postUpdate("Finalizing my Groovy Twitter API")
def u = twitter.getUser("glaforge")
println "Id of Groovy grand master is ${u.id}"
def f = twitter.friends
f.each{println it.name}
Obviously this API is still in its infancy, I have been playing with it few hours. You will be able to get it here: http://code.google.com/p/groovy-twitter/
No comments:
Post a Comment