Adventures with Twitter in Groovy

Have you ever wanted to get a better sense of an event’s commentary on Twitter? For example, a subset of your following may be attending an event in your periphery but making interesting enough comments to warrant a closer look.

Well, I’ve had these thoughts on an increasing basis lately so I thought I’d dive into the Twitter stream a bit to see what I could learn.

Fortunately, I was aware of a gist to query Twitter by @pmonks, and this Groovy script became the foundation for my exploration.

Since I don’t normally program in Groovy, I had to setup Groovy on my MacBook Pro. (To be clear, I went for “get this to work” rather than “accomplish your configuration with all best practices incorporated.” I’m sure there may be better techniques where Groovy setup on MacOS is concerned, and I thank you in advance for any comment-based pointers.)

  1. Visit the Groovy website and understand basic Groovy installation instructions.
  2. Downloaded the binary release (Zip) of Groovy 1.8.6 (i.e. latest available version of the stable branch at the time of this post).
  3. Extracted Zip contents and moved (via Terminal) them under my up-to-date Java distribution:
    prompt$ sudo mv ~/downloads/groovy-1.8.6 /usr/share/java

    (I’ve seen apparent best practice guidance to create a symlink that allows you to access Groovy without referencing the version number, but I’ve skipped that here.)

  4. Edit your .profile file to point your environment to your new Groovy installation. (If you don’t have a .profile file, read and follow this.)
    prompt$ open /Applications/TextEdit.app .profile

    Add (or confirm) the following lines in your .profile file:

    JAVA_HOME=/Library/Java/Home; export JAVA_HOME
    GROOVY_HOME=/usr/share/java/groovy-1.8.6; export GROOVY_HOME
    PATH=$GROOVY_HOME/bin:$PATH; export PATH

    Save your .profile file, exit TextEdit, close all open Terminal windows, exit Terminal and open a new Terminal window to enable .profile changes to take effect.

  5. Confirm that Groovy is ready to act on your script:
    prompt$ groovy -v

    My confirmation looked as follows:
    Groovy Version: 1.8.6 JVM: 1.6.0_31 Vendor: Apple Inc. OS: Mac OS X

For this particular script, queryTwitter.groovy, you need to provide a peer twitter4j.properties file that will provide to Twitter your username and password. Once that’s ready,

prompt$ groovy queryTwitter.groovy %23interesting_event

The results are ready to be nested within a table element, if you want to produce HTML for display as-is. However, you could also modify the script to render JSON output and pursue alternative visualizations (e.g. rate of tweets over time, geolocation of individuals in the tweetstream, word clouds for the event, influencer status, etc.)–something for D3.js.

-Craig
http://craigrandall.net/
@craigsmusings