Use Weather Underground for data when PWS or ICAO are used

This commit is contained in:
Samer Albahra
2015-07-03 18:36:12 -05:00
parent d4b7c2db16
commit 21e579f0e1

View File

@@ -12,22 +12,6 @@ var http = require( "http" ),
timezone: /^()()()()()()([+-])(\d{2})(\d{2})/ timezone: /^()()()()()()([+-])(\d{2})(\d{2})/
}; };
// Takes a PWS or ICAO location and resolves the GPS coordinates
function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey +
"/conditions/forecast/q/" + encodeURIComponent( location ) + ".json";
httpRequest( url, function( data ) {
data = JSON.parse( data );
if ( typeof data === "object" && data.current_observation && data.current_observation.observation_location ) {
callback( [ data.current_observation.observation_location.latitude,
data.current_observation.observation_location.longitude ] );
} else {
callback( false );
}
} );
}
// If location does not match GPS or PWS/ICAO, then attempt to resolve // If location does not match GPS or PWS/ICAO, then attempt to resolve
// location using Weather Underground autocomplete API // location using Weather Underground autocomplete API
function resolveCoordinates( location, callback ) { function resolveCoordinates( location, callback ) {
@@ -344,32 +328,18 @@ exports.getWeather = function( req, res ) {
} }
// Parse location string // Parse location string
if ( filters.pws.test( location ) ) { if ( weatherUndergroundKey ) {
// Handle locations using PWS or ICAO (Weather Underground)
if ( !weatherUndergroundKey ) {
// If no key is provided for Weather Underground then the PWS or ICAO cannot be resolved
res.send( "Error: Weather Underground key required when using PWS or ICAO location." );
return;
}
getPWSCoordinates( location, weatherUndergroundKey, function( result ) {
if ( result === false ) {
res.send( "Error: Unable to resolve location" );
return;
}
location = result;
getWeatherData( location, finishRequest );
} );
} else if ( weatherUndergroundKey ) {
// The current weather script uses Weather Underground and during the transition period // The current weather script uses Weather Underground and during the transition period
// both will be supported and users who provide a Weather Underground API key will continue // both will be supported and users who provide a Weather Underground API key will continue
// using Weather Underground until The Weather Service becomes the default API // using Weather Underground until The Weather Service becomes the default API
getWeatherUndergroundData( location, weatherUndergroundKey, finishRequest ); getWeatherUndergroundData( location, weatherUndergroundKey, finishRequest );
} else if ( filters.pws.test( location ) ) {
// If no key is provided for Weather Underground then the PWS or ICAO cannot be resolved
res.send( "Error: Weather Underground key required when using PWS or ICAO location." );
return;
} else if ( filters.gps.test( location ) ) { } else if ( filters.gps.test( location ) ) {
// Handle GPS coordinates by storing each coordinate in an array // Handle GPS coordinates by storing each coordinate in an array