Fix issues sourcing data from weather.com by using OpenWeatherMaps
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
"moment-timezone": "^0.4.0",
|
"moment-timezone": "^0.4.0",
|
||||||
"mongoose": "^4.0.6",
|
"mongoose": "^4.0.6",
|
||||||
"suncalc": "^1.6.0",
|
"suncalc": "^1.6.0",
|
||||||
|
"timezoner": "^0.1.9",
|
||||||
"xml2js": "^0.4.9"
|
"xml2js": "^0.4.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ var http = require( "http" ),
|
|||||||
Cache = require( "../models/Cache" ),
|
Cache = require( "../models/Cache" ),
|
||||||
SunCalc = require( "suncalc" ),
|
SunCalc = require( "suncalc" ),
|
||||||
moment = require( "moment-timezone" ),
|
moment = require( "moment-timezone" ),
|
||||||
|
timezoner = require( "timezoner" ),
|
||||||
|
|
||||||
// Define regex filters to match against location
|
// Define regex filters to match against location
|
||||||
filters = {
|
filters = {
|
||||||
@@ -216,6 +217,57 @@ function getYesterdayWeatherData( location, callback ) {
|
|||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve weather data from Open Weather Map
|
||||||
|
function getOWMWeatherData( location, callback ) {
|
||||||
|
|
||||||
|
// Generate URL using The Weather Company API v1 in Imperial units
|
||||||
|
var url = "http://api.openweathermap.org/data/2.5/weather?units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ];
|
||||||
|
|
||||||
|
// Perform the HTTP request to retrieve the weather data
|
||||||
|
httpRequest( url, function( data ) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
data = JSON.parse( data );
|
||||||
|
var sunrise = new Date( data.sys.sunrise * 1000 ),
|
||||||
|
sunset = new Date( data.sys.sunset * 1000 );
|
||||||
|
|
||||||
|
timezoner.getTimeZone(
|
||||||
|
location[ 0 ],
|
||||||
|
location[ 1 ],
|
||||||
|
function( err, timezone ) {
|
||||||
|
if ( err ) {
|
||||||
|
callback( false );
|
||||||
|
} else {
|
||||||
|
var weather = {
|
||||||
|
timezone: ( timezone.rawOffset + timezone.dstOffset ) / 60,
|
||||||
|
sunrise: ( sunrise.getHours() * 60 + sunrise.getMinutes() ),
|
||||||
|
sunset: ( sunset.getHours() * 60 + sunset.getMinutes() ),
|
||||||
|
temp: parseInt( data.main.temp ),
|
||||||
|
humidity: parseInt( data.main.humidity ),
|
||||||
|
wind: parseInt( data.wind.speed )
|
||||||
|
};
|
||||||
|
|
||||||
|
getCache( {
|
||||||
|
key: "yesterdayHumidity",
|
||||||
|
location: location,
|
||||||
|
weather: weather,
|
||||||
|
callback: callback
|
||||||
|
} );
|
||||||
|
|
||||||
|
updateCache( location, weather );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch ( err ) {
|
||||||
|
|
||||||
|
// Otherwise indicate the request failed
|
||||||
|
callback( false );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve cached record for a given location
|
// Retrieve cached record for a given location
|
||||||
// opt is defined as an object with two required items
|
// opt is defined as an object with two required items
|
||||||
// opt.location defines the location for the cache record
|
// opt.location defines the location for the cache record
|
||||||
@@ -458,7 +510,7 @@ exports.getWeather = function( req, res ) {
|
|||||||
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
||||||
|
|
||||||
// Continue with the weather request
|
// Continue with the weather request
|
||||||
getWeatherData( location, finishRequest );
|
getOWMWeatherData( location, finishRequest );
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user