def _get_wund_weather(settings, location):
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data, cache = _load_cached_data('wund', location)

    if data is None:
        wunderground.set_key(settings['key.wund'])
        data = wunderground.forecast(location)
        cache = _save_cached_data('wund', location, data)

    conditions = data['current_observation']
    weather = {'current': {}, 'forecast': [], 'info': {}}
    weather['info']['time'] = \
        cache['wund']['forecasts'][location]['requested_at']

    weather['current'] = {
        'weather': conditions['weather'],
        'icon': conditions['icon'],
        'humidity': int(conditions['relative_humidity'][:-1])
    }
    if settings['units'] == 'us':
        weather['current']['temp'] = conditions['temp_f']
    else:
        weather['current']['temp'] = conditions['temp_c']

    days = data['forecast']['simpleforecast']['forecastday']
    today = date.today()

    def get_day_info(day):
        d = day['date']
        fdate = date(day=d['day'], month=d['month'], year=d['year'])

        info = {
            'conditions': day['conditions'],
            'precip': day['pop'],
            'icon': day['icon'],
            'date': fdate.strftime('%Y-%m-%d')
        }

        if fdate == today:
            info['day'] = 'Today'
        elif fdate.day - today.day == 1:
            info['day'] = 'Tomorrow'
        else:
            info['day'] = fdate.strftime('%A')

        if settings['units'] == 'us':
            info['temp_hi'] = day['high']['fahrenheit']
            info['temp_lo'] = day['low']['fahrenheit']
        else:
            info['temp_hi'] = day['high']['celsius']
            info['temp_lo'] = day['low']['celsius']

        return info

    forecast = [get_day_info(d) for d in days]
    weather['forecast'] = sorted(forecast, key=lambda d: d['date'])
    return weather
def _get_wund_weather(settings, location):
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data, cache = _load_cached_data('wund', location)

    if data is None:
        wunderground.set_key(settings['key.wund'])
        data = wunderground.forecast(location)
        cache = _save_cached_data('wund', location, data)

    conditions = data['current_observation']
    weather = {'current': {}, 'forecast': [], 'info': {}}
    weather['info']['time'] = \
        cache['wund']['forecasts'][location]['requested_at']

    weather['current'] = {
        'weather': conditions['weather'],
        'icon': conditions['icon'],
        'humidity': int(conditions['relative_humidity'][:-1])
    }
    if settings['units'] == 'us':
        weather['current']['temp'] = conditions['temp_f']
    else:
        weather['current']['temp'] = conditions['temp_c']

    days = data['forecast']['simpleforecast']['forecastday']
    today = date.today()

    def get_day_info(day):
        d = day['date']
        fdate = date(day=d['day'], month=d['month'], year=d['year'])

        info = {
            'conditions': day['conditions'],
            'precip': day['pop'],
            'icon': day['icon'],
            'date': fdate.strftime('%Y-%m-%d')
        }

        if fdate == today:
            info['day'] = 'Today'
        elif fdate.day - today.day == 1:
            info['day'] = 'Tomorrow'
        else:
            info['day'] = fdate.strftime('%A')

        if settings['units'] == 'us':
            info['temp_hi'] = day['high']['fahrenheit']
            info['temp_lo'] = day['low']['fahrenheit']
        else:
            info['temp_hi'] = day['high']['celsius']
            info['temp_lo'] = day['low']['celsius']

        return info

    forecast = [get_day_info(d) for d in days]
    weather['forecast'] = sorted(forecast, key=lambda d: d['date'])
    return weather
示例#3
0
def _get_wund_weather():
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data = _load_cached_data('wund', location)

    if data is None:
        wunderground.set_key(settings['key.wund'])
        data = wunderground.forecast(location)
        _save_cached_data('wund', location, data)

    def parse_alert(alert):
        data = {
            'description': alert['description'],
            'expires': datetime.fromtimestamp(int(alert['expires_epoch'])),
        }

        if 'level_meteoalarm' not in alert:
            # only generate URIs for US alerts
            try:
                zone = alert['ZONES'][0]
                data['uri'] = '{}/US/{}/{}.html'.format(SERVICES['wund']['url'],
                                                        zone['state'],
                                                        zone['ZONE'])
            except:
                location = '{},{}'.format(settings['location']['latitude'],
                                          settings['location']['longitude'])
                data['uri'] = wunderground.get_forecast_url(location)
        return data

    weather = {'current': {}, 'forecast': [], 'info': {}}

    if 'alerts' in data:
        weather['alerts'] = [parse_alert(a) for a in data['alerts']]

    conditions = data['current_observation']
    weather['info']['time'] = datetime.strptime(
        cache['wund']['forecasts'][location]['requested_at'], TIMESTAMP_FMT)

    if 'moon_phase' in data:
        def to_time(time_dict):
            hour = int(time_dict['hour'])
            minute = int(time_dict['hour'])
            dt = datetime.now().replace(hour=hour, minute=minute)
            return _localize_time(dt)

        moon_phase = data['moon_phase']
        weather['info']['sunrise'] = to_time(moon_phase['sunrise'])
        weather['info']['sunset'] = to_time(moon_phase['sunset'])

    try:
        r = urlparse.urlparse(conditions['icon_url'])
        parts = os.path.split(r[2])[-1]
        name, ext = os.path.splitext(parts)
        icon = name
    except:
        icon = conditions['icon']


    weather['current'] = {
        'weather': conditions['weather'],
        'icon': icon,
        'humidity': int(conditions['relative_humidity'][:-1])
    }
    if settings['units'] == 'us':
        weather['current']['temp'] = conditions['temp_f']
    else:
        weather['current']['temp'] = conditions['temp_c']

    days = data['forecast']['simpleforecast']['forecastday']

    def get_day_info(day):
        d = day['date']
        fdate = date(day=d['day'], month=d['month'], year=d['year'])

        info = {
            'conditions': day['conditions'],
            'precip': day['pop'],
            'icon': day['icon'],
            'date': fdate
        }

        if settings['units'] == 'us':
            info['temp_hi'] = day['high']['fahrenheit']
            info['temp_lo'] = day['low']['fahrenheit']
        else:
            info['temp_hi'] = day['high']['celsius']
            info['temp_lo'] = day['low']['celsius']

        return info

    forecast = [get_day_info(d) for d in days]
    weather['forecast'] = sorted(forecast, key=lambda d: d['date'])
    return weather
示例#4
0
    def _get_wund_weather(self):
        LOG.debug('getting weather from Weather Underground')
        location = '{},{}'.format(self.config['location']['latitude'],
                                  self.config['location']['longitude'])
        data = self._load_cached_data('wund', location)

        if data is None:
            wunderground.set_key(self.config['key.wund'])
            data = wunderground.forecast(location)
            self._save_cached_data('wund', location, data)

        def parse_alert(alert):
            data = {'description': alert['description']}
            try:
                data['expires'] = datetime.fromtimestamp(
                    int(alert['expires_epoch']))
            except ValueError:
                data['expires'] = None
                LOG.warn('invalid expiration time: %s', alert['expires_epoch'])

            if 'level_meteoalarm' not in alert:
                # only generate URIs for US alerts
                try:
                    zone = alert['ZONES'][0]
                    data['uri'] = '{}/US/{}/{}.html'.format(
                        SERVICES['wund']['url'], zone['state'], zone['ZONE'])
                except:
                    location = '{},{}'.format(
                        self.config['location']['latitude'],
                        self.config['location']['longitude'])
                    data['uri'] = wunderground.get_forecast_url(location)
            return data

        weather = {'current': {}, 'forecast': [], 'info': {}}

        if 'alerts' in data:
            weather['alerts'] = [parse_alert(a) for a in data['alerts']]

        conditions = data['current_observation']
        weather['info']['time'] = datetime.strptime(
            self.cache['wund']['forecasts'][location]['requested_at'],
            TIMESTAMP_FMT)

        if 'moon_phase' in data:

            def to_time(time_dict):
                hour = int(time_dict['hour'])
                minute = int(time_dict['minute'])
                dt = datetime.now().replace(hour=hour, minute=minute)
                return self._localize_time(dt)

            moon_phase = data['moon_phase']
            weather['info']['sunrise'] = to_time(moon_phase['sunrise'])
            weather['info']['sunset'] = to_time(moon_phase['sunset'])

        try:
            r = urlparse.urlparse(conditions['icon_url'])
            parts = os.path.split(r[2])[-1]
            name, ext = os.path.splitext(parts)
            icon = name
        except:
            icon = conditions['icon']

        feelslike = self.config.get('feelslike', False)

        weather['current'] = {
            'weather': conditions['weather'],
            'icon': icon,
            'humidity': int(conditions['relative_humidity'][:-1])
        }

        temp_kind = 'feelslike' if feelslike else 'temp'

        if self.config['units'] == 'us':
            weather['current']['temp'] = float(conditions[temp_kind + '_f'])
        else:
            weather['current']['temp'] = float(conditions[temp_kind + '_c'])

        days = data['forecast']['simpleforecast']['forecastday']

        def get_day_info(day):
            d = day['date']
            fdate = date(day=d['day'], month=d['month'], year=d['year'])

            info = {
                'conditions': day['conditions'],
                'precip': day['pop'],
                'icon': day['icon'],
                'date': fdate,
            }

            if self.config['units'] == 'us':
                info['temp_hi'] = day['high']['fahrenheit']
                info['temp_lo'] = day['low']['fahrenheit']
            else:
                info['temp_hi'] = day['high']['celsius']
                info['temp_lo'] = day['low']['celsius']

            return info

        forecast = [get_day_info(d) for d in days]
        weather['forecast'] = sorted(forecast, key=lambda d: d['date'])

        weather['forecast'][0]['sunrise'] = self._remotize_time(
            weather['info']['sunrise'])
        weather['forecast'][0]['sunset'] = self._remotize_time(
            weather['info']['sunset'])
        return weather