Twitter checker Plugin (followers, timeline, mentions and RTs checker)

  • Tags : twitter
  • Latest : 0.2
  • Last Updated: 13 January 2011
  • Grails version : 1.2 > *
  • Authors : null
1 vote
Dependency :
compile ":twitter-checker:0.2"

Documentation

Summary

Installation

grails install-plugin twitter-checker

The installation will create these files in your project:

  1. controllers/twitterChecker/TwitterCheckerController.groovy
  2. views/twitterChecker/demo.gsp
  3. views/twitterChecker/index.gsp
  4. views/twitterChecker/_twitFromMe.gsp
  5. views/twitterChecker/_twitFromOther.gsp
  6. src/groovy/twitterChecker/DefaultTwitterCheckerListener.groovy

Description

Description

The TwitterChecker plugin uses the Twitter4J api with OAuth to periodically check (using Quartz) for:

  • New followers and unfollows in your account.
  • New RTs of you made by other users.
  • New mentions to you made by other users.
On new stuff detected, the plugin triggers events in a class created by you. With these events, you can send emails, direct messages or whatever. The plugin can store your timeline, mentions to you and RTs from you, and show it in your site with a very simple taglib.

Spanish documentation here

Requires

  • Quartz plugin 0.4.1 or higher.
  • Grails 1.2.0 or higher.
  • Tomcat Julli 6.0.16 or higher.

Includes

  • twitter4j-core-2.1.7.jar (latest stable version)

Installation

grails install-plugin twitter-checker

If deploying a war to a non-tomcat container, add this to your BuildConfig.groovy (Grails 1.3+)

dependencies { runtime 'org.apache.tomcat:juli:6.0.16' }

The installation will create these files in your project:

  1. controllers/twitterChecker/TwitterCheckerController.groovy
  2. views/twitterChecker/demo.gsp
  3. views/twitterChecker/index.gsp
  4. views/twitterChecker/_twitFromMe.gsp
  5. views/twitterChecker/_twitFromOther.gsp
  6. src/groovy/twitterChecker/DefaultTwitterCheckerListener.groovy

First time configuration

  1. First, you have to create a Twitter application (default access type: read&write, application type: client) in this url: http://dev.twitter.com/apps/new. Get the consumer key and consumer secret.
2. Copy this snippet in your Config.groovy
grails.spring.bean.packages = ["twitterChecker"]
twitterChecker {
    oauth.consumerKey = "the consumer key of your twitter app you created"
    oauth.consumerSecret = "the consumer key of your twitter app you created"
    storageFolder = "/path/to/store/the/followers/and/other/stuff"
}
  • grails.spring.bean.packages: the package where the DefaultTwitterCheckerListener.groovy class is. Don't modify this value, even if you rename the class. Only if you move the DefaultTwitterCheckerListener class to other package, you will need to change the grails.spring.bean.packages with the new package.
  • outh.consumerKey and oauth.consumerSecret: put your own consumer key / secret from your twitter app.
  • storageFolder: the plugin needs a path where to read and write binary files with the followers information, mentions and RTs, in order to check if there is something new, and trigger events.
3. Authorize your twitter account with your new twitter application (it's not the same thing)
  • Run your application and go to the TwitterCheckerController in your browser: http://localhost:8080/yourAppName/twitterChecker (don't worry about the "OAuth configuration not defined in Config.groovy" exceptions in your log)
  • Follow the instructions: get the new pin, login in twitter, allow access and submit the pin.
  • The page will show your accountId, token and tokenSecret values. Copy these values in your Config.groovy file.
4. Restart your application and go again to the TwitterCheckerController in your browser: http://localhost:8080/yourAppName/twitterChecker (this time you shouldn't see OAuth exceptions in your log). You will see a page with your timeline, the mentions and RTs of you (check the views/twitterChecker/demo.gsp file)

5. Now, you can delete the TwitterCheckerController.groovy controller and the twitterChecker/demo.gsp and twitterChecker/index.gsp views. But DON'T DELETE the _twitFromMe.gsp and _twitFromOther.gsp views. Of course, you can modify these files, but don't delete them.

Usage

Showing timeline, mentions and rts in your site:

As you can see in the demo.gsp view, you can use these tags in your gsp to show cached versions of your timeline, mentions and rts.

<twitterChecker:timeline max="10"/>
<twitterChecker:mentions max="200"/>
<twitterChecker:rts/>

You can use the max param in all taglibs, but it's optional

The plugin refresh and cache this data every 15 minutes, and the taglib will use this cached version data, so you can use these tags as many times as you want in your site. These tags uses the the _twitFromMe.gsp and _twitFromOther.gsp views copied in your project in the installation, so feel free to modify them.

If you need a specific format, you can use the eachMention, eachTimeline and eachRt tags:

<ul>
<twitterChecker:eachMention>
    <li>${it.text}</li>
</twitterChecker:eachMention>
</ul>

If you need to highlight with links the twit's text, you can use the <twitterChecker:parseLinks> tag:

<ul>
<twitterChecker:eachMention>
    <li><twitterChecker:parseLinks text="${it.text}"/></li>
</twitterChecker:eachMention>
</ul>

Events dispatched

Every 15 minutes, the plugin will check for new followers, new mentions and rts. Open the src/groovy/twitterChecker/DefaultTwitterCheckerListener.groovy file in your project and modify the onNewFollowers, onUnfollows, onMentions and onRetweets closures.

You can modify the checking time in the Config.groovy with these properties (values are in minutes, so 15 means 15 minutes)

twitterChecker {
    checkTimelineEvery = 5  // 5 minutes
    checkMentionsEvery = 30 // 30 minutes
    checkRetweetsEvery = 30 // 30 minutes
    checkFollowersEvery = 120 // 2 hours
}

Remember that you only have up to 350 request per hour. Every timeline, mentions, rts and followers check is a request, so with a 15 minutes value for all the checks means 16 request per hour. More info info in: http://dev.twitter.com/pages/rate_limiting_faq

Using the Twitter4J api with pre-configured Oauth.

The plugin provides you a TwitterCheckerService class. This service acts as a Oauth pre-configured Twitter class (using the Delegate Groovy annotation), so you can use all these methods and features: http://twitter4j.org/en/javadoc-latest/core/twitter4j/Twitter.html

The TwitterCheckerService has these new properties: cachedMentions, cachedTimeline and cachedRts. The taglib use them to render the views.

You can use it in any controller, service, taglib like this:

class MyController {
    def twitterCheckerService

def updateStatus = { // use the http://twitter4j.org/en/javadoc-latest/core/twitter4j/Twitter.html#updateStatus(java.lang.String) method def status = twitterCheckerService.updateStatus("Test from grails TwitterChecker plugin") … } def readTimeline = { // use the cached version of the user timeline twitterChecerService.cachedTimeline

// get a new fresh version of the user timeline http://twitter4j.org/en/javadoc-latest/core/twitter4j/Twitter.html#getUserTimeline() def timeline = twitterCheckerService.userTimeline … } }

I want to create a new job and cache info from twitter.

For example, if you want to show in your views a Twitter search for #grailsx, you can create a Job for downloading this data and cache it in your own service:

class CheckSearchQueryJob  {

def yourOwnService // Class where you are going to store the cached data from Twitter...

static triggers = { simple(name: 'CheckSearchQueryJob', repeatInterval: 15*60*1000) // Every 15 minutes } TwitterCheckerService twitterCheckerService

def execute() {

def resultQuery = twitterCheckerService.search(new twitter4j.Query("#grailsx"))

yourOwnService.cachedQuery = resultQuery // … and store the data } }

You can use the yourOwnService.cachedQuery in your views.

Troubleshooting

I got this exception

Caused by: 401:Authentication credentials were missing or incorrect.
{"error":"Read-only application cannot POST","request":"/1/statuses/update.json"}

Your application is read only or it was read only when you granted it.

  • First, go to http://dev.twitter.com, edit your app details and ensure that "read-write" access type is selected.
  • Then, go to your twitter account and revoke the access to your app in http://twitter.com/settings/connections.
  • In your Grails application, remove the token and tokenSecret from your Config.groovy, restart and re-negotiate your access token again (get the pin, allow access).

Author

Alberto Vilches (vilches@gmail.com)

Please report any issues to the Grails User mailing list and/or write up an issue in JIRA at http://jira.codehaus.org/browse/GRAILSPLUGINS under the Grails-Twitter-Checker component.

History

  • December 7, 2010
    • released initial version 0.1
  • January 12, 2011
    • released 0.2 version. Bug fixed in taglib by Ryan Vanderwerf. New "max" param in taglibs.