GeekTool desktop weather with forecasts
December 9, 2009 at 12:10 PM by Dr. Drang
I’ve updated my GeekTool weather script to include forecast information in addition to current conditions.
As I mentioned in this post, I’ve moved this information from the bottom left corner to the upper middle of the left edge of the screen (the text above the weather stuff is an extract from my SuperDuper! log file). That gives me room to include more data, and the forecasts seemed like the natural thing to add. The new version of the weathertext
script (which you can also download from its GitHub repository is this:
1: #!/usr/bin/python
2: # -*- coding: utf-8 -*-
3:
4: import pywapi
5:
6: # Get the current conditions for the given station.
7: noaa = pywapi.get_weather_from_noaa('KARR')
8: yahoo = pywapi.get_weather_from_yahoo('60502', '')
9:
10: # The Yahoo pressure dictionary.
11: ypressure = {'0': 'steady', '1': 'rising', '2': 'falling'}
12:
13: # This is the list of output lines.
14: out = []
15:
16: # Go through the dictionaries and construct a list of the desired output lines.
17: # out.append('Last update:' + noaa['observation_time'].split(',')[1])
18: out.append('Sunlight: %s to %s' % (yahoo['astronomy']['sunrise'], yahoo['astronomy']['sunset']))
19: out.append(yahoo['condition']['text'])
20: try:
21: gust = ', gusting to %s mph' % noaa['wind_gust_mph']
22: except KeyError:
23: gust = ''
24: out.append('Wind: %s at %s mph%s' % ( noaa['wind_dir'], noaa['wind_mph'], gust))
25: out.append('Relative Humidity: %s%%' % noaa['relative_humidity'])
26: out.append('Pressure: %2.2f and %s' % (float(yahoo['atmosphere']['pressure']), ypressure[yahoo['atmosphere']['rising']]))
27: try:
28: out.append(u'Wind Chill: %s°' % noaa['windchill_f'])
29: except KeyError:
30: pass
31: try:
32: out.append(u'Heat Index: %s°' % noaa['heat_index_f'])
33: except KeyError:
34: pass
35: out.append(u'Temperature: %.0f°' % float(noaa['temp_f']))
36: today = yahoo['forecasts'][0]
37: tomorrow = yahoo['forecasts'][1]
38: out.append(u'''Today:
39: High: %3d°
40: Low: %3d°
41: %s''' % (int(today['high']), int(today['low']), today['text']))
42: out.append(u'''Tomorrow:
43: High: %3d°
44: Low: %3d°
45: %s''' % (int(tomorrow['high']), int(tomorrow['low']), tomorrow['text']))
46:
47: print '\n'.join(out).encode('utf8')
Note that in addition to the forecast lines (36-45) I’ve added the necessary code in Lines 2 and 47 to handle UTF-8 encoding for the degree symbol (°). Because GeekTool 2 doesn’t like UTF-8, the GeekTool command for getting the display right is
~/bin/weathertext | iconv -s -f UTF-8 -t ISO_8859-1
I discussed the use of iconv
to convert from UTF-8 to Latin-1 (which GeekTool 2 can handle) in this post.
The command in the context of the GeekTool preference pane looks like this: