def test_get_team_ids_for_game(self): nba_slug = 'nba-2015-2016-mil-orl-2016-04-11-1900' mlb_slug = 'mlb-2016-mil-pit-2016-04-17-1335' nhl_slug = 'nhl-2015-2016-chi-stl-2016-04-15-1900' score_updater = ScoreUpdater() expected_nba = 'c7f2a75a-d6fc-4d6d-b75f-acbfb9a4aeed', '1bc89428-fb96-49cb-aae0-f6bf07a482a1' expected_mlb = '0644a90f-96ca-4d20-b83a-a6008462f89c', 'd6c517b0-78de-4834-8d36-b6e63b3224ee' expected_nhl = '4ca89483-65ae-484d-bda9-eacba9ff4915', 'a345b10e-87b5-4635-bf24-fd6be058346f' self.assertEqual(expected_nba, score_updater.get_team_ids_for_game(nba_slug, 'nba')) self.assertEqual(expected_mlb, score_updater.get_team_ids_for_game(mlb_slug, 'mlb')) self.assertEqual(expected_nhl, score_updater.get_team_ids_for_game(nhl_slug, 'nhl'))
class TwitterClient: def __init__(self): """ Initializes API keys and sets up Auth with Twitter API """ self.APP_KEY = CommonUtils.get_environ_variable('SPORTS_CANARY_APP_KEY') self.APP_SECRET = CommonUtils.get_environ_variable('SPORTS_CANARY_APP_KEY_SECRET') self.OAUTH_TOKEN = CommonUtils.get_environ_variable('SPORTS_CANARY_OAUTH_TOKEN') self.OAUTH_TOKEN_SECRET = CommonUtils.get_environ_variable('SPORTS_CANARY_OAUTH_TOKEN_SECRET') self.auth = tweepy.OAuthHandler(self.APP_KEY, self.APP_SECRET) self.auth.set_access_token(self.OAUTH_TOKEN, self.OAUTH_TOKEN_SECRET) self.api = tweepy.API(self.auth) self.logger = logging.getLogger(__name__) self.keyword_generator = KeywordGenerator() self.score_updater = ScoreUpdater() def tweet(self, tweet): """ Tweets out content :param tweet: message to be tweeted """ try: self.api.update_status(tweet) except: self.logger.error('Error sending: ' + tweet) def delete_latest_tweet(self): timeline = self.api.user_timeline(count=1) for t in timeline: self.api.destroy_status(t.id) @staticmethod def get_day_month(): return datetime.now().strftime('%m-%d') def enriched_tweet_based_on_confidence(self, tweet_percentage_tuple, teams_tuple, slug, sport): """ Tweets out with hashtags and confidence indicator. :param tweet_percentage_tuple: confidence for team_1 and team_2 :param teams_tuple: team_1, team_2 :param slug: game slug :param sport: Sport type - currently supports mlb, nba and nhl """ try: home_away_id_tuple = self.score_updater.get_team_ids_for_game(slug, sport) home_hashtags = self.create_space_separated_hashtags(self.keyword_generator.get_hashtags_for_team(team_id=home_away_id_tuple[0], sport=sport)) away_hashtags = self.create_space_separated_hashtags(self.keyword_generator.get_hashtags_for_team(team_id=home_away_id_tuple[1], sport=sport)) if tweet_percentage_tuple[0] > tweet_percentage_tuple[1]: if abs(tweet_percentage_tuple[0] - tweet_percentage_tuple[1]) <= 15: self.tweet(str(self.get_day_month()) + ' We predict the ' + teams_tuple[0] + ' will be victorious today against the ' + teams_tuple[1] + '. #' + sport.upper() + ' ' + home_hashtags + ' ' + away_hashtags) else: self.tweet(str(self.get_day_month()) + ' We feel confident the ' + teams_tuple[0] + ' will be victorious today against the ' + teams_tuple[1] + '. #' + sport.upper() + ' ' + home_hashtags + ' ' + away_hashtags) else: if abs(tweet_percentage_tuple[1] - tweet_percentage_tuple[0]) <= 15: self.tweet(str(self.get_day_month()) + ' We predict the ' + teams_tuple[1] + ' will be victorious today against the ' + teams_tuple[0] + '. #' + sport.upper() + ' ' + home_hashtags + ' ' + away_hashtags) else: self.tweet(str(self.get_day_month()) + ' We feel confident the ' + teams_tuple[1] + ' will be victorious today against the ' + teams_tuple[0] + '. #' + sport.upper() + ' ' + home_hashtags + ' ' + away_hashtags) return True except: #pragma: no cover self.logger.error('Error sending enriched tweet.') return False @staticmethod def create_space_separated_hashtags(list_of_hashtags): list_of_hashtags = ['#' + hashtag for hashtag in list_of_hashtags] return ' '.join(list_of_hashtags)