Use Weather Underground astronomy data for sun rise/set
Changes discussed in issue #3
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
"express": "^4.13.0",
|
||||
"grunt": "^0.4.5",
|
||||
"mongoose": "^4.0.6",
|
||||
"suncalc": "^1.6.0",
|
||||
"xml2js": "^0.4.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
var http = require( "http" ),
|
||||
SunCalc = require( "suncalc" ),
|
||||
parseXML = require( "xml2js" ).parseString,
|
||||
Cache = require( "../models/Cache" ),
|
||||
|
||||
@@ -41,39 +40,29 @@ function resolveCoordinates( location, callback ) {
|
||||
// Retrieve weather data to complete the weather request using Weather Underground
|
||||
function getWeatherUndergroundData( location, weatherUndergroundKey, callback ) {
|
||||
|
||||
// Generate URL using The Weather Company API v1 in Imperial units
|
||||
// Generate URL using Weather Underground yesterday conditions
|
||||
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey +
|
||||
"/yesterday/conditions/q/" + encodeURIComponent( location ) + ".json";
|
||||
"/yesterday/conditions/astronomy/q/" + encodeURIComponent( location ) + ".json";
|
||||
|
||||
// Perform the HTTP request to retrieve the weather data
|
||||
httpRequest( url, function( data ) {
|
||||
try {
|
||||
data = JSON.parse( data );
|
||||
|
||||
var tzOffset = getTimezone( data.current_observation.local_tz_offset, "minutes" ),
|
||||
|
||||
// Calculate sunrise and sunset since Weather Underground does not provide it
|
||||
sunData = SunCalc.getTimes( data.current_observation.local_epoch * 1000,
|
||||
data.current_observation.observation_location.latitude || data.current_observation.display_location.latitude,
|
||||
data.current_observation.observation_location.longitude || data.current_observation.display_location.longitude );
|
||||
|
||||
sunData.sunrise.setUTCMinutes( sunData.sunrise.getUTCMinutes() + tzOffset );
|
||||
sunData.sunset.setUTCMinutes( sunData.sunset.getUTCMinutes() + tzOffset );
|
||||
|
||||
var weather = {
|
||||
icon: data.current_observation.icon,
|
||||
timezone: data.current_observation.local_tz_offset,
|
||||
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
|
||||
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() ),
|
||||
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
||||
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
||||
temp: parseInt( data.current_observation.temp_f ),
|
||||
humidity: ( parseInt( data.history.dailysummary[0].maxhumidity ) + parseInt( data.history.dailysummary[0].minhumidity ) ) / 2,
|
||||
precip: ( parseFloat( data.current_observation.precip_today_in ) || 0 ) + ( parseFloat( data.history.dailysummary[0].precipi ) || 0 ),
|
||||
solar: parseInt( data.current_observation.UV ),
|
||||
wind: parseInt( data.history.dailysummary[0].meanwindspdi ),
|
||||
elevation: parseInt( data.current_observation.observation_location.elevation )
|
||||
};
|
||||
icon: data.current_observation.icon,
|
||||
timezone: data.current_observation.local_tz_offset,
|
||||
sunrise: parseInt( data.sun_phase.sunrise.hour ) * 60 + parseInt( data.sun_phase.sunrise.minute ),
|
||||
sunset: parseInt( data.sun_phase.sunset.hour ) * 60 + parseInt( data.sun_phase.sunset.minute ),
|
||||
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
||||
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
||||
temp: parseInt( data.current_observation.temp_f ),
|
||||
humidity: ( parseInt( data.history.dailysummary[0].maxhumidity ) + parseInt( data.history.dailysummary[0].minhumidity ) ) / 2,
|
||||
precip: ( parseFloat( data.current_observation.precip_today_in ) || 0 ) + ( parseFloat( data.history.dailysummary[0].precipi ) || 0 ),
|
||||
solar: parseInt( data.current_observation.UV ),
|
||||
wind: parseInt( data.history.dailysummary[0].meanwindspdi ),
|
||||
elevation: parseInt( data.current_observation.observation_location.elevation )
|
||||
};
|
||||
|
||||
callback( weather );
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
|
||||
"features": {
|
||||
"yesterday": 1,
|
||||
"conditions": 1
|
||||
"conditions": 1,
|
||||
"astronomy": 1
|
||||
}
|
||||
},
|
||||
"current_observation": {
|
||||
@@ -107,6 +108,34 @@
|
||||
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=42.408440,-72.504799",
|
||||
"nowcast": ""
|
||||
},
|
||||
"moon_phase": {
|
||||
"percentIlluminated": "48",
|
||||
"ageOfMoon": "7",
|
||||
"phaseofMoon": "Waxing Crescent",
|
||||
"hemisphere": "North",
|
||||
"current_time": {
|
||||
"hour": "18",
|
||||
"minute": "08"
|
||||
},
|
||||
"sunrise": {
|
||||
"hour": "6",
|
||||
"minute": "27"
|
||||
},
|
||||
"sunset": {
|
||||
"hour": "20",
|
||||
"minute": "23"
|
||||
}
|
||||
},
|
||||
"sun_phase": {
|
||||
"sunrise": {
|
||||
"hour": "5",
|
||||
"minute": "24"
|
||||
},
|
||||
"sunset": {
|
||||
"hour": "20",
|
||||
"minute": "28"
|
||||
}
|
||||
},
|
||||
"history": {
|
||||
"date": {
|
||||
"pretty": "July 11, 2015",
|
||||
|
||||
Reference in New Issue
Block a user