Created
February 9, 2013 06:41
-
-
Save jasonbaldridge/4744311 to your computer and use it in GitHub Desktop.
Code to accompany tutorial for using twitter4j with Scala. http://bcomposes.wordpress.com/2013/02/09/using-twitter4j-with-scala-to-access-streaming-tweets/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bcomposes.twitter | |
import twitter4j._ | |
object StatusStreamer { | |
def main(args: Array[String]) { | |
val twitterStream = new TwitterStreamFactory(Util.config).getInstance | |
twitterStream.addListener(Util.simpleStatusListener) | |
twitterStream.sample | |
Thread.sleep(2000) | |
twitterStream.cleanUp | |
twitterStream.shutdown | |
} | |
} | |
object FollowIdsStreamer { | |
def main(args: Array[String]) { | |
val twitterStream = new TwitterStreamFactory(Util.config).getInstance | |
twitterStream.addListener(Util.simpleStatusListener) | |
twitterStream.filter(new FilterQuery(Array(1344951,5988062,807095,3108351))) | |
Thread.sleep(10000) | |
twitterStream.cleanUp | |
twitterStream.shutdown | |
} | |
} | |
object SearchStreamer { | |
def main(args: Array[String]) { | |
val twitterStream = new TwitterStreamFactory(Util.config).getInstance | |
twitterStream.addListener(Util.simpleStatusListener) | |
twitterStream.filter(new FilterQuery().track(args)) | |
Thread.sleep(10000) | |
twitterStream.cleanUp | |
twitterStream.shutdown | |
} | |
} | |
object AustinStreamer { | |
def main(args: Array[String]) { | |
val twitterStream = new TwitterStreamFactory(Util.config).getInstance | |
twitterStream.addListener(Util.simpleStatusListener) | |
val austinBox = Array(Array(-97.8,30.25),Array(-97.65,30.35)) | |
twitterStream.filter(new FilterQuery().locations(austinBox)) | |
Thread.sleep(10000) | |
twitterStream.cleanUp | |
twitterStream.shutdown | |
} | |
} | |
object LocationStreamer { | |
def main(args: Array[String]) { | |
val boundingBoxes = args.map(_.toDouble).grouped(2).toArray | |
val twitterStream = new TwitterStreamFactory(Util.config).getInstance | |
twitterStream.addListener(Util.simpleStatusListener) | |
twitterStream.filter(new FilterQuery().locations(boundingBoxes)) | |
Thread.sleep(10000) | |
twitterStream.cleanUp | |
twitterStream.shutdown | |
} | |
} | |
object Util { | |
val config = new twitter4j.conf.ConfigurationBuilder() | |
.setOAuthConsumerKey("[your consumer key here]") | |
.setOAuthConsumerSecret("[your consumer secret here]") | |
.setOAuthAccessToken("[your access token here]") | |
.setOAuthAccessTokenSecret("[your access token secret here]") | |
.build | |
def simpleStatusListener = new StatusListener() { | |
def onStatus(status: Status) { println(status.getText) } | |
def onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {} | |
def onTrackLimitationNotice(numberOfLimitedStatuses: Int) {} | |
def onException(ex: Exception) { ex.printStackTrace } | |
def onScrubGeo(arg0: Long, arg1: Long) {} | |
def onStallWarning(warning: StallWarning) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment