Advertisement
Guest User

Full Circle magazine #37 - Beginning Python Pt.11

a guest
Jul 17th, 2010
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. """ w_currents.py
  2.    Returns current conditions, forecast and alerts for a given zipcode from WeatherUnderground.com.
  3.  
  4.    Usage: python wonderground.py [options]
  5.    Options:
  6.             -h, --help         Show this help
  7.             -l, --location     City,State to use
  8.             -z, --zip          Zipcode to use as location
  9.  
  10.    Examples:
  11.    w_currents.py -h (shows this help information)
  12.    w_currents.py -z 80013 (uses the zip code 80013 as location)
  13. """
  14.  
  15. from xml.etree import ElementTree as ET
  16. import urllib
  17. import sys
  18. import getopt
  19.  
  20.  
  21. class CurrentInfo:
  22.     """
  23.        This routine retrieves the current condition xml data from WeatherUnderground.com
  24.        based off of the zip code or Airport Code...
  25.        currently tested only with Zip Code and Airport code
  26.        For location,
  27.        if zip code use something like 80013 (no quotes)
  28.        if airport use something like "KDEN" (use double quotes)
  29.        if city/state (US) use something like "Aurora,%20CO" or "Aurora,CO" (use double quotes)
  30.        if city/country, use something like "London,%20England" (use double quotes)
  31.    """
  32.     def getCurrents(self,debuglevel,Location):
  33.         if debuglevel > 0:
  34.             print "Location = %s" % Location
  35.         try:
  36.             CurrentConditions = 'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=%s' % Location
  37.             urllib.socket.setdefaulttimeout(8)
  38.             usock = urllib.urlopen(CurrentConditions)
  39.             tree = ET.parse(usock)
  40.             usock.close()
  41.         except:
  42.             print 'ERROR - Current Conditions - Could not get information from server...'
  43.             if debuglevel > 0:
  44.                 print Location
  45.             sys.exit(2)
  46.         # Get Display Location
  47.         for loc in tree.findall("//full"):
  48.             self.location = loc.text
  49.         # Get Observation time
  50.         for tim in tree.findall("//observation_time"):
  51.             self.obtime = tim.text
  52.         # Get Current conditions
  53.         for weather in tree.findall("//weather"):
  54.             self.we = weather.text
  55.         # Get Temp
  56.         for TempF in tree.findall("//temperature_string"):
  57.             self.tmpB = TempF.text
  58.         #Get Humidity
  59.         for hum in tree.findall("//relative_humidity"):
  60.             self.relhum = hum.text
  61.         # Get Wind info
  62.         for windstring in tree.findall("//wind_string"):
  63.             self.winds = windstring.text
  64.         # Get Barometric Pressure
  65.         for pressure in tree.findall("//pressure_string"):
  66.             self.baroB = pressure.text
  67.  
  68.     def output(self):
  69.         print 'Weather Information From Wunderground.com'
  70.         print 'Weather info for %s ' % self.location
  71.         print self.obtime
  72.         print 'Current Weather - %s' % self.we
  73.         print 'Current Temp - %s' % self.tmpB
  74.         print 'Barometric Pressure - %s' % self.baroB
  75.         print 'Relative Humidity - %s' % self.relhum
  76.         print 'Winds %s' % self.winds
  77.            
  78.     def DoIt(self,Location):
  79.         self.getCurrents(1,Location)
  80.         self.output()            
  81.  
  82. def usage():
  83.     print __doc__
  84.    
  85. def main(argv):  
  86.     location = 80013
  87.    
  88.     try:
  89.         opts, args = getopt.getopt(argv, "hz:l:", ["help=", "zip=", "location="])
  90.     except getopt.GetoptError:
  91.         usage()
  92.         sys.exit(2)
  93.     for opt, arg in opts:
  94.         if opt in ("-h", "--help"):
  95.             usage()
  96.             sys.exit()
  97.         elif opt in ("-l", "--location"):
  98.             location = arg
  99.         elif opt in ("-z", "--zip"):
  100.             location = arg
  101.             print "Location = %s" % location
  102.     currents = CurrentInfo()      
  103.     currents.DoIt(location)
  104.  
  105. #===========================================================
  106. #       Main loop
  107. #===========================================================      
  108. if __name__ == "__main__":  
  109.  
  110.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement