Skip to content
Browse files

more wunderground data, settings file

  • Loading branch information...
1 parent eafc315 commit e603587b769ce55e79ab5bae5f45fb8baa804c3f Max Henstell committed Oct 5, 2012
Showing with 75 additions and 25 deletions.
  1. +15 −7 server/weather-script-preprocess.svg
  2. +53 −16 server/weather-script.py
  3. +7 −2 server/weather-script.sh
View
22 server/weather-script-preprocess.svg
@@ -46,19 +46,27 @@
<text style="text-anchor:end;" font-size="100px" y="120" x="550">CURRENT</text>
<text style="text-anchor:middle;" font-size="35px" y="60" x="565">°F</text>
+<text style="text-anchor:end;" font-size="30px" y="160" x="570">ALERT</text>
+
+<text style="text-anchor:end;" font-size="17px" y="795" x="590">TIME</text>
+
<text style="text-anchor:start;" font-size="35px" y="200" x="375">High:</text>
<text style="text-anchor:start;" font-size="35px" y="200" x="500">HIGH_ONE</text>
<text style="text-anchor:middle;" font-size="25px" y="190" x="560">°F</text>
-<text style="text-anchor:start;" font-size="35px" y="260" x="375">Low:</text>
-<text style="text-anchor:start;" font-size="35px" y="260" x="500">LOW_ONE</text>
-<text style="text-anchor:middle;" font-size="25px" y="250" x="560">°F</text>
+<text style="text-anchor:start;" font-size="35px" y="245" x="375">Low:</text>
+<text style="text-anchor:start;" font-size="35px" y="245" x="500">LOW_ONE</text>
+<text style="text-anchor:middle;" font-size="25px" y="235" x="560">°F</text>
+
+<text style="text-anchor:start;" font-size="35px" y="290" x="375">POP_BOOL</text>
+<text style="text-anchor:start;" font-size="35px" y="290" x="500">POP_NUM</text>
+
-<text style="text-anchor:start;" font-size="35px" y="320" x="375">RAIN_BOOL</text>
-<text style="text-anchor:start;" font-size="35px" y="320" x="500">RAIN_PERC</text>
+<text style="text-anchor:start;" font-size="35px" y="335" x="375">PRECIP1</text>
+<text style="text-anchor:start;" font-size="35px" y="335" x="500">PRECIP_NUM1</text>
-<text style="text-anchor:start;" font-size="35px" y="380" x="375">SNOW_BOOL</text>
-<text style="text-anchor:start;" font-size="35px" y="380" x="500">SNOW_PERC</text>
+<text style="text-anchor:start;" font-size="35px" y="380" x="375">PRECIP2</text>
+<text style="text-anchor:start;" font-size="35px" y="380" x="500">PRECIP_NUM2</text>
<text style="text-anchor:middle;" font-size="32px" y="465.16922" x="102.92969">Tomorrow:</text>
<text style="text-anchor:middle;" font-size="20px" y="635.68347" x="74.958336">High:</text>
View
69 server/weather-script.py
@@ -9,25 +9,48 @@
import datetime
import codecs
import json
+import sys
+import datetime
+
+api_key = sys.argv[1]
+zip_code = sys.argv[2]
+
-api_key = ""
-zip_code = 11233
-wunderground_current_url = "http://api.wunderground.com/api/%s/conditions/q/%s.json" % (api_key, zip_code)
+try:
+ wunderground_current_url = "http://api.wunderground.com/api/%s/conditions/q/%s.json" % (api_key, zip_code)
+ wunderground_current_data = json.loads(urllib2.urlopen(wunderground_current_url).read())
+ lat = wunderground_current_data['current_observation']['observation_location']['latitude']
+ lon = wunderground_current_data['current_observation']['observation_location']['longitude']
+ observed_temp = '%.1f' % wunderground_current_data['current_observation']['temp_f']
-wunderground_current_data = json.loads(urllib2.urlopen(wunderground_current_url).read())
-lat = wunderground_current_data['current_observation']['observation_location']['latitude']
-lon = wunderground_current_data['current_observation']['observation_location']['longitude']
-observed_temp = '%.1f' % wunderground_current_data['current_observation']['temp_f']
+ wunderground_forecast_url = "http://api.wunderground.com/api/%s/forecast/q/%s.json" % (api_key, zip_code)
+ wunderground_forecast_data = json.loads(urllib2.urlopen(wunderground_forecast_url).read())
-wunderground_forecast_url = "http://api.wunderground.com/api/%s/forecast/q/%s.json" % (api_key, zip_code)
-wunderground_forecast_data = json.loads(urllib2.urlopen(wunderground_forecast_url).read())
+ pop = wunderground_forecast_data['forecast']['simpleforecast']['forecastday'][0]['pop']
+ qpf = wunderground_forecast_data['forecast']['simpleforecast']['forecastday'][0]['qpf_allday']['in']
+ snow = wunderground_forecast_data['forecast']['simpleforecast']['forecastday'][0]['snow_allday']['in']
-pop = wunderground_forecast_data['forecast']['simpleforecast']['forecastday'][0]['pop']
+ wunderground_alerts_url = "http://api.wunderground.com/api/%s/alerts/q/%s.json" % (api_key, zip_code)
+ wunderground_alerts_data = json.loads(urllib2.urlopen(wunderground_alerts_url).read())
+ alerts = wunderground_alerts_data['alerts']
-NWS_url = "http://graphical.weather.gov/xml/SOAP_server/ndfdSOAPclientByDay.php?whichClient=NDFDgenByDay&lat=%s&lon=%s&format=24+hourly&numDays=4&Unit=e" % (lat, lon)
+except Exception as e:
+ print "Error consulting Weather Underground: %s" % e
-weather_xml = urllib2.urlopen(NWS_url).read()
-dom = minidom.parseString(weather_xml)
+# observed_temp = 00
+# pop = 10
+# qpf = 0.023
+# snow = 0.23
+# alerts = []
+
+try:
+ #NWS_url = "http://graphical.weather.gov/xml/SOAP_server/ndfdSOAPclientByDay.php?whichClient=NDFDgenByDay&lat=%s&lon=%s&format=24+hourly&numDays=4&Unit=e" % (lat, lon)
+ NWS_url = "http://graphical.weather.gov/xml/SOAP_server/ndfdSOAPclientByDay.php?whichClient=NDFDgenByDay&lat=40.7142&lon=-74.0064&format=24+hourly&numDays=4&Unit=e"
+
+ weather_xml = urllib2.urlopen(NWS_url).read()
+ dom = minidom.parseString(weather_xml)
+except Exception as e:
+ print "Error consulting the NOAA: %s" % e
# Parse temperatures
xml_temperatures = dom.getElementsByTagName('temperature')
@@ -71,22 +94,36 @@
print icons
+
+precips = {'qpf': qpf, 'snow': snow}
+
#
# Preprocess SVG
#
# Open SVG to process
output = codecs.open('weather-script-preprocess.svg', 'r', encoding='utf-8').read()
+timestamp = datetime.datetime.now().strftime('%-m/%-d/%y %-I:%M%p')
+
# Insert icons and temperatures
output = output.replace('ICON_ONE',icons[0]).replace('ICON_TWO',icons[1]).replace('ICON_THREE',icons[2]).replace('ICON_FOUR',icons[3])
output = output.replace('HIGH_ONE',str(highs[0])).replace('HIGH_TWO',str(highs[1])).replace('HIGH_THREE',str(highs[2])).replace('HIGH_FOUR',str(highs[3]))
output = output.replace('LOW_ONE',str(lows[0])).replace('LOW_TWO',str(lows[1])).replace('LOW_THREE',str(lows[2])).replace('LOW_FOUR',str(lows[3]))
-output = output.replace('CURRENT', observed_temp)
-output = output.replace('RAIN_BOOL', '').replace('RAIN_PERC', '')
-output = output.replace('SNOW_BOOL', '').replace('SNOW_PERC', '')
+output = output.replace('CURRENT', str(observed_temp))
+
+if len(alerts) == 0: output = output.replace('ALERT', '')
+
+if pop != 0: output = output.replace('POP_BOOL', 'PoP: ').replace('POP_NUM', str(pop) + '%')
+else: output = output.replace('POP_BOOL', '').replace('POP_NUM', '')
+
+if qpf != 0: output = output.replace('PRECIP1', 'Rain: ').replace('PRECIP_NUM1', '%.2f"' % qpf)
+else: output = output.replace('PRECIP1', '').replace('PRECIP_NUM1', '')
+if snow != 0: output = output.replace('PRECIP2', 'Snow: ').replace('PRECIP_NUM2', '%.2f"' % snow)
+else: output = output.replace('PRECIP2', '').replace('PRECIP_NUM2', '')
+output = output.replace('TIME', timestamp)
# Insert days of week
one_day = datetime.timedelta(days=1)
View
9 server/weather-script.sh
@@ -1,8 +1,13 @@
#!/bin/sh
cd "$(dirname "$0")"
+set -e
-python2 weather-script.py
+WUND_CREDS=`grep WUNDERGROUND_KEY settings.conf | awk -F"=" '{print $2}'`
+IMAGE_PATH=`grep SERVER_IMAGE_PATH settings.conf | awk -F"=" '{print $2}'`
+ZIP=`grep ZIP_CODE settings.conf | awk -F"=" '{print $2}'`
+
+python2 weather-script.py $WUND_CREDS $ZIP
rsvg-convert --background-color=white -o weather-script-output.png weather-script-output.svg
pngcrush -c 0 -ow weather-script-output.png
-cp -f weather-script-output.png /path/to/web/server/directory/weather-script-output.png
+cp -f weather-script-output.png $IMAGE_PATH/weather-script-output.png

0 comments on commit e603587

Please sign in to comment.
Something went wrong with that request. Please try again.