Improve request to OWM

This commit is contained in:
Samer Albahra
2018-05-20 17:06:23 -05:00
parent aa266d5d8b
commit b332eaaf14

View File

@@ -223,13 +223,12 @@ function getOWMWeatherData( location, callback ) {
// Generate URL using The Weather Company API v1 in Imperial units // Generate URL using The Weather Company API v1 in Imperial units
var OWM_API_KEY = process.env.OWM_API_KEY, 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 ]; 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 ) { getTimeData( location, function( weather ) {
// Perform the HTTP request to retrieve the weather data // Perform the HTTP request to retrieve the weather data
httpRequest( currentUrl, function( data ) { httpRequest( forecastUrl, function( data ) {
try { try {
data = JSON.parse( data ); data = JSON.parse( data );
} catch ( err ) { } catch ( err ) {
@@ -237,21 +236,21 @@ function getOWMWeatherData( location, callback ) {
callback( weather ); callback( weather );
} }
weather.temp = parseInt( data.main.temp ); weather.temp = parseInt( data.list[ 0 ].main.temp );
weather.humidity = parseInt( data.main.humidity ); weather.humidity = parseInt( data.list[ 0 ].main.humidity );
weather.wind = parseInt( data.wind.speed ); weather.wind = parseInt( data.list[ 0 ].wind.speed );
weather.precip = data.list[ 0 ].rain[ "3h" ];
httpRequest( forecastUrl, function( forecastData ) { location = location.join( "," );
try {
forecastData = JSON.parse( forecastData );
weather.precip = forecastData.list[ 0 ].rain[ "3h" ];
callback( weather );
} catch ( err ) {
// Otherwise indicate the request failed getCache( {
callback( weather ); key: "yesterdayHumidity",
} location: location,
weather: weather,
callback: callback
} ); } );
updateCache( location, weather );
} ); } );
} ); } );
} }