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",
|
"express": "^4.13.0",
|
||||||
"grunt": "^0.4.5",
|
"grunt": "^0.4.5",
|
||||||
"mongoose": "^4.0.6",
|
"mongoose": "^4.0.6",
|
||||||
"suncalc": "^1.6.0",
|
|
||||||
"xml2js": "^0.4.9"
|
"xml2js": "^0.4.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
var http = require( "http" ),
|
var http = require( "http" ),
|
||||||
SunCalc = require( "suncalc" ),
|
|
||||||
parseXML = require( "xml2js" ).parseString,
|
parseXML = require( "xml2js" ).parseString,
|
||||||
Cache = require( "../models/Cache" ),
|
Cache = require( "../models/Cache" ),
|
||||||
|
|
||||||
@@ -41,30 +40,20 @@ function resolveCoordinates( location, callback ) {
|
|||||||
// Retrieve weather data to complete the weather request using Weather Underground
|
// Retrieve weather data to complete the weather request using Weather Underground
|
||||||
function getWeatherUndergroundData( location, weatherUndergroundKey, callback ) {
|
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 +
|
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
|
// Perform the HTTP request to retrieve the weather data
|
||||||
httpRequest( url, function( data ) {
|
httpRequest( url, function( data ) {
|
||||||
try {
|
try {
|
||||||
data = JSON.parse( data );
|
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 = {
|
var weather = {
|
||||||
icon: data.current_observation.icon,
|
icon: data.current_observation.icon,
|
||||||
timezone: data.current_observation.local_tz_offset,
|
timezone: data.current_observation.local_tz_offset,
|
||||||
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
|
sunrise: parseInt( data.sun_phase.sunrise.hour ) * 60 + parseInt( data.sun_phase.sunrise.minute ),
|
||||||
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() ),
|
sunset: parseInt( data.sun_phase.sunset.hour ) * 60 + parseInt( data.sun_phase.sunset.minute ),
|
||||||
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
||||||
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
||||||
temp: parseInt( data.current_observation.temp_f ),
|
temp: parseInt( data.current_observation.temp_f ),
|
||||||
|
|||||||
@@ -20,7 +20,8 @@
|
|||||||
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
|
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
|
||||||
"features": {
|
"features": {
|
||||||
"yesterday": 1,
|
"yesterday": 1,
|
||||||
"conditions": 1
|
"conditions": 1,
|
||||||
|
"astronomy": 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"current_observation": {
|
"current_observation": {
|
||||||
@@ -107,6 +108,34 @@
|
|||||||
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=42.408440,-72.504799",
|
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=42.408440,-72.504799",
|
||||||
"nowcast": ""
|
"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": {
|
"history": {
|
||||||
"date": {
|
"date": {
|
||||||
"pretty": "July 11, 2015",
|
"pretty": "July 11, 2015",
|
||||||
|
|||||||
Reference in New Issue
Block a user