def get_plays(season_id, game_id): # Get the play-by-play data url = "http://live.nhl.com/GameData/%s/%s/PlayByPlay.json" % (season_id, game_id) data = requests.get(url).json()["data"]["game"] game = schedule.get_game(game_id) home = game["home_abbr"] away = game["away_abbr"] plays = pd.DataFrame(data["plays"]["play"]) # Figure out the UTC timestamp for each play game_date = game["date"] plays["localtime"] = pd.to_datetime(plays["localtime"].apply(lambda x: "%s %s" % (game_date, x))).dt.tz_localize( schedule.tz_dict[home] ) plays["utc_timestamp"] = plays["localtime"].astype(int) / 1e9 # Replace team ids with their abbreviations plays["team_abbr"] = plays["teamid"].replace( to_replace=[data["awayteamid"], data["hometeamid"]], value=[away, home] ) # Extract the properties that I want from each play plays = plays[["as", "hs", "desc", "utc_timestamp", "type", "time", "team_abbr"]] return plays
def get_game_plotdata(id): # Gather request parameters start_timestamp = request.args.get('start') end_timestamp = request.args.get('end') # Get home and away team abbreviations home_abbr = schedule.get_game(id).home_abbr away_abbr = schedule.get_game(id).away_abbr # Gather Redis set keys sentiment_key = 'sentiment:%s' % id home_sentiment_key = '%s:%s' % (sentiment_key, home_abbr) away_sentiment_key = '%s:%s' % (sentiment_key, away_abbr) plays_key = 'plays:%s' % 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() # Take union of sentiment and play timestamps timestamps = list(sentiment_timestamps.union(plays_timestamps)) # Grab a subset of the timestamps if start and end parameters requested if start_timestamp is not None: if end_timestamp is not None: timestamps = [timestamp for timestamp in timestamps if start_timestamp <= timestamp <= end_timestamp] timestamps = [timestamp for timestamp in timestamps if timestamp >= start_timestamp] # Helper function to format sentiment and play data into something convenient for the front end def format_data(timestamp, sentiment, home_sentiment, away_sentiment, plays): # Add the timestamp as a property timestamp_data = {'timestamp': timestamp} # Format the sentiment data - return a mean sentiment of 0 if there is no data for a timestamp if sentiment: timestamp_data['mean_sentiment'] = float(sentiment['sum']) # timestamp_data['mean_sentiment'] = float(sentiment['sum']) / float(sentiment['count']) timestamp_data['tweet_count'] = float(sentiment['count']) else: timestamp_data['mean_sentiment'] = 0. timestamp_data['tweet_count'] = 0. # Home plot data if home_sentiment: timestamp_data['home_mean_sentiment'] = float(home_sentiment['sum']) # timestamp_data['home_mean_sentiment'] = float(home_sentiment['sum']) / float(home_sentiment['count']) timestamp_data['home_tweet_count'] = float(home_sentiment['count']) else: timestamp_data['home_mean_sentiment'] = 0. timestamp_data['home_tweet_count'] = 0. # Away plot data if away_sentiment: timestamp_data['away_mean_sentiment'] = float(away_sentiment['sum']) # timestamp_data['away_mean_sentiment'] = float(away_sentiment['sum']) / float(away_sentiment['count']) timestamp_data['away_tweet_count'] = float(away_sentiment['count']) else: timestamp_data['away_mean_sentiment'] = 0. timestamp_data['away_tweet_count'] = 0. # Format the plays data - return an empty list if there is no data for a timestamp if len(plays) != 0: timestamp_data['plays'] = [ast.literal_eval(play) for play in plays] else: timestamp_data['plays'] = [] return timestamp_data # Dict to store properly formatted data. This is ultimately jsonified json_data = {} # 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() # Format the results into something convenient for the front end for i in range(0, len(results), 4): json_data[timestamps[i / 4]] = format_data(timestamps[i / 4], results[i], results[i + 1], results[i + 2], results[i + 3]) return jsonify(json_data)
def game_details(id): game = schedule.get_game(id) return game.to_json()
def game(id): game = schedule.get_game(id) game['time_est'] = game['time'].strftime('%I:%M %p EST') return render_template('game.html', game=game.to_dict())