コード例 #1
0
def _get_fio_weather(settings, location):
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data, cache = _load_cached_data('fio', location)

    if data is None or data['flags']['units'] != settings['units']:
        forecastio.set_key(settings['key.fio'])
        units = settings['units']
        data = forecastio.forecast(location, params={'units': units})
        cache = _save_cached_data('fio', location, data)

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

    weather['current'] = {
        'weather': conditions['summary'],
        'icon': FIO_TO_WUND.get(conditions['icon'], conditions['icon']),
        'humidity': conditions['humidity'] * 100,
        'temp': conditions['temperature']
    }

    days = data['daily']['data']
    today = date.today()

    def get_day_info(day):
        fdate = date.fromtimestamp(day['time'])
        if day['summary'][-1] == '.':
            day['summary'] = day['summary'][:-1]
        info = {
            'date': fdate.strftime('%Y-%m-%d'),
            'conditions': day['summary'],
            'icon': FIO_TO_WUND.get(day['icon'], day['icon']),
            'temp_hi': int(round(day['temperatureMax'])),
            'temp_lo': int(round(day['temperatureMin'])),
        }
        if 'precipProbability' in day:
            info['precip'] = 100 * day['precipProbability']

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

        return info

    forecast = [get_day_info(d) for d in days]
    weather['forecast'] = sorted(forecast, key=lambda d: d['date'])
    return weather
コード例 #2
0
def _get_fio_weather(settings, location):
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data, cache = _load_cached_data('fio', location)

    if data is None or data['flags']['units'] != settings['units']:
        forecastio.set_key(settings['key.fio'])
        units = settings['units']
        data = forecastio.forecast(location, params={'units': units})
        cache = _save_cached_data('fio', location, data)

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

    weather['current'] = {
        'weather': conditions['summary'],
        'icon': FIO_TO_WUND.get(conditions['icon'], conditions['icon']),
        'humidity': conditions['humidity'] * 100,
        'temp':  conditions['temperature']
    }

    days = data['daily']['data']
    today = date.today()

    def get_day_info(day):
        fdate = date.fromtimestamp(day['time'])
        if day['summary'][-1] == '.':
            day['summary'] = day['summary'][:-1]
        info = {
            'date': fdate.strftime('%Y-%m-%d'),
            'conditions': day['summary'],
            'icon': FIO_TO_WUND.get(day['icon'], day['icon']),
            'temp_hi': int(round(day['temperatureMax'])),
            'temp_lo': int(round(day['temperatureMin'])),
        }
        if 'precipProbability' in day:
            info['precip'] = 100 * day['precipProbability']

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

        return info

    forecast = [get_day_info(d) for d in days]
    weather['forecast'] = sorted(forecast, key=lambda d: d['date'])
    return weather
コード例 #3
0
ファイル: alfred_weather.py プロジェクト: camflan/jc-weather
def _get_fio_weather():
    location = '{},{}'.format(settings['location']['latitude'],
                              settings['location']['longitude'])
    data = _load_cached_data('fio', location)

    if data is None or data['flags']['units'] != settings['units']:
        forecastio.set_key(settings['key.fio'])
        units = settings['units']
        data = forecastio.forecast(location, params={'units': units})
        _save_cached_data('fio', location, data)

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

    if 'alerts' in data:
        alerts = []
        for alert in data['alerts']:
            alerts.append({
                'description': alert['title'],
                'expires': datetime.fromtimestamp(alert['expires']),
                'uri': alert['uri']
            })
        weather['alerts'] = alerts

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

    weather['current'] = {
        'weather': conditions['summary'],
        'icon': FIO_TO_WUND.get(conditions['icon'], conditions['icon']),
        'humidity': conditions['humidity'] * 100,
        'temp':  conditions['temperature']
    }

    days = data['daily']['data']
    sunrise = None
    sunset = None

    if len(days) > 0:
        today = days[0]
        weather['info']['sunrise'] = _localize_time(datetime.fromtimestamp(
            int(today['sunriseTime'])))
        weather['info']['sunset'] = _localize_time(datetime.fromtimestamp(
            int(today['sunsetTime'])))

    def get_day_info(day):
        fdate = _remotize_time(datetime.fromtimestamp(day['time'])).date()
        if day['summary'][-1] == '.':
            day['summary'] = day['summary'][:-1]
        info = {
            'date': fdate,
            'conditions': day['summary'],
            'icon': FIO_TO_WUND.get(day['icon'], day['icon']),
            'temp_hi': int(round(day['temperatureMax'])),
            'temp_lo': int(round(day['temperatureMin'])),
        }
        if 'precipProbability' in day:
            info['precip'] = 100 * day['precipProbability']

        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_fio_weather(self):
        LOG.debug('getting weather from Forecast.io')
        location = '{},{}'.format(self.config['location']['latitude'],
                                  self.config['location']['longitude'])
        data = self._load_cached_data('fio', location)

        if data is None or data['flags']['units'] != self.config['units']:
            forecastio.set_key(self.config['key.fio'])
            units = self.config['units']
            data = forecastio.forecast(location, params={'units': units})
            self._save_cached_data('fio', location, data)

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

        if 'alerts' in data:
            alerts = []
            for alert in data['alerts']:
                alerts.append({
                    'description':
                    alert['title'],
                    'expires':
                    datetime.fromtimestamp(alert['expires']),
                    'uri':
                    alert['uri']
                })
            weather['alerts'] = alerts

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

        feelslike = self.config.get('show-feelslike', False)
        temp_kind = 'apparentTemperature' if feelslike else 'temperature'

        weather['current'] = {
            'weather': conditions['summary'],
            'icon': FIO_TO_WUND.get(conditions['icon'], conditions['icon']),
            'humidity': conditions['humidity'] * 100,
            'temp': float(conditions[temp_kind])
        }

        days = data['daily']['data']

        if len(days) > 0:
            today = days[0]
            weather['info']['sunrise'] = \
                self._localize_time(datetime.fromtimestamp(
                    int(today['sunriseTime'])))
            weather['info']['sunset'] = \
                self._localize_time(datetime.fromtimestamp(
                    int(today['sunsetTime'])))

        def get_day_info(day):
            fdate = self._remotize_time(datetime.fromtimestamp(
                day['time'])).date()
            if day['summary'][-1] == '.':
                day['summary'] = day['summary'][:-1]
            info = {
                'date':
                fdate,
                'conditions':
                day['summary'],
                'icon':
                FIO_TO_WUND.get(day['icon'], day['icon']),
                'temp_hi':
                int(round(day['temperatureMax'])),
                'temp_lo':
                int(round(day['temperatureMin'])),
                'sunrise':
                self._remotize_time(
                    datetime.fromtimestamp(int(day['sunriseTime']))),
                'sunset':
                self._remotize_time(
                    datetime.fromtimestamp(int(today['sunsetTime']))),
            }
            if 'precipProbability' in day:
                info['precip'] = 100 * day['precipProbability']

            return info

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