Advertisement
Guest User

Beginning Python #12 - Full Circle Magazine

a guest
Jun 5th, 2010
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.85 KB | None | 0 0
  1. from xml.etree import ElementTree as ET
  2. import urllib
  3. import sys
  4. import getopt
  5.  
  6. class ForecastInfo:
  7.     def __init__(self):
  8.         self.forecastText = []  # Today/tonight forecast information
  9.         self.Title = []         # Today/tonight
  10.         self.date = ''        
  11.         self.icon = []          # Icon to use for conditions today/tonight
  12.         self.periods = 0
  13.         self.period = 0
  14.         #===========================================================
  15.         # Extended forecast information
  16.         #===========================================================      
  17.         self.extIcon = []       # Icon to use for extended forecast
  18.         self.extDay = []        # Day text for this forecast ("Monday", "Tuesday" etc)
  19.         self.extHigh = []       # High Temp. (F)
  20.         self.extHighC = []      # High Temp. (C)
  21.         self.extLow = []        # Low Temp. (F)
  22.         self.extLowC = []       # Low Temp. (C)
  23.         self.extConditions = [] # Conditions text
  24.         self.extPeriod = []     # Numerical Period information (counter)
  25.         self.extpop = []           # Percent chance Of Precipitation
  26.  
  27.     def GetForecastData(self,location):
  28.         try:
  29.             forecastdata = 'http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=%s' % location
  30.             urllib.socket.setdefaulttimeout(8)
  31.             usock = urllib.urlopen(forecastdata)
  32.             tree = ET.parse(usock)
  33.             usock.close()
  34.         except:
  35.             print 'ERROR - Forecast - Could not get information from server...'
  36.             sys.exit(2)
  37.         #===========================================================  
  38.         # Get the forecast for today and (if available) tonight
  39.         #===========================================================
  40.         fcst = tree.find('.//txt_forecast')
  41.         for f in fcst:
  42.             if f.tag == 'number':
  43.                 self.periods = f.text
  44.             elif f.tag == 'date':
  45.                 self.date = f.text
  46.             for subelement in f:
  47.                 if subelement.tag == 'period':
  48.                     self.period=int(subelement.text)
  49.                 if subelement.tag == 'fcttext':
  50.                     self.forecastText.append(subelement.text)
  51.                 elif subelement.tag == 'icon':
  52.                     self.icon.append( subelement.text)
  53.                 elif subelement.tag == 'title':
  54.                     self.Title.append(subelement.text)
  55.         #===========================================================
  56.         # Now get the extended forecast
  57.         #===========================================================
  58.         fcst = tree.find('.//simpleforecast')
  59.         for f in fcst:
  60.              for subelement in f:
  61.                 if subelement.tag == 'period':
  62.                     self.extPeriod.append(subelement.text)
  63.                 elif subelement.tag == 'conditions':
  64.                     self.extConditions.append(subelement.text)
  65.                 elif subelement.tag == 'icon':
  66.                     self.extIcon.append(subelement.text)
  67.                 elif subelement.tag == 'pop':
  68.                     self.extpop.append(subelement.text)
  69.                 elif subelement.tag == 'date':
  70.                     for child in subelement.getchildren():
  71.                         if child.tag == 'weekday':
  72.                             self.extDay.append(child.text)
  73.                 elif subelement.tag == 'high':
  74.                     for child in subelement.getchildren():
  75.                         if child.tag == 'fahrenheit':
  76.                             self.extHigh.append(child.text)
  77.                         if child.tag == 'celsius':
  78.                             self.extHighC.append(child.text)
  79.                 elif subelement.tag == 'low':
  80.                     for child in subelement.getchildren():
  81.                         if child.tag == 'fahrenheit':
  82.                             self.extLow.append(child.text)
  83.                         if child.tag == 'celsius':
  84.                             self.extLowC.append(child.text)
  85.                        
  86.     def output(self,US,IncludeToday,Output):
  87.         # US takes 0,1 or 2
  88.         #   0 = Centegrade
  89.         #   1 = Farenheit
  90.         #   2 = both (if available)
  91.         # Now print it all
  92.         if Output == 0:
  93.             for c in range(int(self.period)):
  94.                 if c <> 1:
  95.                     print '----------------------------------------'
  96.                 print 'Forecast for %s' % self.Title[c].lower()
  97.                 print 'Forecast = %s' % self.forecastText[c]
  98.                 print 'ICON=%s' % self.icon[c]
  99.                 print '----------------------------------------'
  100.             print 'Extended Forecast...'
  101.             if IncludeToday == 1:
  102.                 startRange = 0
  103.             else:
  104.                 startRange = 1
  105.             for c in range(startRange,6):
  106.                 print self.extDay[c]
  107.                 if US == 0: #Centegrade information
  108.                     print '\tHigh - %s(C)' % self.extHigh[c]
  109.                     print '\tLow - %s(C)' % self.extLow[c]
  110.                 elif US == 1: #Farenheit information
  111.                     print '\tHigh - %s(F)' % self.extHigh[c]
  112.                     print '\tLow - %s(F)' % self.extLow[c]
  113.                 else: #US == 2 both(if available)
  114.                     print '\tHigh - %s' % self.extHigh[c]
  115.                     print '\tLow - %s' % self.extLow[c]
  116.                 if int(self.extpop[c]) == 0:
  117.                     print '\tConditions - %s.' % self.extConditions[c]
  118.                 else:
  119.                     print '\tConditions - %s.  %d%% chance of precipitation.' % (self.extConditions[c],int(self.extpop[c]))
  120.          
  121.     def DoIt(self,Location,US,IncludeToday,Output):
  122.         self.GetForecastData(Location)
  123.         self.output(US,IncludeToday,Output)
  124.  
  125. forecast = ForecastInfo()
  126. forecast.DoIt('80013',1,0,0) # Insert your own postal code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement