Nashorn in the Twitterverse

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... 

Comments:

Hi Jim,

I'm trying to use the MongoDB driver here but I can't instantiate the MongoClient class, do you have any idea why?

var mongodb = Packages.com.mongodb;
var MongoClient = mongodb.MongoClient;

var mongoClient = new MongoClient();

-- Running the test:
jjs -cp mongo-java-driver-2-9-3.jar mongo.js

-- result:
mongo.js:4 TypeError: [JavaPackage com.mongodb.MongoClient] is not a function

Posted by guest on May 20, 2013 at 03:57 AM PDT #

The Mongo documentation is out of sync with the Java repo (moved to GitHub).
Prior to 2.10, the name was Mongo, not MongoClient. You need to use the 2.10.x
jar from https://github.com/mongodb/mongo-java-driver/downloads or
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.10.0/.

var mongodb = Packages.com.mongodb;
var MongoClient = mongodb.MongoClient;

var mongo = new MongoClient("localhost");

print(mongo);

/Volumes/Elephant/Users/jlaskey/jjstest% jjs -cp mongo-2.10.1.jar mongodb.js
Mongo: localhost/127.0.0.1:27017

Cheers,

-- Jim

Posted by jlaskey on May 20, 2013 at 05:46 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Technical discussions and status of the Nashorn JavaScript Project.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today