From 9ef4cb759b6a3298ced96cff00d1f0227986159f Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 23 Jul 2015 18:25:23 -0500 Subject: [PATCH] Use Weather Underground astronomy data for sun rise/set Changes discussed in issue #3 --- package.json | 1 - routes/weather.js | 41 +++++++++++++++-------------------------- test/replies.json | 31 ++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index b1899e8..092d436 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/routes/weather.js b/routes/weather.js index 1598249..ccf5e96 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -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 ); diff --git a/test/replies.json b/test/replies.json index c4c16dc..33f170e 100644 --- a/test/replies.json +++ b/test/replies.json @@ -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",