Saturday, 30 April 2011

Griffon Twitter Plugin

Well I've just released the Twitter Plugin for Griffon.

This plugin enables your application to send and receive messages from Twitter using the twitter4j library. I tried to make it as simple as I could.

The plugin is based on two different concepts: the authentication process and the use of the twitter4j instance for accessing Twitter services.
  1. Authentication process or Login through events

When installing the Twitter plugin a twitter4j.properties file is created. For being able to get the proper credentials from Twitter, at least your application must supply the following properties :
  • oauth.consumerKey
  • oauth.consumerSecret
This two properties can be obtained once you've registered your application on Twitter (see http://dev.twitter.com/). Right now only pin authentication is supported (see desktop clients authentication)

If our Griffon application there're two events for getting an authenticated connection:
  • loginRequested
  • credentialsRequested
You should use the loginRequested event just the first time you try to connect to Twitter for getting the credentials.
  app.event("loginRequested",[username,password])

This event can produce two different events: loginFailed, loginSucceed

If the loginRequested throws a loginSucceed event you can use the onLoginSucceed closure to use the twitter instance to do a privileged action.

def onLoginSucceeded = {twitter->
//
}

Notice that once application has been authenticated the credentials are stored in your operating system home directory, in a hidden directory called .tweetagile. This way next time you open the application you simply have to ask for the previously acquired credentials.

Once you've stored the credentials from Twitter the credentialsRequested event will be the most used event every time you want to check if credentials has been stored in your system.

If credentials are available the credentialsRequested event will throw a credentialsFoundEvent.

  def onCredentialsFound = {twitter->
//
}


Otherwise it will throw a credentialsNotFound event with the failure exception as closure argument.

def onCredentialsNotFound = {ex->
//
}


2. Twitter instance injection in every controller

Events are used to launch the authentication process, but once we get the credentials we don't want to deal with them anymore.

The plugin injects a Twitter class instance in every controller. This instance has most of the methods needed to access to Twitter. You can consult the javadoc of Twitter4j here.

If the authentication process has been resolved successfully before the creation of a MVC group, then the injected instance won't need any further authentication, otherwise you'll need to launch the login event chain.
import static java.awt.BorderLayout.CENTER

/**
* @author mgg
*
*/
class LastTweetsController {

private static final String HTML = "#"

def model
def view
def twitter

/* This method is triggered each time the tweetList event is thrown. It updates the
* home time line tweets */
def onTweetList = {
/* We need the main view to change the */
def mainModel = app.groups.patweet.model
def fTeets = [] as Vector
try{
execAsync{
mainModel.busy = true
}
doOutside{
/* Getting the last 20 tweets from our home line */
twitter.getHomeTimeline().each{
def entry =
new TweetEntry(
text: HTML.replace("#",it.text),
createdAt:it.createdAt
)
/* If tweet user has profile image */
if (it.user.profileImageUrl){
entry.imageUrl = it.user.profileImageUrl
}
/* And finally adding the tweet to the list */
fTeets << entry } /* If there were any tweet */ if (fTeets){ doLater{ execAsync{ /* Updating JList data in the EDT */ view.lastTweetsList.listData = fTeets } } } } } finally { /* Whatever it may happen the busy panel should be turned off */ execAsync{ mainModel.busy = false } } } }



All the examples are taken from the TweetAgile example application, you can check the code here.

1 comment:

  1. Hi, Some one from facebook refereed your link i have book marked it nice blogs you write see Free  how to grow twitter followers here

    ReplyDelete