使用akka-streams解析推文

时间:2016-09-09 18:15:29

标签: twitter akka akka-stream

我使用以下代码连接到Twitter并获取推文,但我无法创建JsValue。如果只有map byteString.utf8String我可以看到字符串。但是当我添加框架时,我得到一个错误:

Read 7925 bytes which is more than 1000 without seeing a line terminator

无论我输入多长时间,我仍然会收到此错误。我需要更改什么才能在我的websocket中获得JsValue流?

有关人们如何使用Twitter流的信息可以在here

找到
@Singleton
class TwitterController @Inject() (ws: WSClient, conf: Configuration) {

  def tweets = WebSocket.accept[JsValue,JsValue] { implicit request =>
    Flow.fromSinkAndSource(Sink.ignore, queryToSource("cat"))
  }

  def queryToSource(keyword: String): Source[JsValue, NotUsed] = {
    val url = "https://stream.twitter.com/1.1/statuses/filter.json"
    credentials.map { case (consumerKey, requestToken) =>

      val request = ws.url(url)
        .sign(OAuthCalculator(consumerKey, requestToken))
        .withQueryString("track" -> keyword)
        .withMethod("GET")

      streamResponse(request)
        .via(Framing.delimiter(ByteString("\r\n"), maximumFrameLength = 1000, allowTruncation = true))
        .map { byteString =>
          Json.parse(byteString.utf8String)
//        byteString.utf8String
        }
    } getOrElse {
      Source.single(Json.parse("Twitter credentials missing"))
    }
  }

  private def streamResponse(request:WSRequest): Source[ByteString, NotUsed] = Source.fromFuture(request.stream()).flatMapConcat(_.body)

  lazy val credentials: Option[(ConsumerKey, RequestToken)] = for {
    apiKey <- conf.getString("twitter.apiKey")
    apiSecret <- conf.getString("twitter.apiSecret")
    token <- conf.getString("twitter.token")
    tokenSecret <- conf.getString("twitter.tokenSecret")
  } yield (
    ConsumerKey(apiKey, apiSecret),
    RequestToken(token, tokenSecret)
    )
}

0 个答案:

没有答案