Command line Twitter search in Groovy and Scala

by 10/24/2011 02:18:00 PM 0 comments
I found a nice post about doing command line searches of Twitter using Groovy. In a nutshell the code was this: twitsearch.groovy
#!/usr/bin/env groovy
/*
  From http://www.ibm.com/developerworks/java/library/j-groovy09299/
 */
 
if (args) {
    def queryterm = args[0]
    def q = "http://search.twitter.com/search.atom?q=${queryterm}"
    def feed = new XmlSlurper().parse(q)
    feed.entry.each { entry ->
        println  """\
${entry.author.name} - ${entry.published}
\"${entry.title}\"
${'-' * 72}"""
    }
}
else {
    println  "USAGE: twitsearch.groovy "
}
I tried the same thing in Scala: twitsearch.scala
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#

if (args.size > 0) {
    import java.net.{URLConnection, URL}
    import scala.xml._
    val queryterm = args(0)
    val q = "http://search.twitter.com/search.atom?q=" + queryterm
    val feed = XML.load(new URL(q))
    feed \ "entry" foreach { entry =>
        println((entry \\ "author" \\ "name" text) + " - " + (entry \\ "published" text))
        println("\"" + (entry \\ "title" text) + "\"")
        println("-" * 72 )
    }
}
else {
    println("USAGE: twitsearch.scala ")
}

hohonuuli

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments: