def weather(zipcode): """ @param zipcode: eg, 02139 @return: (code, sunrise, sunset) @rtype: (int, str, str) """ url = 'http://weather.yahooapis.com/forecastrss?p=%s&u=f' % zipcode html = urllib.urlopen(url).read() #<yweather:astronomy sunrise="6:57 am" sunset="6:00 pm"/> yweather_astronomy_re = re.compile("<yweather:astronomy[^>]*/>") sunrise_re = re.compile('sunrise="([\s\w\d:]*)"') sunset_re = re.compile('sunset="([\s\w\d:]*)"') #<yweather:condition text="Fair" code="33" temp="37" date="Thu, 19 Feb 2009 9:51 pm EST" /> yweather_condition_re = re.compile("<yweather:condition[^>]*/>") code_re = re.compile('code="(\d\d?)"') zipcode_err = re.compile("City not found") zerr = zipcode_err.search(html) if zerr and zerr.group(): print "zipcode err" return (None, None, None) yahoo_err = re.compile("Yahoo! Weather - Error") yerr = yahoo_err.search(html) if yerr and yerr.group(): print "yahoo err" return (None, None, None) sunrise = None sunset = None code = None #print #print html #print astro = yweather_astronomy_re.search(html) if astro and astro.group(): sunrise_g = sunrise_re.search(astro.group()) sunset_g = sunset_re.search(astro.group()) if sunrise_g and sunrise_g.groups(): sunrise = sunrise_g.groups()[0] if sunset_g and sunset_g.groups(): sunset = sunset_g.groups()[0] condi = yweather_condition_re.search(html) if condi and condi.group(): code_g = code_re.search(condi.group()) if code_g and code_g.groups(): code = code_g.groups()[0] print "yahoo code: ", code return Code.get_or_none(code=code), sunrise, sunset
def weather(zipcode): """ @param zipcode: eg, 02139 @return: {'code': code, ...plus others if code not None } @rtype: (int, str, str) """ units = 'f' if not zipcode_re.match(zipcode): units = 'c' url = 'http://weather.yahooapis.com/forecastrss?p=%s&u=%s' % (zipcode, units) html = urllib.urlopen(url).read() #print #print html #print zerr = zipcode_err.search(html) if zerr and zerr.group(): print "zipcode err" raise ZipcodeLookupFailedException() yerr = yahoo_err.search(html) if yerr and yerr.group(): print "yahoo err" raise WeatherLookupFailedException() sunrise = None sunset = None code = None text = None temp = None tomorrow_text = None tomorrow_high = None tomorrow_low = None next_day_text = None next_day_high = None next_day_low = None def grep(doc, regex): """ refactor common regex search pattern """ regex_g = regex.search(doc) if regex_g and regex_g.groups(): return regex_g.groups()[0] elif regex_g and regex_g.group(): return regex_g.group() else: return None astro = grep(html, yweather_astronomy_re) if astro: sunrise = grep(astro, sunrise_re) sunset = grep(astro, sunset_re) condi = grep(html, yweather_condition_re) if condi: code = grep(condi, code_re) temp = grep(condi, temp_re) text = grep(condi, text_re) if not code: raise ZipcodeLookupFailedException() forec = yweather_forecast_re.findall(html) if forec: if len(forec) > 0: tomorrow = forec[0] tomorrow_text = grep(tomorrow, text_re) tomorrow_high = grep(tomorrow, high_re) tomorrow_low = grep(tomorrow, low_re) if len(forec) > 1: next_day = forec[1] next_day_text = grep(next_day, text_re) next_day_high = grep(next_day, high_re) next_day_low = grep(next_day, low_re) print " yahoo code: ", code return {'code':Code.get_or_none(code=code), 'sunrise':sunrise, 'sunset':sunset, 'temp':temp, 'text':text, 'tomorrow_text':tomorrow_text, 'tomorrow_high':tomorrow_high, 'tomorrow_low':tomorrow_low, 'next_day_text':next_day_text, 'next_day_high':next_day_high, 'next_day_low':next_day_low, }
def weather(zipcode): """ @param zipcode: eg, 02139 @return: (code, sunrise, sunset) @rtype: (int, str, str) """ url = 'http://weather.yahooapis.com/forecastrss?p=%s&u=f' % zipcode html = urllib.urlopen(url).read() #<yweather:astronomy sunrise="6:57 am" sunset="6:00 pm"/> yweather_astronomy_re = re.compile("<yweather:astronomy[^>]*/>") sunrise_re = re.compile('sunrise="([\s\w\d:]*)"') sunset_re = re.compile('sunset="([\s\w\d:]*)"') #<yweather:condition text="Fair" code="33" temp="37" date="Thu, 19 Feb 2009 9:51 pm EST" /> yweather_condition_re = re.compile("<yweather:condition[^>]*/>") code_re = re.compile('code="(\d\d?)"') text_re = re.compile('text="([^"]+)"') temp_re = re.compile('temp="(\d+)"') #<yweather:forecast day="Tue" date="29 Nov 2005" low="45" high="62" text="Mostly Cloudy" code="27" /> #<yweather:forecast day="Wed" date="30 Nov 2005" low="52" high="60" text="Mostly Cloudy" code="28" /> yweather_forecast_re = re.compile("<yweather:forecast[^>]*/>") low_re = re.compile('low="(\d+)"') high_re = re.compile('high="(\d+)"') zipcode_err = re.compile("City not found") zerr = zipcode_err.search(html) if zerr and zerr.group(): print "zipcode err" return (None, None, None) yahoo_err = re.compile("Yahoo! Weather - Error") yerr = yahoo_err.search(html) if yerr and yerr.group(): print "yahoo err" return (None, None, None) sunrise = None sunset = None code = None text = None temp = None tomorrow_text = None tomorrow_high = None tomorrow_low = None next_day_text = None next_day_high = None next_day_low = None #print #print html #print def grep(doc, regex): """ refactor common regex search pattern """ regex_g = regex.search(doc) if regex_g and regex_g.groups(): return regex_g.groups()[0] elif regex_g and regex_g.group(): return regex_g.group() else: return None astro = grep(html, yweather_astronomy_re) if astro: sunrise = grep(astro, sunrise_re) sunset = grep(astro, sunset_re) condi = grep(html, yweather_condition_re) if condi: code = grep(condi, code_re) temp = grep(condi, temp_re) text = grep(condi, text_re) forec = yweather_forecast_re.findall(html) if forec: if len(forec) > 0: tomorrow = forec[0] tomorrow_text = grep(tomorrow, text_re) tomorrow_high = grep(tomorrow, high_re) tomorrow_low = grep(tomorrow, low_re) if len(forec) > 1: next_day = forec[1] next_day_text = grep(next_day, text_re) next_day_high = grep(next_day, high_re) next_day_low = grep(next_day, low_re) print "yahoo code: ", code return {'code':Code.get_or_none(code=code), 'sunrise':sunrise, 'sunset':sunset, 'temp':temp, 'text':text, 'tomorrow_text':tomorrow_text, 'tomorrow_high':tomorrow_high, 'tomorrow_low':tomorrow_low, 'next_day_text':next_day_text, 'next_day_high':next_day_high, 'next_day_low':next_day_low, }
} if __name__ == "__main__": if settings.DJANGO_SERVER: weatherize(weatherized=Weatherized.get_or_none(user__twitter_username='******')) else: print "hello" if True: for w in Weatherized.objects.all(): code = None success = False msg = '' w.weatherized_background = None try: code, success = weatherize(weatherized=w) except ZipcodeLookupFailedException: msg = 'Could not find weather condition for this zipcode. (Expanded weather lookup coming soon)' except WeatherLookupFailedException: msg = 'Weather condition lookup failed inexplicably. (Expanded weather lookup coming soon)' except UpdateBackgroundFailedException: msg = 'Twitter background update inexplicably failed. It may work if you try again.' except: msg = 'Random failure. It may work if you try again.' w.error_message = msg w.background_success = success w.current_code = code or Code.get(name="not available") w.save() print "SUCCESS"