diff --git a/tgetwx.py b/tgetwx.py index ba44c15..c0ceb57 100755 --- a/tgetwx.py +++ b/tgetwx.py @@ -3,11 +3,11 @@ # Copyright (c) 2014 TundraWare Inc., Des Plaines, IL 60018 USA # All Rights Reserved. For Terms Of Use See: tgetwx-license.txt # For Program Updates See: http://www.tundraware.com/Software/tgetwx -# $Id: tgetwx.py,v 1.101 2014/02/19 20:30:40 tundra Exp $ +# $Id: tgetwx.py,v 1.102 2014/02/20 00:19:10 tundra Exp $ # Embed the source control ID string for use by program -CVSID='$Id: tgetwx.py,v 1.101 2014/02/19 20:30:40 tundra Exp $' +CVSID='$Id: tgetwx.py,v 1.102 2014/02/20 00:19:10 tundra Exp $' ##### # Program Information @@ -68,6 +68,7 @@ # Constants ##### +KELVIN = 273.15 ##### # Literals @@ -82,30 +83,13 @@ ##### -# Debug Messages -##### - -##### -# Debug Messages -##### - -DEBUGFLAG = "-d" -dDEBUG = "DEBUG" -dPROGENV = "$" + PROGENV - -##### # Error Messages ##### -eBADARG = "Invalid command line: %s!" -eERROR = "ERROR" - - -##### -# Informational Messages -##### - -iINFO = "INFO" +eBADARG = "Invalid command line: %s!" +eBADLOCATION = "Location, %s, Not Found!" +eERROR = "ERROR" +eLOOKUPFAIL = "Weather Lookup For, %s, Failed!" ##### @@ -121,7 +105,7 @@ uTable = [PROGVER, "usage: " + PROGNAME + " [%s]" % optionslist, " where,", - " -H Generate HTML (default: text)", + " -H Generate HTML (default: Plain Text)", " -h Print this help information", " -v Print detailed version information", ] @@ -208,11 +192,47 @@ def ReportWeather(location, EmitHTML): - f = urllib2.urlopen(WEATHERURL % location) - json_string = f.read() - parsed_json = json.loads(json_string) - print parsed_json.keys() - f.close() + results = [] + + try: + f = urllib2.urlopen(WEATHERURL % location) + lookup = f.read() + + except: + results.append(eLOOKUPFAIL % s) + + finally: + f.close() + + data = json.loads(lookup) + # Handle cities not found + if "message" in data: + results.append(data["message"]) + + # Lookup was successful, populate data + else: + + # Order is Country, Date, Temp, Humidity, Pressure, Wind Speed, Wind Direction, Cloud Cover, Description + + results.append(data["sys"]["country"]) + results.append(data["dt"]) + results.append(str((data["main"]["temp"] - KELVIN) * (9/5) + 32) + "F") + results.append(str(data["main"]["humidity"])+ "%") + results.append(str(data["main"]["pressure"] * 0.0295299830714) + "in") + results.append(str(data["wind"]["speed"]) + "mph") + results.append(str(data["wind"]["deg"]) + "deg") + results.append(str(data["clouds"]["all"]) + "%") + results.append(data["weather"][0]["description"]) + + + print results + + + + + + + # End of 'ReportWeather()'