Tweet Controller
package controllers
import play.api.mvc.{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
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 = 10
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))
}
}
}
}
routes
GET /tweets/:query controllers.Tweets.tweetList(query: String)
scala template:
@(tweets: Seq[Tweet])
@main("Tweets!") {
Tweets:
@tweets.map { tweet =>
- @tweet.from: @tweet.text
So invoking following URL http://localhost:9000/tweets/playframework now gives me the last 10 tweets containing playframework (case insensitive). I think it's pretty slick how little code is needed.
Hey, you should be more consistent with labels. You have play and playframework already ;)
ReplyDeleteyeah... well... if you launch a play application it will tell you "Welcome to Play 2.1". But when querying twitter it makes sense to use 'playframework' as i'm not interested in every tweet with keyword 'play' ;-)
ReplyDelete