| 
#!/usr/bin/python
import urllib2
import json
import time
import sys
HEADINGS = {
        0 : 'N',
        1 : 'NNE',
        2 : 'E',
        3 : 'SSE',
        4 : 'S',
        5 : 'SSW',
        6 : 'W',
        7 : 'NNW',
}
def getStationCurrentStatus(stationID):
    url = "http://stationdata.wunderground.com/cgi-bin/stationlookup?station=%s&format=json"
    resp = json.load(urllib2.urlopen(url % stationID))
    return resp.get('conds').get(stationID)
def degToDir(deg):
    deg = round((deg)/45) % 8
    return HEADINGS[deg]
def main():
    if len(sys.argv) <= 1:
        return
    recentData = getStationCurrentStatus(sys.argv[1])
    if recentData is None:
        return
    speed = float(recentData['windspeedmph'])
    gust = float(recentData['windgustmph'])
    print "Wind:    %0.1f / %0.1f MPH" % (speed, gust)
    if gust != 0:
        print "Quality: %0.1f" %  (speed/gust)
    print "Dir:     %s" % degToDir(int(recentData['winddir']))
    lastUpdated = time.time()-float(recentData['lu'])
    print "(%0.1f sec ago)" % (lastUpdated)
main()
 |