Advertisement
Guest User

Beginning Python #11 - Full Circle Magazine

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