Advertisement
Guest User

Untitled

a guest
Jun 10th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.59 KB | None | 0 0
  1. package controllers
  2.  
  3. import play.api._
  4. import play.api.mvc._
  5. import play.api.libs.ws.WS
  6. import scala.concurrent.Await
  7. import scala.concurrent.duration.Duration
  8. import models.Usuario
  9. import scala.concurrent._
  10. import play.api.libs.json.JsArray
  11. import play.api.libs.json.JsArray
  12. import play.api.libs.json.JsArray
  13. import models.Previsao
  14. import models.ResultadoPrevisao
  15.  
  16.  
  17. object Application extends Controller {
  18.  
  19.   implicit val context = scala.concurrent.ExecutionContext.Implicits.global
  20.   val clientID = "34032624077-revgatqad5qtljp98n8g2d8d70fpnf5c.apps.googleusercontent.com"
  21.   val clientSecret = "5PprD1ZXJuA1SORgNjExxKzQ"
  22.   val callbackURL = "http://localhost:9000/callback"
  23.   val loginUrl = s"https://accounts.google.com/o/oauth2/auth?client_id=$clientID&response_type=code&scope=openid+email&redirect_uri=$callbackURL"
  24.   val formate = new java.text.SimpleDateFormat("dd/MM/yyyy")
  25.   var usuarioP = new Usuario("", "");
  26.   def index = Action {
  27.     Ok(views.html.index(loginUrl))
  28.   }
  29.  
  30.   def callback = Action.async{
  31.   request =>    val optCode: Option[String] = request.getQueryString("code")
  32.     optCode match {
  33.       case None => future { Ok(views.html.index(loginUrl)) }
  34.       case Some(code) => {
  35.           val params = Map("code" -> Seq(code),
  36.                            "client_id" -> Seq(clientID),
  37.                            "client_secret" -> Seq(clientSecret),
  38.                            "redirect_uri" -> Seq(callbackURL),
  39.                            "grant_type" -> Seq("authorization_code"))
  40.  
  41.           val tokenRequest = WS.url("https://accounts.google.com/o/oauth2/token").post(params)
  42.           for {
  43.              resposta <- tokenRequest
  44.              respostaDados <- {
  45.                 val accessToken = (resposta.json \ "access_token").as[String]
  46.                 val url = s"https://www.googleapis.com/oauth2/v2/userinfo?access_token=$accessToken"
  47.                 WS.url(url).get
  48.              }
  49.           } yield {
  50.             val respJson = respostaDados.json
  51.             val usuario = new Usuario((respJson \ "picture").as[String], (respJson \ "name").as[String])
  52.             usuarioP = usuario;
  53.             Ok(views.html.pesquisa(usuario, Left("")))
  54.           }
  55.         }
  56.       }
  57.   }
  58.  
  59.   def pesquise(cidade: String, dia: String) = Action.async { request =>
  60.    
  61.         val diasMaisUm = dia.toInt + 1
  62.         val city = java.net.URLEncoder.encode(cidade, "UTF-8")
  63.         val url = s"http://api.openweathermap.org/data/2.5/forecast/daily/?q=$city&lang=pt&units=metric&cnt=$diasMaisUm&APPID=926700d7e887255b946acb938a6bdf20"
  64.  
  65.    
  66.         WS.url(url).get.map { resposta =>
  67.         val jsonResp = resposta.json
  68.      
  69.         val cidadeEncontrada: String = (jsonResp \ "city" \ "name").as[String]
  70.         val pais: String = (jsonResp \ "city" \ "country").as[String]
  71.         val lista = (jsonResp \ "list").asInstanceOf[JsArray].value.tail
  72.        
  73.        val previsoes = for(previsao <- lista)
  74.          yield {
  75.         val weather = (previsao \ "weather").asInstanceOf[JsArray](0)
  76.         val icon = (weather \ "icon").as[String]
  77.         val url_icon = s"http://openweathermap.org/img/w/$icon.png"
  78.          val data = formate.format(new java.util.Date(1000L * (previsao \ "dt").as[Long]))
  79.         val descricao = (weather \ "description").as[String]
  80.         val tempMin= (previsao \ "temp" \ "min").as[Float]
  81.         val tempMax = (previsao \ "temp" \ "max").as[Float]
  82.         val umidade = (previsao \ "humidity").as[Float]
  83.        
  84.         new Previsao(url_icon, data, descricao, tempMin, tempMax, umidade)
  85.        }
  86.        
  87.        val resultadoPesquisa = new ResultadoPrevisao(cidadeEncontrada, pais, previsoes.toList)
  88.        
  89.        
  90.         Ok(views.html.pesquisa(usuarioP, Right(resultadoPesquisa)))
  91.     }
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement