def loadtest_logic(self, tweet, tweet_id, screen_name, user_id): """Logic similar to what is being used in the real bot so that we can load test how much volume it can handle before twitter kicks it off """ # use SUTime to parse a datetime out of tweet time_room = self.parse_time_room(tweet) # fake time in the future that imitates a event's start time local_tz = pytz.timezone('US/Pacific') sample_time = datetime.datetime.now(local_tz) + datetime.timedelta( minutes=10) sample_time = sample_time.strftime("%Y-%m-%d %H:%M:%S") converted_time = time_utils.convert_to_utc(sample_time) room = "r123" # check for a time and room conflict, only 1 set of retweets per event conflict = db_utils.check_time_room_conflict(converted_time, room) # send message to slack when a tweet is scheduled to go out slack_message = "{} From: {}, id: {}".format(tweet, screen_name, user_id) self.slacker.chat.post_message('#loadtest_tweets', slack_message) # This record lets us check that retweets not for same event db_utils.create_event(description=tweet, start=converted_time, location=room, creator=screen_name) tweet_utils.loadtest_schedule_tweets(screen_name, tweet, tweet_id, converted_time) print("tweet scheduled for retweet: {}".format(tweet))
def test_convert_to_utc_when_sutime_off_default_inside_gap(self): # time below is like a user saying tomorrow at 7pm when UTC is # already a day ahead of Portland sutime_str_after_17_00 = "2017-05-18T19:00" converted_time = time_utils.convert_to_utc(sutime_str_after_17_00) expected_output = datetime(2017, 5, 18, 2, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)
def retweet_logic(self, tweet, tweet_id, screen_name, user_id): """Use SUTime to try to parse a datetime out of a tweet, if successful save tweet to OutgoingTweet to be retweeted """ # use SUTime to parse a datetime out of tweet time_room = self.parse_time_room(tweet) # make sure both time and room extracted and only one val each val_check = self.value_check(time_room) if val_check == (1, 1): room = time_room["room"][0] date_mention = tweet_utils.check_date_mention(tweet) converted_time = time_utils.convert_to_utc(time_room["date"][0], date_mention) # check for a time and room conflict, only 1 set of retweets per event # default time range that a room is resrved for is -15 +30 mins conflict = db_utils.check_time_room_conflict(converted_time, room) if not conflict: # send message to slack when a tweet is scheduled to go out slack_message = "{} From: {}, id: {}".format( tweet, screen_name, user_id) self.send_slack_message('#outgoing_tweets', slack_message) self.send_mention_tweet(screen_name) # This record lets us check that retweets not for same event db_utils.create_event(description=tweet, start=converted_time, location=room, creator=screen_name) tweet_utils.schedule_tweets(screen_name, tweet, tweet_id, converted_time) loggly.info( "scheduled this tweet for retweet: {}".format(tweet)) else: message = """Tweet recived for an event bot is already scheduled to retweet about. Sender: {}, room: {}, time: {}, tweet: {} tweet_id: {} """ message = message.format(screen_name, room, converted_time, tweet, tweet_id) self.send_slack_message("#event_conflict", message) loggly.info(message) elif val_check == (0, 0): # tweet found but without valid time or room extracted, ignore pass else: # tweet with relevant information but not exactly 1 time & 1 room message = """Tweet found that needs review: {} tweet_id: {} screen_name: {}, user_id: {} """ message = message.format(tweet, tweet_id, screen_name, user_id) self.send_slack_message("#need_review", message)
def test_convert_to_utc_with_date_mention_accounts_for_midnight_utc(self): sutime_str = "2017-05-17T18:00" date_mention = ["5/19"] converted_time = time_utils.convert_to_utc(sutime_str, date_mention) # 18:00 is 01:00 in utc date next day expected_output = datetime(2017, 5, 20, 1, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)
def test_convert_to_utc_with_date_mention_changes_date(self): sutime_str = "2017-05-17T12:00" date_mention = ["5/19"] converted_time = time_utils.convert_to_utc(sutime_str, date_mention) # 12pm is 19:00 in utc date should be same as in date_mention expected_output = datetime(2017, 5, 19, 19, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)
def loadtest_logic(self, tweet, tweet_id, screen_name, user_id): """Logic similar to what is being used in the real bot so that we can load test how much volume it can handle before twitter kicks it off """ # use SUTime to parse a datetime out of tweet time_room = self.parse_time_room(tweet) # fake time in the future that imitates a event's start time local_tz = pytz.timezone('US/Eastern') sample_time = datetime.datetime.now(local_tz) + datetime.timedelta( minutes=10) sample_time = sample_time.strftime("%Y-%m-%d %H:%M:%S") event_time = time_utils.convert_to_utc(sample_time) room = random.randint(0, 3000) # check for a time and room conflict, only 1 set of retweets per event conflict = db_utils.check_time_room_conflict(event_time, room) if not conflict: # This record lets us check that retweets not for same event event_obj = db_utils.create_event(description=tweet, start=event_time, location=room, creator=screen_name) tweet_utils.loadtest_schedule_tweets(screen_name=screen_name, tweet=tweet, tweet_id=tweet_id, event_time=event_time, event_obj=event_obj) print("tweet scheduled for retweet: {}".format(tweet)) slack_msg = "{} From: {}, id: {}".format(tweet, screen_name, user_id) # self.send_slack_message('#outgoing_tweets', slack_message) send_slack_message(user_id=user_id, tweet_id=tweet_id, screen_name=screen_name, tweet_created=True, tweet=tweet, slack_msg=slack_msg, event_obj=event_obj) else: print("conflict when scheduling the tweet")
def retweet_logic(self, tweet, tweet_id, screen_name, user_id): """Use SUTime to try to parse a datetime out of a tweet, if successful save tweet to OutgoingTweet to be retweeted """ # use SUTime to parse a datetime out of tweet time_room = self.parse_time_room(tweet) # make sure both time and room extracted and only one val each val_check = self.value_check(time_room) if val_check == (1, 1): room = time_room["room"][0] date_mention = tweet_utils.check_date_mention(tweet) converted_time = time_utils.convert_to_utc(time_room["date"][0], date_mention) # check for a time and room conflict, only 1 set of retweets per event # default time range that a room is resrved for is -15 +30 mins conflict = db_utils.check_time_room_conflict(converted_time, room) if not conflict: # This record lets us check that retweets not for same event event_obj = db_utils.create_event(description=tweet, start=converted_time, location=room, creator=screen_name) tweet_utils.schedule_tweets(screen_name, tweet, tweet_id, converted_time, event_obj) slack_msg = "{} \n Tweet from: {}, \n id: {}".format( tweet, screen_name, user_id) send_slack_message(user_id=user_id, tweet_id=tweet_id, screen_name=screen_name, tweet_created=True, tweet=tweet, slack_msg=slack_msg) self.send_mention_tweet(screen_name) else: message = f"Tweet found for an already scheduled event: \n {tweet}" send_slack_message(user_id=user_id, tweet_id=tweet_id, screen_name=screen_name, tweet_created=False, tweet=tweet, slack_msg=message, channel="conflict") elif val_check == (0, 0): # tweet found but without valid time or room extracted, ignore pass else: # tweet with relevant information but not exactly 1 time & 1 room slack_msg = "Tweet found that needs review: {} \n tweet_id: {} \n screen_name: {} \n user_id: {}" slack_msg = slack_msg.format(tweet, tweet_id, screen_name, user_id) send_slack_message(user_id=user_id, tweet_id=tweet_id, screen_name=screen_name, tweet_created=False, tweet=tweet, slack_msg=slack_msg)
def test_convert_to_utc_when_sutime_off_default_outside_gap(self): # time for talk after midnight UTC sutime_str_after_17_00 = "2017-05-17T19:00" converted_time = time_utils.convert_to_utc(sutime_str_after_17_00) expected_output = datetime(2017, 5, 18, 2, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)
def test_convert_to_utc_when_sutime_off_default_outside_gap(self): sutime_str_after_17_00 = "2017-05-18T14:00" converted_time = time_utils.convert_to_utc(sutime_str_after_17_00) expected_output = datetime(2017, 5, 17, 21, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)
def test_convert_to_utc_returns_correct_time(self): time_str_from_sutime = "2017-08-5T08:00" converted_time = time_utils.convert_to_utc(time_str_from_sutime) expected_output = datetime(2017, 8, 4, 15, tzinfo=self.utc) self.assertEqual(converted_time, expected_output)