Nashorn in the Twitterverse

Posted by jlaskey on Oracle Blogs See other posts from Oracle Blogs or by jlaskey
Published on Thu, 29 Nov 2012 15:14:31 +0000 Indexed on 2012/11/29 17:11 UTC
Read the original article Hit count: 233

Filed under:

I have been following how often Nashorn has been showing up on the net.  Nashorn got a burst of tweets when we announced Project Nashorn and I was curious how Nashorn was trending per day, maybe graph the result.  Counting tweets manually seemed mindless, so why not write a program to do the same.

This is where Nashorn + Java came shining through.  There is a very nice Java library out there called Twitter4J https://github.com/yusuke/twitter4j that handles all things Twitter.  After running bin/getAccessToken.sh to get a twitter4j.properties file with personal authorization, all I had to do to run my simple exploratory app was;

nashorn -cp $TWITTER4J/twitter4j-core-3.0.1.jar GetHomeTimeline.js

The content of GetHomeTimeline.js is as follows;

var twitter4j      = Packages.twitter4j;
var TwitterFactory = twitter4j.TwitterFactory;
var Query          = twitter4j.Query;

var twitter = new TwitterFactory().instance;
var query   = new Query("nashorn OR nashornjs");
query.count = 100;

do {
    var result = twitter.search(query);
    var tweets = result.tweets;

    for each (tweet in tweets) {
        print("@" + tweet.user.screenName + "\t" + tweet.text);
    }
} while (query = result.nextQuery());

How easy was that?  Now to hook it up to the JavaFX graphing library... 

© Oracle Blogs or respective owner

Related posts about /Personal