Пример #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'))
Пример #2
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()