Skip to content

Instantly share code, notes, and snippets.

@mehalter
Created March 30, 2016 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehalter/363192861d0f999b2178f859f23102e7 to your computer and use it in GitHub Desktop.
Save mehalter/363192861d0f999b2178f859f23102e7 to your computer and use it in GitHub Desktop.
Twitter API Stream
package com.mehalter
import twitter4j._
object StatusStreamer {
def main(args: Array[String]): Unit = {
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.sample //sample of tweets
//twitterStream.filter(new FilterQuery(Array(2803971569L))) //Twitter IDs
//twitterStream.filter(new FilterQuery().track(Array("computer science"))) //Track by specific terms
twitterStream.filter(new FilterQuery().locations(austinBox)) //filter by locations
Thread.sleep(10000) //how long to be streaming
twitterStream.cleanUp
twitterStream.shutdown
}
}
object Util {
val config = new twitter4j.conf.ConfigurationBuilder()
.setOAuthConsumerKey("lxcQj5cTHanXBFpJTW42hy62i")
.setOAuthConsumerSecret("WbFyYpN1D1d3uAVeK80sbY0bxCuxJz7zL2aCeJLXAG3VLwIqQL")
.setOAuthAccessToken("2803971569-RetGcDFtpV7of6Akx0m69JocfYvuEQx0ykzuQVj")
.setOAuthAccessTokenSecret("RAn8Ijemr4ZgR3rgj4F4N4zDNN9M0ohNRGF7z5v8cXDGy")
.build()
def simpleStatusListener = new StatusListener {override def onStallWarning(warning: StallWarning): Unit = ???
override def onDeletionNotice(statusDeletionNotice: StatusDeletionNotice): Unit = {}
override def onScrubGeo(userId: Long, upToStatusId: Long): Unit = {}
override def onStatus(status: Status): Unit = println(status.getText)
override def onTrackLimitationNotice(numberOfLimitedStatuses: Int): Unit = {}
override def onException(ex: Exception): Unit = {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment