How to read response data from Twitter Streaming API - POST statuses/filter ? I have established connection and I receive 200 status code, but I don't know how to read tweets. I just want to println tweets as they coming.
ws.url(url)
.sign(OAuthCalculator(consumerKey, requestToken))
.withMethod("POST")
.stream()
.map { response =>
if(response.headers.status == 200)
println(response.body)
}
EDIT: I found this solution
ws.url(url)
.sign(OAuthCalculator(consumerKey, requestToken))
.withMethod("POST")
.stream()
.map { response =>
if(response.headers.status == 200){
response.body
.scan("")((acc, curr) => if (acc.contains("\r\n")) curr.utf8String else acc + curr.utf8String)
.filter(_.contains("\r\n"))
.map(json => Try(parse(json).extract[Tweet]))
.runForeach {
case Success(tweet) =>
println("-----")
println(tweet.text)
case Failure(e) =>
println("-----")
println(e.getStackTrace)
}
}
}
calling
stream()
gives you back aFuture[StreamedResponse]
. you'll then have to use akka idioms to convert theByteString
chunks within that. something like:note that i didn't test the code above (but it's based off of the streaming response section of https://www.playframework.com/documentation/2.5.x/ScalaWS plus the sink description from http://doc.akka.io/docs/akka/2.4.2/scala/stream/stream-flows-and-basics.html)
also note that this will print each chunk on its own line, and i'm not sure if the twitter API gives back complete json blobs per chunk. you may need to use a
Sink.fold
if you want to accumulate chunks before printing them.The body of the response for a streaming WS request is an Akka Streams
Source
of bytes. Since Twitter Api responses are newline delimited (usually) you can useFraming.delimiter
to split them up into byte chunks, parse the chunks to JSON, and do what you want with them. Something like this should work:Note: to materialize the stream you'll need to inject an implicit
Materializer
into your controller.