Example #1
0
def daily_update():
    """Update the data of yesterday and forecasted data of the following 7 days."""
    for city_index in xrange(len(CITY_MAP)):
        yesterday = datetime.utcnow() - timedelta(days=1)
        this_city = forecast(API_KEY, *CITY_MAP[city_index][1], time=yesterday, units='si')
        update_process(this_city, city_index)

        this_city = forecast(API_KEY, *CITY_MAP[city_index][1], units='si')
        update_process(this_city, city_index)
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
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
Example #4
0
def download_historical_data():
    """Download the historical daily data as a csv file."""
    city_count = len(CITY_MAP)
    tracked_period = API_CALLS // city_count

    for city_index in xrange(city_count):
        report = DataFrame()
        tracking_day = datetime.utcnow()

        for i in xrange(tracked_period):
            tracking_day -= timedelta(days=1)
            this_city = forecast(API_KEY, *CITY_MAP[city_index][1], time=tracking_day, units='si')
            daily_record = this_city.daily().data[0]

            record = DailyRecord(
                city_id=city_index,
                year=tracking_day.year,
                month=tracking_day.month,
                day=tracking_day.day,
                max_temperature=daily_record.temperatureHigh,
                min_temperature=daily_record.temperatureLow,
            )
            # session.add(record)
            # session.commit()

            to_be_inserted = {
                'city_id': [record.city_id],
                'year': [record.year],
                'month': [record.month],
                'day': [record.day],
                'max_temperature': [record.max_temperature],
                'min_temperature': [record.min_temperature]
            }
            report = report.append(DataFrame(to_be_inserted))

            print '{} historical record on {}: {}/{}'.format(
                CITY_MAP[city_index][0],
                tracking_day.date(),
                record.max_temperature,
                record.min_temperature
            )

        os.chdir('/usr/src/app/reports')
        report.to_csv('{}_report.csv'.format(CITY_MAP[city_index][0]), encoding='utf-8',
                      columns=['city_id', 'year', 'month', 'day', 'max_temperature', 'min_temperature'])
Example #5
0
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
Example #6
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