예제 #1
0
    def _build_properties(self):
        properties = {}
        attributes = {}
        use_twenty_four_hour = g15gconf.get_bool_or_default(
            self.gconf_client, "%s/twenty_four_hour_times" % self.gconf_key,
            True)
        if self._weather is None:
            properties["message"] = _("No weather source configuration")
        else:
            current = self._weather['current_conditions']
            if len(current) == 0:
                properties["message"] = _("No weather data for location:-\n%s"
                                          ) % self._weather['location']
            else:
                properties["location"] = self._weather['location']
                dt = self._weather['datetime']
                if use_twenty_four_hour:
                    properties["time"] = g15locale.format_time_24hour(
                        dt, self.gconf_client, False)
                else:
                    properties["time"] = g15locale.format_time(
                        dt, self.gconf_client, False)
                properties["date"] = g15locale.format_date(
                    dt, self.gconf_client)
                properties["datetime"] = g15locale.format_date_time(
                    dt, self.gconf_client, False)
                properties["message"] = ""
                c_icon, f_icon, t_icon = self._get_icons(current)
                if t_icon != None:
                    attributes["icon"] = g15cairo.load_surface_from_file(
                        t_icon)
                    properties["icon"] = g15icontools.get_embedded_image_url(
                        attributes["icon"])
                else:
                    logger.warning("No translated weather icon for %s", c_icon)
                mono_thumb = self._get_mono_thumb_icon(c_icon)
                if mono_thumb != None:
                    attributes[
                        "mono_thumb_icon"] = g15cairo.load_surface_from_file(
                            os.path.join(
                                os.path.join(os.path.dirname(__file__),
                                             "default"), mono_thumb))
                properties["condition"] = current['condition']

                temp_c = g15pythonlang.to_float_or_none(current['temp_c'])
                if temp_c is not None:
                    temp_f = c_to_f(temp_c)
                    temp_k = c_to_k(temp_c)
                low_c = g15pythonlang.to_float_or_none(
                    current['low']) if 'low' in current else None
                if low_c is not None:
                    low_f = c_to_f(low_c)
                    low_k = c_to_k(low_c)
                high_c = g15pythonlang.to_float_or_none(
                    current['high']) if 'high' in current else None
                if high_c is not None:
                    high_f = c_to_f(high_c)
                    high_k = c_to_k(high_c)

                properties[
                    "temp_c"] = "%3.1f°C" % temp_c if temp_c is not None else ""
                properties[
                    "hi_c"] = "%3.1f°C" % high_c if high_c is not None else ""
                properties[
                    "lo_c"] = "%3.1f°C" % low_c if low_c is not None else ""
                properties[
                    "temp_f"] = "%3.1f°F" % temp_f if temp_c is not None else ""
                properties[
                    "lo_f"] = "%3.1f°F" % low_f if low_c is not None else ""
                properties[
                    "high_f"] = "%3.1f°F" % high_f if high_c is not None else ""
                properties[
                    "temp_k"] = "%3.1f°K" % temp_k if temp_c is not None else ""
                properties[
                    "lo_k"] = "%3.1f°K" % low_k if low_c is not None else ""
                properties[
                    "high_k"] = "%3.1f°K" % high_k if high_c is not None else ""

                units = self.gconf_client.get_int(self.gconf_key + "/units")
                if units == CELSIUS:
                    unit = "C"
                    properties["temp"] = properties["temp_c"]
                    properties[
                        "temp_short"] = "%2.0f°" % temp_c if temp_c else ""
                    properties["hi"] = properties["hi_c"]
                    properties[
                        "hi_short"] = "%2.0f°" % high_c if high_c else ""
                    properties["lo"] = properties["lo_c"]
                    properties["lo_short"] = "%2.0f°" % low_c if low_c else ""
                elif units == FARANHEIT:
                    unit = "F"
                    properties["lo"] = properties["lo_f"]
                    properties[
                        "lo_short"] = "%2.0f°" % low_f if low_c is not None else ""
                    properties["hi"] = properties["high_f"]
                    properties[
                        "hi_short"] = "%2.0f°" % high_f if high_c is not None else ""
                    properties["temp"] = properties["temp_f"]
                    properties[
                        "temp_short"] = "%2.0f°" % temp_f if temp_c is not None else ""
                else:
                    unit = "K"
                    properties["lo"] = properties["lo_k"]
                    properties[
                        "lo_short"] = "%2.0f°" % low_k if low_c is not None else ""
                    properties["hi"] = properties["high_k"]
                    properties[
                        "hi_short"] = "%2.0f°" % high_k if high_c is not None else ""
                    properties["temp"] = properties["temp_k"]
                    properties[
                        "temp_short"] = "%2.0f°" % temp_k if temp_c is not None else ""

                # Wind
                wind = g15pythonlang.append_if_exists(current, "wind_chill",
                                                      "", "%sC")
                wind = g15pythonlang.append_if_exists(current, "wind_speed",
                                                      wind, "%sKph")
                wind = g15pythonlang.append_if_exists(current,
                                                      "wind_direction", wind,
                                                      "%sdeg")
                properties["wind"] = wind

                # Visibility
                visibility = g15pythonlang.append_if_exists(
                    current, "visibility", "", "%sM")
                properties["visibility"] = visibility

                # Pressure
                pressure = g15pythonlang.append_if_exists(
                    current, "pressure", "", "%smb")
                properties["pressure"] = pressure

                # Humidity
                humidity = g15pythonlang.append_if_exists(
                    current, "humidity", "", "%s%%")
                properties["humidity"] = humidity

                # Sunrise
                dt = current['sunrise'] if 'sunrise' in current else None
                if dt is None:
                    properties["sunrise_time"] = ""
                elif use_twenty_four_hour:
                    properties["sunrise_time"] = g15locale.format_time_24hour(
                        dt, self.gconf_client, False)
                else:
                    properties["sunrise_time"] = g15locale.format_time(
                        dt, self.gconf_client, False)

                # Sunset
                dt = current['sunset'] if 'sunset' in current else None
                if dt is None:
                    properties["sunset_time"] = ""
                elif use_twenty_four_hour:
                    properties["sunset_time"] = g15locale.format_time_24hour(
                        dt, self.gconf_client, False)
                else:
                    properties["sunset_time"] = g15locale.format_time(
                        dt, self.gconf_client, False)

                # Blank all the forecasts by default
                for y in range(1, 10):
                    properties["condition" + str(y)] = ""
                    properties["hi" + str(y)] = ""
                    properties["lo" + str(y)] = ""
                    properties["day" + str(y)] = ""
                    properties["day_letter" + str(y)] = ""
                    properties["icon" + str(y)] = ""

                # Forecasts
                y = 1
                if 'forecasts' in self._weather:
                    for forecast in self._weather['forecasts']:
                        properties["condition" +
                                   str(y)] = forecast['condition']

                        lo_c = g15pythonlang.to_float_or_none(forecast['low'])
                        if lo_c is not None:
                            lo_f = c_to_f(temp_c)
                            lo_k = c_to_k(temp_c)
                        hi_c = g15pythonlang.to_float_or_none(forecast['high'])
                        if hi_c is not None:
                            hi_f = c_to_f(hi_c)
                            hi_k = c_to_k(hi_c)

                        if units == CELSIUS:
                            properties["hi" + str(y)] = "%3.0f°C" % hi_c
                            properties["lo" + str(y)] = "%3.0f°C" % lo_c
                        elif units == FARANHEIT:
                            properties["hi" + str(y)] = "%3.0f°F" % hi_f
                            properties["lo" + str(y)] = "%3.0f°F" % lo_f
                        else:
                            properties["hi" + str(y)] = "%3.0f°K" % hi_k
                            properties["lo" + str(y)] = "%3.0f°K" % lo_k

                        properties["day" + str(y)] = forecast['day_of_week']
                        properties["day_letter" +
                                   str(y)] = forecast['day_of_week'][:1]

                        c_icon, f_icon, t_icon = self._get_icons(forecast)
                        properties[
                            "icon" +
                            str(y)] = g15icontools.get_embedded_image_url(
                                g15cairo.load_surface_from_file(t_icon))

                        y += 1

        return properties, attributes
예제 #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
예제 #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
예제 #4
0
파일: weather.py 프로젝트: FPar/gnome15
 def _build_properties(self):
     properties = {}
     attributes = {}
     use_twenty_four_hour = g15gconf.get_bool_or_default(self.gconf_client, "%s/twenty_four_hour_times" % self.gconf_key, True)
     if self._weather is None:            
         properties["message"] = _("No weather source configuration")
     else: 
         current = self._weather['current_conditions']
         if len(current) == 0:
             properties["message"] = _("No weather data for location:-\n%s") % self._weather['location']
         else:
             properties["location"] = self._weather['location']
             dt = self._weather['datetime']
             if use_twenty_four_hour:
                 properties["time"] = g15locale.format_time_24hour(dt, self.gconf_client, False)
             else:          
                 properties["time"] = g15locale.format_time(dt, self.gconf_client, False)
             properties["date"] = g15locale.format_date(dt, self.gconf_client)
             properties["datetime"] = g15locale.format_date_time(dt, self.gconf_client, False)
             properties["message"] = ""
             c_icon, f_icon, t_icon = self._get_icons(current)
             if t_icon != None:
                 attributes["icon"] = g15cairo.load_surface_from_file(t_icon)
                 properties["icon"] = g15icontools.get_embedded_image_url(attributes["icon"])
             else:
                 logger.warning("No translated weather icon for %s", c_icon)
             mono_thumb = self._get_mono_thumb_icon(c_icon)        
             if mono_thumb != None:
                 attributes["mono_thumb_icon"] = g15cairo.load_surface_from_file(os.path.join(os.path.join(os.path.dirname(__file__), "default"), mono_thumb))
             properties["condition"] = current['condition']
             
             temp_c = g15pythonlang.to_float_or_none(current['temp_c'])
             if temp_c is not None:
                 temp_f = c_to_f(temp_c)
                 temp_k = c_to_k(temp_c)
             low_c = g15pythonlang.to_float_or_none(current['low']) if 'low' in current else None
             if low_c is not None :
                 low_f = c_to_f(low_c)
                 low_k = c_to_k(low_c)
             high_c  = g15pythonlang.to_float_or_none(current['high']) if 'high' in current else None
             if high_c is not None :
                 high_f  = c_to_f(high_c)
                 high_k = c_to_k(high_c)
             
             properties["temp_c"] = "%3.1f°C" % temp_c if temp_c is not None else ""
             properties["hi_c"] = "%3.1f°C" % high_c if high_c is not None else ""
             properties["lo_c"] = "%3.1f°C" % low_c if low_c is not None else ""
             properties["temp_f"] = "%3.1f°F" % temp_f if temp_c is not None else ""
             properties["lo_f"] = "%3.1f°F" % low_f if low_c is not None else ""
             properties["high_f"] = "%3.1f°F" % high_f if high_c is not None else ""
             properties["temp_k"] = "%3.1f°K" % temp_k if temp_c is not None else ""
             properties["lo_k"] = "%3.1f°K" % low_k if low_c is not None else ""
             properties["high_k"] = "%3.1f°K" % high_k if high_c is not None else ""
             
             units = self.gconf_client.get_int(self.gconf_key + "/units")
             if units == CELSIUS:      
                 unit = "C"           
                 properties["temp"] = properties["temp_c"]
                 properties["temp_short"] = "%2.0f°" % temp_c if temp_c else ""
                 properties["hi"] = properties["hi_c"]
                 properties["hi_short"] = "%2.0f°" % high_c if high_c else ""                 
                 properties["lo"] = properties["lo_c"]
                 properties["lo_short"] = "%2.0f°" % low_c if low_c else ""
             elif units == FARANHEIT:      
                 unit = "F"                      
                 properties["lo"] = properties["lo_f"]              
                 properties["lo_short"] = "%2.0f°" % low_f if low_c is not None else ""
                 properties["hi"] = properties["high_f"]              
                 properties["hi_short"] = "%2.0f°" % high_f if high_c is not None else ""
                 properties["temp"] = properties["temp_f"]              
                 properties["temp_short"] = "%2.0f°" % temp_f if temp_c is not None else ""
             else:                         
                 unit = "K"          
                 properties["lo"] = properties["lo_k"]
                 properties["lo_short"] = "%2.0f°" % low_k if low_c is not None else ""      
                 properties["hi"] = properties["high_k"]
                 properties["hi_short"] = "%2.0f°" % high_k if high_c is not None else ""
                 properties["temp"] = properties["temp_k"]
                 properties["temp_short"] = "%2.0f°" % temp_k if temp_c is not None else ""
                 
             
             # Wind
             wind = g15pythonlang.append_if_exists(current, "wind_chill", "", "%sC")
             wind = g15pythonlang.append_if_exists(current, "wind_speed", wind, "%sKph")
             wind = g15pythonlang.append_if_exists(current, "wind_direction", wind, "%sdeg")
             properties["wind"] =  wind 
             
             # Visibility
             visibility = g15pythonlang.append_if_exists(current, "visibility", "", "%sM")
             properties["visibility"] =  visibility
             
             # Pressure
             pressure = g15pythonlang.append_if_exists(current, "pressure", "", "%smb")
             properties["pressure"] =  pressure
             
             # Humidity
             humidity = g15pythonlang.append_if_exists(current, "humidity", "", "%s%%")
             properties["humidity"] =  humidity
             
             # Sunrise                
             dt = current['sunrise'] if 'sunrise' in current else None
             if dt is None:
                 properties["sunrise_time"] = ""
             elif use_twenty_four_hour:
                 properties["sunrise_time"] = g15locale.format_time_24hour(dt, self.gconf_client, False)
             else:          
                 properties["sunrise_time"] = g15locale.format_time(dt, self.gconf_client, False)
                 
             # Sunset                
             dt = current['sunset'] if 'sunset' in current else None
             if dt is None:
                 properties["sunset_time"] = ""
             elif use_twenty_four_hour:
                 properties["sunset_time"] = g15locale.format_time_24hour(dt, self.gconf_client, False)
             else:          
                 properties["sunset_time"] = g15locale.format_time(dt, self.gconf_client, False)
                 
             # Blank all the forecasts by default
             for y in range(1, 10):
                 properties["condition" + str(y)] = ""
                 properties["hi" + str(y)] = ""
                 properties["lo" + str(y)] = ""
                 properties["day" + str(y)] = ""
                 properties["day_letter" + str(y)] = ""
                 properties["icon" + str(y)] = ""
                 
             # Forecasts
             y = 1
             if 'forecasts' in self._weather:
                 for forecast in self._weather['forecasts']:        
                     properties["condition" + str(y)] = forecast['condition']
                     
                     lo_c = g15pythonlang.to_float_or_none(forecast['low'])
                     if lo_c is not None:
                         lo_f = c_to_f(temp_c)
                         lo_k = c_to_k(temp_c)
                     hi_c = g15pythonlang.to_float_or_none(forecast['high'])
                     if hi_c is not None:
                         hi_f = c_to_f(hi_c)
                         hi_k = c_to_k(hi_c)
                     
                     if units == CELSIUS:                 
                         properties["hi" + str(y)] = "%3.0f°C" % hi_c
                         properties["lo" + str(y)] = "%3.0f°C" % lo_c
                     elif units == FARANHEIT:                         
                         properties["hi" + str(y)] = "%3.0f°F" % hi_f
                         properties["lo" + str(y)] = "%3.0f°F" % lo_f
                     else:                                  
                         properties["hi" + str(y)] = "%3.0f°K" % hi_k
                         properties["lo" + str(y)] = "%3.0f°K" % lo_k
     
                     properties["day" + str(y)] = forecast['day_of_week']
                     properties["day_letter" + str(y)] = forecast['day_of_week'][:1]
                     
                     c_icon, f_icon, t_icon = self._get_icons(forecast)
                     properties["icon" + str(y)] = g15icontools.get_embedded_image_url(g15cairo.load_surface_from_file(t_icon))
                     
                     y += 1
     
     return properties, attributes
예제 #5
0
def to_float_or_none(s):
    return g15pythonlang.to_float_or_none(s)