Ejemplo n.º 1
0
 def test_localize_utc_datetime(self):
     """Testing localizing a plain datetime object to a pytz timezone aware object"""
     dt = datetime.datetime.fromtimestamp(1461731335)  # datetime.datetime(2016, 4, 26, 23, 28, 55)
     timezone_id = 'Europe/Copenhagen'
     localized_dt = utils.localize_utc_datetime(timezone_id, dt)
     correct_dt = datetime.datetime.fromtimestamp(1461738535)  # datetime.datetime(2016, 4, 27, 1, 28, 55)
     self.assertEqual(localized_dt, pytz.timezone('Europe/Copenhagen').localize(correct_dt))
Ejemplo n.º 2
0
def tweet_logic(weather_data, wb_string):
    """
    Core logic for tweets once initialization and configuration has been set and weather data fetched.
    :type weather_data: models.WeatherData
    :type wb_string: models.WeatherBotString
    """
    # pylint: disable=global-variable-not-assigned
    # CACHE is being modified here, pylint doesn't see that
    global CACHE
    wb_string.set_weather(weather_data)
    special = wb_string.special()
    normal_text = wb_string.normal()

    now = datetime.utcnow()
    now_utc = utils.datetime_to_utc('UTC', now)
    now_local = utils.localize_utc_datetime(weather_data.timezone, now)

    # weather alerts
    for alert in weather_data.alerts:
        if alert.sha() not in CACHE['throttles'] and not alert.expired(now_utc):
            try:
                CACHE['throttles'][alert.sha()] = alert.expires
            except AttributeError:
                # most alerts are probably done after 3 days
                CACHE['throttles'][alert.sha()] = alert.time + timedelta(days=3)
            do_tweet(wb_string.alert(alert, weather_data.timezone),
                     weather_data.location,
                     CONFIG['basic']['tweet_location'],
                     CONFIG['variable_location']['enabled'],
                     hashtag=CONFIG['basic']['hashtag'])

    # forecast
    forecast_dt = now_local.replace(hour=CONFIG['scheduled_times']['forecast'].hour,
                                    minute=CONFIG['scheduled_times']['forecast'].minute,
                                    second=0, microsecond=0).astimezone(pytz.utc)
    timed_tweet(forecast_dt, now_utc, wb_string.forecast(), weather_data.location)

    # scheduled tweet
    for scheduled_time in CONFIG['scheduled_times']['conditions']:
        scheduled_dt = now_local.replace(hour=scheduled_time.hour,
                                         minute=scheduled_time.minute,
                                         second=0, microsecond=0).astimezone(pytz.utc)
        timed_tweet(scheduled_dt, now_utc, normal_text, weather_data.location)

    # special condition
    if special.type != 'normal':
        logging.debug('Special event')
        try:
            next_allowed = CACHE['throttles'][special.type]
        except KeyError:
            next_allowed = CACHE['throttles']['default']

        if now_utc >= next_allowed:
            try:
                minutes = CONFIG['throttles'][special.type]
            except KeyError:
                minutes = CONFIG['throttles']['default']
            do_tweet(special.text,
                     weather_data.location,
                     CONFIG['basic']['tweet_location'],
                     CONFIG['variable_location']['enabled'],
                     hashtag=CONFIG['basic']['hashtag'])
            CACHE['throttles'][special.type] = now_utc + timedelta(minutes=minutes)
        logging.debug(CACHE)
Ejemplo n.º 3
0
def tweet_logic(weather_data, wb_string):
    """
    Core logic for tweets once initialization and configuration has been set and weather data fetched.
    :type weather_data: models.WeatherData
    :type wb_string: models.WeatherBotString
    """
    # pylint: disable=global-variable-not-assigned
    # CACHE is being modified here, pylint doesn't see that
    global CACHE
    wb_string.set_weather(weather_data)
    logging.debug(wb_string.__dict__())
    special = wb_string.special()
    normal_text = wb_string.normal()

    now = datetime.utcnow()
    now_utc = utils.datetime_to_utc('UTC', now)
    now_local = utils.localize_utc_datetime(weather_data.timezone, now)

    # weather alerts
    for alert in weather_data.alerts:
        if alert.sha() not in CACHE['throttles'] and not alert.expired(now_utc):
            local_expires_time = alert.expires.astimezone(pytz.timezone(weather_data.timezone))
            CACHE['throttles'][alert.sha()] = alert.expires
            do_tweet(wb_string.alert(alert.title, local_expires_time, alert.uri),
                     weather_data.location,
                     CONFIG['basic']['tweet_location'],
                     CONFIG['variable_location']['enabled'],
                     hashtag=CONFIG['basic']['hashtag'])

    # forecast
    forecast_dt = now_local.replace(hour=CONFIG['scheduled_times']['forecast'].hour,
                                    minute=CONFIG['scheduled_times']['forecast'].minute,
                                    second=0, microsecond=0).astimezone(pytz.utc)
    timed_tweet(forecast_dt, now_utc, wb_string.forecast(), weather_data.location)

    # scheduled tweet
    for scheduled_time in CONFIG['scheduled_times']['conditions']:
        scheduled_dt = now_local.replace(hour=scheduled_time.hour,
                                         minute=scheduled_time.minute,
                                         second=0, microsecond=0).astimezone(pytz.utc)
        timed_tweet(scheduled_dt, now_utc, normal_text, weather_data.location)

    # special condition
    if special.type != 'normal':
        logging.debug('Special event')
        try:
            next_allowed = CACHE['throttles'][special.type]
        except KeyError:
            next_allowed = CACHE['throttles']['default']

        if now_utc >= next_allowed:
            try:
                minutes = CONFIG['throttles'][special.type]
            except KeyError:
                minutes = CONFIG['throttles']['default']
            do_tweet(special.text,
                     weather_data.location,
                     CONFIG['basic']['tweet_location'],
                     CONFIG['variable_location']['enabled'],
                     hashtag=CONFIG['basic']['hashtag'])
            CACHE['throttles'][special.type] = now_utc + timedelta(minutes=minutes)
        logging.debug(CACHE)