#!/usr/bin/env python2
import sys
from optparse import OptionParser
from urllib import urlopen
from xml.etree import ElementTree


def options(args=None):
    parser = OptionParser()
    add = parser.add_option
    add('-i', '--id', dest='id', type='string', default='KAKANCHO52',
        help='Station ID.', metavar='ID')
    opts, args = parser.parse_args(sys.argv if args is None else args)
    return opts


def etext(ele, tag, default=''):
    try:
	return ele.find(tag).text
    except:
	return default


def main():
    opts = options()
    url = "http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID={id}"
    url = url.format(id=opts.id)
    data = urlopen(url).read() #BeautifulSoup(urlopen(url))
    e = ElementTree.fromstring(data)
    print 'Location:', e.find('location').find('full').text
    print 'Time:', etext(e, 'observation_time')
    print 'Temp:', etext(e, 'temperature_string')
    print 'Humidity:', etext(e, 'relative_humidity')
    print 'Wind:', etext(e, 'wind_string'), etext(e, 'wind_dir') # also wind_mph and wind_gust_phn



if __name__ == '__main__':
    main()

