我正在使用来自@diegolparra 的示例来进行推特搜索和/或流式传输。
package controllers
import play.api.mvc.{WebSocket, Action, Controller}
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.ws.WS
import play.api.libs.iteratee.{Iteratee, Concurrent, Enumerator}
import play.api.libs.oauth.{OAuthCalculator, RequestToken, ConsumerKey}
case class Tweet(from: String, text: String)
object Tweets extends Controller {
implicit val tweetReads = (
(__ \ "from_user_name").read[String] and
(__ \ "text").read[String]
)(Tweet)
def tweetList(query: String) = Action {
Async {
val results = 50
val responsePromise =
WS.url("http://search.twitter.com/search.json")
.withQueryString("q" -> query, "rpp" -> results.toString).get
responsePromise.map {
response =>
val tweets = Json.parse(response.body).\("results").as[Seq[Tweet]]
Ok(views.html.tweetlist(tweets))
}
}
}
val consumerKey = ConsumerKey("EBcP4MM9VnI64L8RZLO7g","SVi7XyyNpWzidR2Zx2HVNZ7kZTwFGxpqGKqhOeA")
val accessToken = RequestToken("1228081488-sLSztNAm0ST2kssCkBNRyhSsmk8SP5dtcbX1ZE2", "GxHUymXdyTYRZxw4bbTbgN8Xh53jKxC1KwvgsVwUU")
def stream(keywords: String) = WebSocket.using[String] { request =>
val out: Enumerator[String] = Concurrent.unicast[String](onStart = pushee => {
def twitterIteratee = Iteratee.foreach[Array[Byte]]{ ba =>
val msg = new String(ba, "UTF-8")
pushee.push(msg)
println(msg)
}
WS.url("https://stream.twitter.com/1.1/statuses/filter.json?track=" + keywords)
.sign(OAuthCalculator(consumerKey, accessToken))
.get(headers=> twitterIteratee)
})
val in = Iteratee.ignore[String]
(in, out)
}
}
此示例从 twitter 搜索字段的结果中获取 from_user_name 和 text。现在我需要访问 created_at 日期和另一个级别(节点用户)以从用户获取:profile_image_url 和 lang。
请问,有什么帮助吗?
谢谢。