#!/usr/bin/env python
# INITIAL FIGURING OUT OF XML PARSING FROM Thomas Upton's (http://www.thomasupton.com/) weather.py SCRIPT
# HAVE LEARNED A LOT FROM HIS PYTHON SCRIPT
import sys
from optparse import OptionParser
from xml.dom.minidom import parse
# SET UP WEATHER NAME SERVICE FOR THE XML
weatherNS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
# SET UP THE COMMAND LINE OPTION HOLDER
cmdparse = OptionParser()
# ADD COMMAND LINE OPTIONS
cmdparse.add_option('-t', '--temp', action="store_true", default=False)
cmdparse.add_option('-c', '--cond', action="store_true", default=False)
cmdparse.add_option('-o', '--code', action="store_true", default=False)
cmdparse.add_option('-b', '--both', action="store_true", default=False)
# PARSE THE COMMAND LINE OPITONS
opts, args = cmdparse.parse_args(sys.argv[1:])
# PARSE THE XML FILE
dom = parse("/tmp/yahooweather.xml")
# GRAB THE UNITS OF MEASURE AND CURRENT CONDITION
currUNITS = dom.getElementsByTagNameNS(weatherNS, 'units')[0]
currCOND = dom.getElementsByTagNameNS(weatherNS, 'condition')[0]
# PARSE OUT currUNITS AND currCond
unit = currUNITS.getAttribute('temperature')
currWX = currCOND.getAttribute('text')
currTEMP = currCOND.getAttribute('temp')
currCODE = currCOND.getAttribute('code')
# PRINT OUT DATA BASED UPON CLI ARGUMENTS
if (opts.temp):
print currTEMP + unit
if (opts.cond):
print currWX
if (opts.code):
print currCODE
if (opts.both):
print currWX + ", " + currTEMP + unit