From b332eaaf14edf5238d41120cf7c30606b9f5e6c4 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Sun, 20 May 2018 17:06:23 -0500 Subject: [PATCH] Improve request to OWM --- routes/weather.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index bfb1220..304565e 100755 --- a/routes/weather.js +++ b/routes/weather.js @@ -223,13 +223,12 @@ function getOWMWeatherData( location, callback ) { // Generate URL using The Weather Company API v1 in Imperial units var OWM_API_KEY = process.env.OWM_API_KEY, - currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ], forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ]; getTimeData( location, function( weather ) { // Perform the HTTP request to retrieve the weather data - httpRequest( currentUrl, function( data ) { + httpRequest( forecastUrl, function( data ) { try { data = JSON.parse( data ); } catch ( err ) { @@ -237,21 +236,21 @@ function getOWMWeatherData( location, callback ) { callback( weather ); } - weather.temp = parseInt( data.main.temp ); - weather.humidity = parseInt( data.main.humidity ); - weather.wind = parseInt( data.wind.speed ); + weather.temp = parseInt( data.list[ 0 ].main.temp ); + weather.humidity = parseInt( data.list[ 0 ].main.humidity ); + weather.wind = parseInt( data.list[ 0 ].wind.speed ); + weather.precip = data.list[ 0 ].rain[ "3h" ]; - httpRequest( forecastUrl, function( forecastData ) { - try { - forecastData = JSON.parse( forecastData ); - weather.precip = forecastData.list[ 0 ].rain[ "3h" ]; - callback( weather ); - } catch ( err ) { + location = location.join( "," ); - // Otherwise indicate the request failed - callback( weather ); - } + getCache( { + key: "yesterdayHumidity", + location: location, + weather: weather, + callback: callback } ); + + updateCache( location, weather ); } ); } ); }