Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controllers
- import play.api._
- import play.api.mvc._
- import play.api.libs.ws.WS
- import scala.concurrent.Await
- import scala.concurrent.duration.Duration
- import models.Usuario
- import scala.concurrent._
- import play.api.libs.json.JsArray
- import play.api.libs.json.JsArray
- import play.api.libs.json.JsArray
- import models.Previsao
- import models.ResultadoPrevisao
- object Application extends Controller {
- implicit val context = scala.concurrent.ExecutionContext.Implicits.global
- val clientID = "34032624077-revgatqad5qtljp98n8g2d8d70fpnf5c.apps.googleusercontent.com"
- val clientSecret = "5PprD1ZXJuA1SORgNjExxKzQ"
- val callbackURL = "http://localhost:9000/callback"
- val loginUrl = s"https://accounts.google.com/o/oauth2/auth?client_id=$clientID&response_type=code&scope=openid+email&redirect_uri=$callbackURL"
- val formate = new java.text.SimpleDateFormat("dd/MM/yyyy")
- var usuarioP = new Usuario("", "");
- def index = Action {
- Ok(views.html.index(loginUrl))
- }
- def callback = Action.async{
- request => val optCode: Option[String] = request.getQueryString("code")
- optCode match {
- case None => future { Ok(views.html.index(loginUrl)) }
- case Some(code) => {
- val params = Map("code" -> Seq(code),
- "client_id" -> Seq(clientID),
- "client_secret" -> Seq(clientSecret),
- "redirect_uri" -> Seq(callbackURL),
- "grant_type" -> Seq("authorization_code"))
- val tokenRequest = WS.url("https://accounts.google.com/o/oauth2/token").post(params)
- for {
- resposta <- tokenRequest
- respostaDados <- {
- val accessToken = (resposta.json \ "access_token").as[String]
- val url = s"https://www.googleapis.com/oauth2/v2/userinfo?access_token=$accessToken"
- WS.url(url).get
- }
- } yield {
- val respJson = respostaDados.json
- val usuario = new Usuario((respJson \ "picture").as[String], (respJson \ "name").as[String])
- usuarioP = usuario;
- Ok(views.html.pesquisa(usuario, Left("")))
- }
- }
- }
- }
- def pesquise(cidade: String, dia: String) = Action.async { request =>
- val diasMaisUm = dia.toInt + 1
- val city = java.net.URLEncoder.encode(cidade, "UTF-8")
- val url = s"http://api.openweathermap.org/data/2.5/forecast/daily/?q=$city&lang=pt&units=metric&cnt=$diasMaisUm&APPID=926700d7e887255b946acb938a6bdf20"
- WS.url(url).get.map { resposta =>
- val jsonResp = resposta.json
- val cidadeEncontrada: String = (jsonResp \ "city" \ "name").as[String]
- val pais: String = (jsonResp \ "city" \ "country").as[String]
- val lista = (jsonResp \ "list").asInstanceOf[JsArray].value.tail
- val previsoes = for(previsao <- lista)
- yield {
- val weather = (previsao \ "weather").asInstanceOf[JsArray](0)
- val icon = (weather \ "icon").as[String]
- val url_icon = s"http://openweathermap.org/img/w/$icon.png"
- val data = formate.format(new java.util.Date(1000L * (previsao \ "dt").as[Long]))
- val descricao = (weather \ "description").as[String]
- val tempMin= (previsao \ "temp" \ "min").as[Float]
- val tempMax = (previsao \ "temp" \ "max").as[Float]
- val umidade = (previsao \ "humidity").as[Float]
- new Previsao(url_icon, data, descricao, tempMin, tempMax, umidade)
- }
- val resultadoPesquisa = new ResultadoPrevisao(cidadeEncontrada, pais, previsoes.toList)
- Ok(views.html.pesquisa(usuarioP, Right(resultadoPesquisa)))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement