Ejemplo n.º 1
0
    def get_weather_data(self):
        station_id = g15gconf.get_string_or_default(self.gconf_client, "%s/station_id" % self.gconf_key, "KPEO")
        p = pywapi.get_weather_from_noaa(station_id)

        tm = email.utils.parsedate_tz(p["observation_time_rfc822"])[:9]
        data = {
            "location": p["location"],
            "datetime": datetime.datetime.fromtimestamp(time.mktime(tm)),
            "current_conditions": {
                "wind_speed": g15pythonlang.to_int_or_none(weather.mph_to_kph(float(p["wind_mph"])))
                if "wind_mph" in p
                else None,
                "wind_direction": g15pythonlang.to_int_or_none(p["wind_degrees"]) if "wind_degrees" in p else None,
                "pressure": p["pressure_mb"] if "pressure_mb" in p else None,
                "humidity": p["relative_humidity"] if "relative_humidity" in p else None,
                "condition": p["weather"] if "weather" in p else None,
                "temp_c": p["temp_c"] if "temp_c" in p else None,
                "icon": self._get_icon(p["icon_url_name"]) if "icon_url_name" in p else None,
                "fallback_icon": "http://w1.weather.gov/images/fcicons/%s"
                % ("%s.jpg" % os.path.splitext(p["icon_url_name"])[0])
                if "icon_url_name" in p
                else None,
            },
        }

        return data
Ejemplo n.º 2
0
    def _do_get_weather_data_xml(self):
        location_id = g15gconf.get_string_or_default(
            self.gconf_client, "%s/location_id" % self.gconf_key, "2487956")
        p = self._get_weather_from_yahoo(location_id)
        if p is None:
            return None

        # Get location
        location_el = p["location"]
        location = g15pythonlang.append_if_exists(location_el, "city", "")
        location = g15pythonlang.append_if_exists(location_el, "region",
                                                  location)
        location = g15pythonlang.append_if_exists(location_el, "country",
                                                  location)

        # Get current condition
        condition_el = p["condition"]
        wind_el = p["wind"] if "wind" in p else None

        # Observed date
        try:
            observed_datetime = datetime.datetime.strptime(
                condition_el["date"], "%a, %d %b %Y %H:%M %p %Z")
        except ValueError as v:
            logger.debug("Error parsing date, trying alternative method.",
                         exc_info=v)
            import email.utils
            dxt = email.utils.parsedate_tz(condition_el["date"])

            class TZ(datetime.tzinfo):
                def dst(self, dt):
                    return datetime.timedelta(0)

                def tzname(self, dt):
                    return dxt[9]

                def utcoffset(self, dt):
                    return datetime.timedelta(seconds=dxt[9])

            observed_datetime = datetime.datetime(*dxt[:7], tzinfo=TZ())

        # Forecasts (we only get 2 from yahoo)
        forecasts_el = p["forecasts"]
        forecasts = []
        today_low = None
        today_high = None
        for f in forecasts_el:
            condition_code = g15pythonlang.to_int_or_none(f["code"])
            high = g15pythonlang.to_float_or_none(f["high"])
            low = g15pythonlang.to_float_or_none(f["low"])
            if today_low is None:
                today_low = low
                today_high = high
            forecasts.append({
                "condition":
                f["text"],
                "high":
                high,
                "low":
                low,
                "day_of_week":
                f["day"],
                "icon":
                self._translate_icon(condition_code),
                "fallback_icon":
                "http://l.yimg.com/a/i/us/we/52/%s.gif" % condition_code
            })

        # Sunset and sunrise
        sunset = None
        sunrise = None
        if "astronomy" in p:
            astronomy = p["astronomy"]
            if "sunset" in astronomy:
                sunset = g15locale.parse_US_time_or_none(astronomy["sunset"])
            if "sunrise" in astronomy:
                sunrise = g15locale.parse_US_time_or_none(astronomy["sunrise"])

        # Pressure, Visibility and Humidity
        pressure = None
        if "atmosphere" in p:
            atmosphere = p["atmosphere"]
            if "pressure" in atmosphere:
                pressure = g15pythonlang.to_float_or_none(
                    atmosphere["pressure"])
            if "visibility" in atmosphere:
                visibility = g15pythonlang.to_float_or_none(
                    atmosphere["visibility"])
            if "humidity" in atmosphere:
                humidity = g15pythonlang.to_float_or_none(
                    atmosphere["humidity"])

        # Build data structure
        condition_code = g15pythonlang.to_int_or_none(condition_el["code"])
        data = {
            "location": location,
            "forecasts": forecasts,
            "datetime": observed_datetime,
            "current_conditions": {
                "wind_chill":
                wind_el["chill"]
                if wind_el is not None and "chill" in wind_el else None,
                "wind_direction":
                wind_el["direction"]
                if wind_el is not None and "direction" in wind_el else None,
                "wind_speed":
                wind_el["speed"]
                if wind_el is not None and "speed" in wind_el else None,
                "condition":
                condition_el["text"],
                "sunset":
                sunset,
                "sunrise":
                sunrise,
                "pressure":
                pressure,
                "visibility":
                visibility,
                "humidity":
                humidity,
                "low":
                today_low,
                "high":
                today_high,
                "temp_c":
                g15pythonlang.to_float_or_none(condition_el["temp"]),
                "icon":
                self._translate_icon(condition_code),
                "fallback_icon":
                "http://l.yimg.com/a/i/us/we/52/%s.gif" %
                condition_code if condition_code is not None else None
            }
        }

        return data
Ejemplo n.º 3
0
 def _do_get_weather_data_xml(self):
     location_id = g15gconf.get_string_or_default(self.gconf_client, "%s/location_id" % self.gconf_key, "2487956")
     p = self._get_weather_from_yahoo(location_id)
     if p is None:
         return None
     
     # Get location
     location_el = p["location"]
     location = g15pythonlang.append_if_exists(location_el, "city", "")
     location = g15pythonlang.append_if_exists(location_el, "region", location)
     location = g15pythonlang.append_if_exists(location_el, "country", location)
     
     # Get current condition
     condition_el = p["condition"]
     wind_el = p["wind"] if "wind" in p else None
     
     # Observed date
     try:
         observed_datetime = datetime.datetime.strptime(condition_el["date"], "%a, %d %b %Y %H:%M %p %Z")
     except ValueError as v: 
         logger.debug("Error parsing date, trying alternative method.", exc_info = v)
         import email.utils
         dxt = email.utils.parsedate_tz(condition_el["date"])
         class TZ(datetime.tzinfo):
             def dst(self, dt):
                 return datetime.timedelta(0)
             
             def tzname(self, dt):
                 return dxt[9]
             
             def utcoffset(self, dt): return datetime.timedelta(seconds=dxt[9])
         observed_datetime = datetime.datetime(*dxt[:7],  tzinfo=TZ())
     
     # Forecasts (we only get 2 from yahoo)
     forecasts_el = p["forecasts"]
     forecasts = []
     today_low = None
     today_high = None
     for f in forecasts_el:        
         condition_code = g15pythonlang.to_int_or_none(f["code"])
         high = g15pythonlang.to_float_or_none(f["high"])
         low = g15pythonlang.to_float_or_none(f["low"])
         if today_low is None:
             today_low = low
             today_high = high
         forecasts.append({
             "condition" : f["text"],                
             "high" : high,              
             "low" : low,            
             "day_of_week" : f["day"],
             "icon" : self._translate_icon(condition_code),
             "fallback_icon" : "http://l.yimg.com/a/i/us/we/52/%s.gif" % condition_code
                           })
         
     # Sunset and sunrise
     sunset = None
     sunrise = None
     if "astronomy" in p:
         astronomy = p["astronomy"]
         if "sunset" in astronomy:
             sunset = g15locale.parse_US_time_or_none(astronomy["sunset"])
         if "sunrise" in astronomy:
             sunrise = g15locale.parse_US_time_or_none(astronomy["sunrise"])
             
     # Pressure, Visibility and Humidity
     pressure = None
     if "atmosphere" in p:
         atmosphere = p["atmosphere"]
         if "pressure" in atmosphere:
             pressure = g15pythonlang.to_float_or_none(atmosphere["pressure"])
         if "visibility" in atmosphere:
             visibility = g15pythonlang.to_float_or_none(atmosphere["visibility"])
         if "humidity" in atmosphere:
             humidity = g15pythonlang.to_float_or_none(atmosphere["humidity"])
     
     # Build data structure        
     condition_code = g15pythonlang.to_int_or_none(condition_el["code"])
     data = {
         "location" : location,
         "forecasts" : forecasts,
         "datetime": observed_datetime,
         "current_conditions" : {
             "wind_chill": wind_el["chill"] if wind_el is not None and "chill" in wind_el else None,
             "wind_direction": wind_el["direction"] if wind_el is not None and "direction" in wind_el else None,
             "wind_speed": wind_el["speed"] if wind_el is not None and "speed" in wind_el else None,
             "condition" : condition_el["text"],
             "sunset" : sunset,
             "sunrise" : sunrise,
             "pressure" : pressure,
             "visibility" : visibility,
             "humidity" : humidity,
             "low" : today_low,
             "high" : today_high,
             "temp_c" : g15pythonlang.to_float_or_none(condition_el["temp"]),
             "icon" : self._translate_icon(condition_code),
             "fallback_icon" : "http://l.yimg.com/a/i/us/we/52/%s.gif" % condition_code if condition_code is not None else None
         }
     }
             
     return data
Ejemplo n.º 4
0
def to_int_or_none(s):
    return g15pythonlang.to_int_or_none(s)