In our sample Gaelyk application here we stopped at just obtaining the location information. In this post lets update that application to fetch the Weather information as well.
also read:
For the weather information we make use of the Weather Underground API which provides a lot of features like geolocation information, current weather conditions, forecast weather conditions among others. You need to sign up to obtain the API KEY.
The URL to request the data is:
http://api.wunderground.com/api/ KEY/FEATURE/[FEATURE…]/[SETTING…]/q/ QUERY.FORMAT
where,
KEY- API key which you can obtain after sign up
FEATURE- The feature you are interested in: geolocation, conditions, astronomy, radar and so on. One can provide multiple feature data to be retrieved.
FORMAT- The format for the return data- XML or JSON
QUERY- This is the query to be used for retrieving the data- can be city name, pin code, latitude-longitude and others.
For our application we can make use of the following version of the URL:
http://api.wunderground.com/api/ ${weatherKey}/conditions/q/ ${locationInformation.cityName}.json
where,
${weatherKey}- will be replaced by the actual weather key
${locationInformation.cityName} will be replaced by actual city name.
Lets create a template- weather.gtpl for showing the Weather information. This template will be added to src/main/webapp/WEB-INF/includes.
<!-- weather.gtpl --> <h3>Weather as of ${request.forecastDate}</h3> <div class="span3"> <ul> <li> <strong>Weather</strong>: ${request.weather} </li> <li> <strong>Temperature</strong>: ${request.temperature} </li> <li> <strong>Wind</strong>: ${request.wind} </li> <li> <strong>Relative Humidity</strong>: ${request.relativeHumidity} </li> </ul> More details <a href="${request.moreUrl}" target="_blank">here</a> </div> <div class="span2"> <img src="${request.icon}" alt="${request.iconDesc}" /> </div>
And this will be included in the src/main/webapp/WEB-INF/pages/index.gtpl as:
<% include '/WEB-INF/includes/header.gtpl' %> <div class="row"> <!-- Older code here--> <div class="span6"> <% include '/WEB-INF/includes/weather.gtpl' %> </div> </div> <% include '/WEB-INF/includes/footer.gtpl' %>
Now we need to fetch the required information in the Groovlet and set it in the request object. This can be performed in the src/main/webapp/WEB-INF/groovy/index.groovy.
//index.groovy //Code from the previous sample- removed for clarity //Fetch the weather information def weatherKey="YOUR API KEY HERE" def weatherUrl = """http://api.wunderground.com/api/ ${weatherKey}/conditions/q/ ${locationInformation.cityName}.json""" def xmlSlurper = new XmlSlurper() def weatherInformation = null if (memcache[locationInformation.cityName] != null){ weatherInformation = memcache[locationInformation.cityName] } else{ weatherInformation = jsonSlurper.parseText(new URL(weatherUrl).text) //caching the weather for each hour memcache.put(locationInformation.cityName, weatherInformation, Expiration.byDeltaSeconds(3599)) } def currentObservation = weatherInformation.current_observation request.forecastDate = currentObservation.observation_time_rfc822 request.weather = currentObservation.weather request.temperature = currentObservation.temperature_string request.wind = currentObservation.wind_string request.relativeHumidity = currentObservation.relative_humidity request.moreUrl = currentObservation.ob_url request.icon = currentObservation.icon_url request.iconDesc = currentObservation.icon forward '/WEB-INF/pages/index.gtpl'
Once completed your application will look like:
If you want to play around with this code and also the previous sample developed here you can fork the git repo here and clone the repo locally. Once you are done with that just execute gradlew gaeRun on your command line to run this application.