Last active
December 21, 2015 14:59
-
-
Save petrovg/6323747 to your computer and use it in GitHub Desktop.
Consuming a twitter feed with OAuth
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
object twitter { | |
import play.api.libs.ws._ | |
import play.api.libs.iteratee._ | |
import play.api.libs.oauth._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
val key = ConsumerKey("XXXX", "XXXX") | |
val twitter = OAuth(ServiceInfo( | |
"https://api.twitter.com/oauth/request_token", | |
"https://api.twitter.com/oauth/access_token", | |
"https://api.twitter.com/oauth/authorize", key), | |
false) | |
val readBBC = () => { | |
WS.url("http://www.bbc.co.uk").get( resp => Iteratee.fold(0) { (i:Int, el:Array[Byte] ) => println(el); i + 1} ) | |
} | |
val auth = OAuthCalculator(twitter.key, RequestToken("XXXX", "XXXX")) | |
} | |
// In console, with Play on the classpath | |
import play.api.libs.ws._ | |
import play.api.libs.iteratee._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
// Plain read | |
WS.url("https://api.twitter.com/1.1/statuses/home_timeline.json").sign(twitter.auth).get.map(_.json).map (println) | |
// Iteratee read | |
WS.url("https://api.twitter.com/1.1/statuses/home_timeline.json").sign(twitter.auth).get( resp => Iteratee.fold(0) { (i:Int, el:Array[Byte] ) => println(el); i + 1} ) | |
// Iteratee read from the streaming sample (showing the total bytes received so far | |
// (How do you stop this?) | |
WS.url("https://stream.twitter.com/1.1/statuses/sample.json").sign(twitter.auth).get( resp => Iteratee.fold(0) { (i:Int, el:Array[Byte] ) => println("]] " + i + el.length); i + el.length} ) | |
// Or, a bit more organized, make a signed request and a counter | |
// Note that this tries to stop it after a while, though a bit clumsily by resetting the client | |
val signedReq = WS.url("https://stream.twitter.com/1.1/statuses/sample.json").sign(twitter.auth) | |
val counter = { r:ResponseHeaders => Iteratee.fold(0) { (count:Int, chunk:Array[Byte]) => if (count < 100000) { println(count); count + chunk.length } else { WS.resetClient(); 0} } } | |
// And then | |
signedReq.get(counter) | |
// JSON | |
// Now let's get some Json out of it | |
import play.api.libs.json._ | |
val timestampPrinter = { r:ResponseHeaders => Iteratee.fold(0) { (count:Int, chunk:Array[Byte]) => if (count < 100000) { val j = Json.parse(chunk) \ "created_at"; println(s"$count, $j"); count + chunk.length } else { WS.resetClient(); 0} } } | |
// And here's one that prints the tweet as they are streamed. j is for json, t is for tweet | |
val tweetPrinter = { r:ResponseHeaders => Iteratee.fold(0) { (count:Int, chunk:Array[Byte]) => if (count < 20) { val j = Json.parse(chunk); val t = (j\"created_at", j\"id", j\"text"); println(s"$count, $t"); count + 1 } else { WS.resetClient(); 0} } } | |
// Don't forget to actually run it! | |
signedReq.get(tweetPrinter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment