def test_get_teams_in_game_tuple(self):
        score_updater = ScoreUpdater()
        expected = 'Team_1', 'Team_2'
        self.assertEqual(expected, score_updater.get_teams_in_game_tuple('Team_1 vs Team_2'))

        # Test exception
        self.assertIsNone(score_updater.get_teams_in_game_tuple('Team_1 vs'))
    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'))
Ejemplo n.º 3
0
 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()
Ejemplo n.º 4
0
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)
 def test_get_slugs_of_games_that_need_updating(self):
     score_updater = ScoreUpdater()
     self.assertIsNotNone(score_updater.get_slugs_of_games_that_need_updating())
 def test_get_correct_percentages_and_write_to_mongo(self):
     score_updater = ScoreUpdater()
     self.assertEqual(True, score_updater.get_correct_percentages_and_write_to_mongo())
 def test_count_number_of_games_predicted(self):
     score_updater = ScoreUpdater()
     self.assertGreater(score_updater.count_number_of_games_predicted('all'), 151)
     self.assertGreater(score_updater.count_number_of_games_predicted('nba'), 36)
     self.assertGreater(score_updater.count_number_of_games_predicted('nhl'), 20)
     self.assertGreater(score_updater.count_number_of_games_predicted('mlb'), 20)
 def test_get_sport_documents(self):
     score_updater = ScoreUpdater()
     scores = score_updater.get_sport_documents('nba')
     self.assertGreater(len(scores), 5)
 def test_get_aws_mongo_db_events(self):
     score_updater = ScoreUpdater()
     self.assertIsNotNone(score_updater.get_aws_mongo_db_events())
 def test_get_success_percentage(self):
     score_updater = ScoreUpdater()
     self.assertEqual(50, score_updater.get_success_percentage(50, 50))
 def test_get_results_for_date(self):
     score_updater = ScoreUpdater()
     self.assertEqual(21, len(score_updater.get_results_for_date('2016-04-13')))
 def test_get_documents_that_are_missing_team_names(self):
     score_updater = ScoreUpdater()
     self.assertEqual([], score_updater.get_documents_that_are_missing_team_names())
 def test_get_all_documents(self):
     score_updater = ScoreUpdater()
     self.assertGreaterEqual(len(score_updater.get_all_documents()), 61)
 def test_count_number_of_right_and_wrong_predictions(self):
     score_updater = ScoreUpdater()
     self.assertGreater(score_updater.count_number_of_right_and_wrong_predictions('nba'), 40)
     self.assertGreater(score_updater.count_number_of_right_and_wrong_predictions(''), 30)
 def test_get_url_for_sport(self):
     score_updater = ScoreUpdater()
     self.assertEqual('https://www.stattleship.com/basketball/nba/', score_updater.get_url_for_sport('nba'))
     self.assertEqual('https://www.stattleship.com/hockey/nhl/', score_updater.get_url_for_sport('nhl'))
     self.assertEqual('https://www.stattleship.com/baseball/mlb/', score_updater.get_url_for_sport('mlb'))