Ejemplo n.º 1
0
def home():
    # Get today's games and format the game data for the view
    games = schedule.games_today()
    y_games = schedule.games_yesterday()
    games['time_est'] = games['time'].apply(lambda x: x.strftime('%I:%M %p EST'))
    y_games['time_est'] = y_games['time'].apply(lambda x: x.strftime('%I:%M %p EST'))
    return render_template('home.html', games=games.to_dict('records'), y_games=y_games.to_dict('records'))
Ejemplo n.º 2
0
def run(*args):
    # Stops execution after runtime seconds if specified
    if args:
        signal.signal(signal.SIGALRM, lambda signum, frame: sys.exit(0))
        signal.alarm(args[0])

    games = schedule.games_today()
    team_abbrs_today = games.home_abbr.tolist() + games.away_abbr.tolist()
    team_terms_today = {team_abbr: ' '.join([schedule.teams_dict[team_abbr]] + twitter.hashtags_dict[team_abbr]) for
                        team_abbr in team_abbrs_today}

    for tweet in queue.consume():
        # Determine which team(s) and game(s) the tweet should be associated with
        similarities = tweet_to_team(tweet['text'], team_terms_today)
        relevant_teams = [team for team, similarity in similarities.iteritems() if similarity != 0.]

        relevant_game_ids = {}
        for team in relevant_teams:
            # Get the game the team is playing in
            game_id = games[(games.home_abbr == team) | (games.away_abbr == team)].index.tolist()[0]
            if game_id in relevant_game_ids:
                relevant_game_ids[game_id].append(team)
            else:
                relevant_game_ids[game_id] = [team]

        if relevant_game_ids:
            for relevant_game_id, teams in relevant_game_ids.iteritems():
                if schedule.game_on(games.loc[relevant_game_id]):
                    # update_tweets(relevant_game_id, teams, tweet)
                    update_sentiment(relevant_game_id, teams, tweet)
Ejemplo n.º 3
0
def run(*args):
    # Stops execution after runtime seconds if specified
    if args:
        signal.signal(signal.SIGALRM, lambda signum, frame: sys.exit(0))
        signal.alarm(args[0])

    season_id = 20152016
    games = schedule.games_today()

    s = sched.scheduler(time.time, time.sleep)

    def update_all_plays(sc):
        [update_plays(season_id, game_id) for game_id in games.index if schedule.game_on(games.ix[game_id])]
        sc.enter(60, 1, update_all_plays, (sc,))

    s.enter(60, 1, update_all_plays, (s,))
    s.run()
Ejemplo n.º 4
0
def run(*args):
    # Stops execution after runtime seconds if specified
    if args:
        signal.signal(signal.SIGALRM, lambda signum, frame: sys.exit(0))
        signal.alarm(args[0])

    # Get today's games
    games = schedule.games_today()

    # Get hashtags of all teams playing
    home_team_hashtag_lists = games['home_abbr'].apply(lambda x: hashtags_dict[x])
    away_team_hashtag_lists = games['away_abbr'].apply(lambda x: hashtags_dict[x])
    team_hashtag_lists = home_team_hashtag_lists + away_team_hashtag_lists
    hashtags = [hashtag for hashtags in team_hashtag_lists.tolist() for hashtag in hashtags]

    # Stream tweets for today's games, filtering by team names and hashtags
    twitter_stream = Stream(auth, MyListener(api))
    twitter_stream.filter(track=games.home.tolist() + games.away.tolist() + hashtags)
Ejemplo n.º 5
0
def run(start_time):
    """Adds data stored in the Redis cache to a database for long-term storage

    :param start_time: start time of the first game for a given day
    """
    # Connect to database and redis
    database = db.database
    r = redis_connection()

    # Create the tables if they don't exist
    database.create_tables([Game, GameTweetMinute, Play], safe=True)

    # Figure out which day it is and add games from the appropriate day
    eastern = pytz.timezone('US/Eastern')
    now = datetime.now(eastern)

    if now.date() == start_time.date():
        games = schedule.games_today()
    else:
        games = schedule.games_yesterday()

    for game in games.iterrows():
        game_id = game[0]
        game = game[1]

        home_abbr = game.home_abbr
        away_abbr = game.away_abbr

        # Add the game to the database
        Game.create(id=game_id, home_abbr=home_abbr, away_abbr=away_abbr, date=game.date, time_est=game.time)

        # Gather Redis set keys
        sentiment_key = 'sentiment:%s' % game_id
        home_sentiment_key = '%s:%s' % (sentiment_key, home_abbr)
        away_sentiment_key = '%s:%s' % (sentiment_key, away_abbr)
        plays_key = 'plays:%s' % game_id

        # Get the minute timestamps when sentiment or plays were gathered
        pipe = r.pipeline()
        pipe.smembers(sentiment_key)
        pipe.smembers(home_sentiment_key)
        pipe.smembers(away_sentiment_key)
        pipe.smembers(plays_key)
        sentiment_timestamps, home_sentiment_timestamps, away_sentiment_timestamps, plays_timestamps = pipe.execute()  # Only create the tables if they do not exist.

        # Take union of sentiment and play timestamps
        timestamps = list(sentiment_timestamps.union(plays_timestamps))

        # Get the sentiment and play data for each timestamp
        pipe = r.pipeline()
        for timestamp in timestamps:
            pipe.hgetall('%s:%s' % (sentiment_key, timestamp))
            pipe.hgetall('%s:%s' % (home_sentiment_key, timestamp))
            pipe.hgetall('%s:%s' % (away_sentiment_key, timestamp))
            pipe.lrange('%s:%s' % (plays_key, timestamp), 0, -1)

        results = pipe.execute()

        for i, timestamp in enumerate(timestamps):
            sentiment, home_sentiment, away_sentiment, plays = results[4 * i:4 * i + 4]

            game_tweet_minute = {'game_id': game_id, 'datetime': datetime.utcfromtimestamp(int(timestamp))}

            # This is hacky - fix this later
            if sentiment:
                game_tweet_minute['tweet_count'] = sentiment['count']
                game_tweet_minute['sentiment_sum'] = sentiment['sum']
                game_tweet_minute['sentiment_sum_sq'] = sentiment['sumsq']

            if home_sentiment:
                game_tweet_minute['home_tweet_count'] = home_sentiment['count']
                game_tweet_minute['home_sentiment_sum'] = home_sentiment['sum']
                game_tweet_minute['home_sentiment_sum_sq'] = home_sentiment['sumsq']

            if away_sentiment:
                game_tweet_minute['away_tweet_count'] = away_sentiment['count']
                game_tweet_minute['away_sentiment_sum'] = away_sentiment['sum']
                game_tweet_minute['away_sentiment_sum_sq'] = away_sentiment['sumsq']

            try:
                with database.transaction():
                    GameTweetMinute.create(**game_tweet_minute)
            except peewee.IntegrityError:
                pass

            for play in plays:
                play = ast.literal_eval(play)
                play = {key: play[key] for key in ['team_abbr', 'hs', 'as', 'type', 'desc']}
                play['game_id'] = game_id
                play['datetime'] = datetime.utcfromtimestamp(int(timestamp))
                play['home_score'] = play.pop('hs')
                play['away_score'] = play.pop('as')
                try:
                    with database.transaction():
                        Play.create(**play)
                except peewee.IntegrityError:
                    pass

    database.close()