def process_team_season_json(team, jdata, season): season_dicts = dictify(jdata['resultSets'][0]) for sdict in season_dicts: sdict['SEASON_ID'] = make_season_int(sdict['YEAR']) if sdict['SEASON_ID'] == season.year: log.debug(sdict['YEAR']) sdict['SEASON'] = season sdict['W'] = sdict['WINS'] sdict['L'] = sdict['LOSSES'] sdict['W_PCT'] = sdict['WIN_PCT'] sdict['PLAYOFF_WINS'] = sdict['PO_WINS'] sdict['PLAYOFF_LOSSES'] = sdict['PO_LOSSES'] sdict['NBA_FINALS_APPEARANCE'] = (sdict['NBA_FINALS_APPEARANCE'] != "N/A") sdict['TEAM'] = team sdict['SEASON_TYPE'] = jdata['parameters']['SeasonType'] sdict['PER_MODE'] = jdata['parameters']['PerMode'] sdict = convert_dict_keys_to_lowercase( sdict, override_list=["CONF_RANK", "DIV_RANK", "W", "L"], aux_list=[ "YEAR", "WINS", "LOSSES", "WIN_PCT", "PO_WINS", "PO_LOSSES" ]) filter_dict = make_unique_filter_dict(TeamSeason, sdict) season, created = TeamSeason.objects.update_or_create( **filter_dict, defaults=sdict) if created: log.debug(("Created team season ", filter_dict))
def create_play_by_play_for_game(game): log.debug("Creating Play By Play Events for Game " + str(game.game_id)) pbp_events = [] url = (NBA_BASE_URL + PBP_URL).format(game_id=game.game_id, season=make_season_str(game.season), season_type='foo') json_data = get_json_response(url) pbp_data = json_data['resultSets'][0] headers = pbp_data['headers'] for pbp_event in pbp_data['rowSet']: temp_dict = dict(zip(headers, pbp_event)) temp_dict['GAME'] = game # TODO: Write a method that will look for a player and if it doesn't find, creates it. # TODO: The method should then return the player object if temp_dict['PLAYER1_NAME']: player1 = Player.objects.filter( player_id=temp_dict['PLAYER1_ID']).first() if not player1: player1 = Player(player_id=temp_dict['PLAYER1_ID'], display_first_last=temp_dict['PLAYER1_NAME']) player1.save() temp_dict['PLAYER1'] = player1 if temp_dict['PLAYER1_TEAM_ID']: temp_dict['PLAYER1_TEAM'] = Team.objects.filter( team_id=temp_dict['PLAYER1_TEAM_ID']).first() if temp_dict['PLAYER2_NAME']: player2 = Player.objects.filter( player_id=temp_dict['PLAYER2_ID']).first() if not player2: player2 = Player(player_id=temp_dict['PLAYER2_ID'], display_first_last=temp_dict['PLAYER2_NAME']) player2.save() temp_dict['PLAYER2'] = player2 if temp_dict['PLAYER2_TEAM_ID']: temp_dict['PLAYER2_TEAM'] = Team.objects.filter( team_id=temp_dict['PLAYER2_TEAM_ID']).first() if temp_dict['PLAYER3_NAME']: player3 = Player.objects.filter( player_id=temp_dict['PLAYER3_ID']).first() if not player3: player3 = Player(player_id=temp_dict['PLAYER3_ID'], display_first_last=temp_dict['PLAYER1_NAME']) player3.save() temp_dict['PLAYER3'] = player3 if temp_dict['PLAYER3_TEAM_ID']: temp_dict['PLAYER3_TEAM'] = Team.objects.filter( team_id=temp_dict['PLAYER3_TEAM_ID']).first() if temp_dict['SCOREMARGIN'] == 'TIE': temp_dict['SCOREMARGIN'] = 0 pbp_create_data = convert_dict_keys_to_lowercase(temp_dict) pbp_obj = PlayByPlayEvent(**pbp_create_data) pbp_events.append(pbp_obj) return pbp_events
def create_team_from_web(team_id): # TODO: Unnest this method call. Blech data_dict = convert_dict_keys_to_lowercase( get_team_info_from_web(team_id=team_id), override_list=['TEAM_ID']) log.info(data_dict) team = Team(**data_dict) log.debug("Team to create: %s %s %s" % (team.team_id, team.city, team.name)) team.save()
def line_scores(self): lscores = self._get_data(5) lscores_list = [] for lscore in lscores: lscore['GAME_DATE_EST'] = convert_date(lscore['GAME_DATE_EST']) lscore['TEAM'] = Team.objects.get(team_id=lscore['TEAM_ID']) lscore['GAME'] = self.game lscore = convert_dict_keys_to_lowercase(lscore) lscores_list.append(LineScore(**lscore)) return lscores_list
def create_update_split_from_raw_json(entity, raw_json): splits = [] if "Message" in raw_json.keys(): # For whatever reason, certain combinations of measure/permode/year aren't available return [] parms = raw_json['parameters'] if "shooting" in raw_json['resource']: measure_type = "Shooting" else: measure_type = parms['MeasureType'] result_sets = raw_json['resultSets'] aux_ignore = ['OPP_PTS_2ND_CHANCE', 'PCT_PTS_2PT_MR'] override = ['GROUP_SET', 'GROUP_VALUE', 'W', 'L'] for rset in result_sets: rows = dictify(rset) for row in rows: if measure_type == "Misc": row['PTS_SECOND_CHANCE'] = row['PTS_2ND_CHANCE'] row['OPP_PTS_SECOND_CHANCE'] = row['OPP_PTS_2ND_CHANCE'] elif measure_type == "Scoring": row['PCT_PTS_2PT_MIDRANGE'] = row['PCT_PTS_2PT_MR'] elif measure_type == "Shooting": if row['GROUP_SET'] == "Assisted By": row['GROUP_VALUE'] = row['PLAYER_NAME'] conv_data = convert_dict_keys_to_lowercase(row, override_list=override, aux_list=aux_ignore) if isinstance(entity, Player): conv_data['player'] = entity elif isinstance(entity, Team): conv_data['team'] = entity conv_data['season_id'] = make_season_int(parms['Season']) conv_data['season_type'] = parms['SeasonType'] conv_data['per_mode'] = parms['PerMode'] conv_data['measure_type'] = measure_type # This if is the result of an impossible to reproduce scenario where we received # A phantom base split for Harrison barnes that said he played two games at starting # position 'None' in 2016-17. If you can figure it out, tell me. if conv_data['group_value'] is None: continue try: split = instantiate_correct_split_type(conv_data) except Exception as e: log.debug(("RAW PARMS", raw_json['parameters'])) log.debug(("RAW RESOURCE ", raw_json['resource'])) log.debug(("ROW", row)) log.debug(("CONV DATA", conv_data)) log.exception(e) raise e splits.append(split) return splits
def other_stats(self): ostats = self._get_data(1) ostats_list = [] for ostat in ostats: ostat['GAME'] = self.game ostat['TEAM'] = (self.game.home_team if ostat['TEAM_ID'] == self.game.home_team.team_id else self.game.visitor_team) ostat['PTS_SECOND_CHANCE'] = ostat['PTS_2ND_CHANCE'] ostat = convert_dict_keys_to_lowercase(ostat) ostat_obj = GameOtherStats(**ostat) ostats_list.append(ostat_obj) return ostats_list
def play_by_play(self): pbp_list = [] pbp_events = self._get_data(0) for event in pbp_events: event['GAME'] = self.game if event['PLAYER1_NAME']: player1 = Player.objects.filter( player_id=event['PLAYER1_ID']).first() if not player1: player1 = Player(player_id=event['PLAYER1_ID'], display_first_last=event['PLAYER1_NAME']) player1.save() event['PLAYER1'] = player1 if event['PLAYER1_TEAM_ID']: event['PLAYER1_TEAM'] = Team.objects.filter( team_id=event['PLAYER1_TEAM_ID']).first() if event['PLAYER2_NAME']: player2 = Player.objects.filter( player_id=event['PLAYER2_ID']).first() if not player2: player2 = Player(player_id=event['PLAYER2_ID'], display_first_last=event['PLAYER2_NAME']) player2.save() event['PLAYER2'] = player2 if event['PLAYER2_TEAM_ID']: event['PLAYER2_TEAM'] = Team.objects.filter( team_id=event['PLAYER2_TEAM_ID']).first() if event['PLAYER3_NAME']: player3 = Player.objects.filter( player_id=event['PLAYER3_ID']).first() if not player3: player3 = Player(player_id=event['PLAYER3_ID'], display_first_last=event['PLAYER1_NAME']) player3.save() event['PLAYER3'] = player3 if event['PLAYER3_TEAM_ID']: event['PLAYER3_TEAM'] = Team.objects.filter( team_id=event['PLAYER3_TEAM_ID']).first() if event['SCOREMARGIN'] == 'TIE': event['SCOREMARGIN'] = 0 event = convert_dict_keys_to_lowercase(event) pbp = PlayByPlayEvent(**event) pbp_list.append(pbp) return pbp_list
def create_line_scores_for_game(game, line_score_data): line_scores = [] headers = line_score_data['headers'] details = line_score_data['rowSet'] for line_score in details: line_score[0] = convert_datetime_string_to_date_instance(line_score[0]) temp_dict = dict(zip(headers, line_score)) temp_dict['TEAM'] = Team.objects.get(team_id=temp_dict['TEAM_ID']) temp_dict['GAME'] = game ls_data = convert_dict_keys_to_lowercase(temp_dict) ls = LineScore(**ls_data) line_scores.append(ls) return line_scores
def official_xrefs(self): xrefs = [ convert_dict_keys_to_lowercase(data) for data in self._get_data(2) ] oxrefs = [] for xref in xrefs: official, created = Official.objects.get_or_create( official_id=xref['official_id'], defaults=xref) if created: log.debug( "Created a new official: {ofc}".format(ofc=str(official))) oxref = GameOfficialXref(game=self.game, official=official) oxrefs.append(oxref) return oxrefs
def create_player_from_web(player_id): data_dict = sanitize_player_data(get_player_info_from_web(player_id=player_id)) # Some historical players last played for a now defunct franchise. team, team_created = Team.objects.get_or_create(team_id=data_dict['TEAM_ID'], defaults={'team_id': data_dict['TEAM_ID'], 'team_name': data_dict['TEAM_NAME'], 'team_abbreviation': data_dict['TEAM_ABBREVIATION'], 'team_code': data_dict['TEAM_CODE'], 'team_city': data_dict['TEAM_CITY']}) if team_created: log.debug("Created team: %s" % team) data_dict['TEAM'] = team data_dict = convert_dict_keys_to_lowercase(data_dict) log.debug("Attempting to create player with data_dict: \n" + str(data_dict)) player = Player(**data_dict) return player
def create_update_all_seasons_for_player(player, year=None): player_seasons = [] url = NBA_BASE_URL + PLAYER_CAREER_STATS_ENDPOINT parms = PLAYER_CAREER_STATS_PARAMS parms['PlayerID'] = player.player_id # Removing Per36 & PerGame cut down the requests by 2/3 per_mode = "Totals" parms['PerMode'] = per_mode json_data = get_json_response(url, params=parms) result_sets = json_data['resultSets'] for rset in result_sets: season_type = rset['name'] if not any(ignore in season_type for ignore in IGNORE_SEASON_TYPES): headers = rset['headers'] rows = rset['rowSet'] team = None for row in rows: data = dict(zip(headers, row)) # log.debug("Data: " + str(data)) dteam_key = "TEAM_ID" if "TEAM_ID" in data else "Team_ID" if team is None or data[dteam_key] != team.team_id: try: team = Team.objects.get(team_id=data[dteam_key]) except Exception as e: log.debug(("ELEPHANT", data)) raise e conv_data = convert_dict_keys_to_lowercase(data, override_list=['LEAGUE_ID']) conv_data['season_id'] = make_season_int(conv_data.get('season_id', 0)) # year=None -> update/create all seasons. Always update career. if year is not None and conv_data['season_id'] not in [0, year]: continue conv_data['season_type'] = season_type conv_data['player'] = player conv_data['team'] = team conv_data['per_mode'] = per_mode # log.debug("Converted Data: " + str(conv_data)) filters = make_unique_filter_dict(PlayerSeason, conv_data) season, created = PlayerSeason.objects.update_or_create(**filters, defaults=conv_data) if created: log.debug(("Created a new season: ", filters)) player_seasons.append(season) return player_seasons
def create_other_stats_for_game(game, other_stats_data): log.debug("Creating OtherStats for game {g}".format(g=game.game_id)) other_stats = [] headers = other_stats_data['headers'] rows = other_stats_data['rowSet'] for row in rows: temp_dict = dict(zip(headers, row)) temp_dict['GAME'] = game temp_dict['TEAM'] = (game.home_team if temp_dict['TEAM_ID'] == game.home_team.team_id else game.visitor_team) temp_dict['PTS_SECOND_CHANCE'] = temp_dict['PTS_2ND_CHANCE'] ostats_data = convert_dict_keys_to_lowercase(temp_dict) ostats = GameOtherStats(**ostats_data) other_stats.append(ostats) return other_stats
def standardize_team_data(data, season_type, year): seasons_list = [] try: hdrs = data.get('headers', None) or data['resultSets'][0]['headers'] rows = data.get('rowSet', None) or data['resultSets'][0]['rowSet'] if rows: for s in rows: d = dict(zip(hdrs, s)) d['SEASON_TYPE'] = season_type d['SEASON_ID'] = year d['TEAM'] = Team.objects.get(team_id=d['TEAM_ID']) seasons_list.append(convert_dict_keys_to_lowercase(d)) except Exception as e: log.debug("Exception: " + str(e)) log.debug("Data that resulted in exception:\n" + str(data)) raise e return seasons_list
def fetch_raw_data(self, season=None, measure_type="Base", per_mode=TOTALS, season_type=REGULAR_SEASON, group_quantity=5): self.measure_type = measure_type self.model = get_model(prefix=self.measure_type, suffix="Lineup") self.raw_data = [] if measure_type in [USAGE, DEFENSE]: raise ValueError("Usage is not a valid measure type for lineups.") if season is None: season = make_season_str(determine_season_for_date(date.today())) log.debug(("Fetching data", season, season_type, measure_type, per_mode, group_quantity)) # Really hope this respects season type. I've seen scenarios where it doesn't lineup = Lineups(season=str(season), season_type=season_type, per_mode=per_mode, group_quantity=group_quantity, measure_type=measure_type) resp_json = lineup.json parms = resp_json['parameters'] result_set = resp_json['resultSets'][0] lineup_rows = dictify(result_set) for row in lineup_rows: row['TEAM'] = Team.objects.get(team_id=int(row['TEAM_ID'])) row['SEASON_ID'] = make_season_int(parms['Season']) row['MEASURE_TYPE'] = parms['MeasureType'] row['SEASON_TYPE'] = parms['SeasonType'] row['PER_MODE'] = parms['PerMode'] row['GROUP_QUANTITY'] = parms['GroupQuantity'] row['SEASON'] = season if measure_type == "Misc": row['PTS_SECOND_CHANCE'] = row['PTS_2ND_CHANCE'] row['OPP_PTS_SECOND_CHANCE'] = row['OPP_PTS_2ND_CHANCE'] elif measure_type == "Scoring": row['PCT_PTS_2PT_MIDRANGE'] = row['PCT_PTS_2PT_MR'] proc_row = convert_dict_keys_to_lowercase(row) self.raw_data.append(proc_row)
def create_game_official_xrefs(game, officials_data): log.debug("Creating OfficialXrefs for game {g}".format(g=game.game_id)) xrefs = [] headers = officials_data['headers'] rows = officials_data['rowSet'] for row in rows: temp_dict = dict(zip(headers, row)) ofc_data = convert_dict_keys_to_lowercase(temp_dict) official, created = Official.objects.get_or_create( official_id=ofc_data['official_id'], defaults=ofc_data) if created: log.debug( "Created a new official: {ofc}".format(ofc=str(official))) xref = GameOfficialXref(game=game, official=official) xrefs.append(xref) return xrefs
def create_player_shotchart_details_from_json(player, raw_json): headers = raw_json['headers'] shotchart_details = [] team = None game = None for row in raw_json['rowSet']: data_dict = dict(zip(headers, row)) data_dict['PLAYER'] = player if team is None or data_dict['TEAM_ID'] != team.team_id: team = Team.objects.get(team_id=data_dict['TEAM_ID']) data_dict['TEAM'] = team if game is None or data_dict['GAME_ID'] != game.game_id: game = Game.objects.get(game_id=data_dict['GAME_ID']) data_dict['GAME'] = game converted_dict = convert_dict_keys_to_lowercase(data_dict, isgame=True) shotchart_dtl = PlayerShotChartDetail(**converted_dict) shotchart_details.append(shotchart_dtl) return shotchart_details
def standardize_player_data(data, player=None, adv_flag=False, season_type='regular', season_str="2014-15"): seasons_list = [] hdrs = data['headers'] if data['rowSet']: for s in data['rowSet']: d = dict(zip(hdrs, s)) # log.debug(d) # Not sure how or why this happens, but just return the empty list and move on.s if d.get('PLAYER_NAME', "") is None: return seasons_list team_id = d.get('Team_ID', None) if team_id is None: team_id = d.get("TEAM_ID", None) team = Team.objects.get(team_id=team_id) if "Career" in data['name']: d['SEASON_ID'] = 0 d['TEAM_ABBREVIATION'] = team.team_abbreviation d['CAREER_FLAG'] = True else: d['CAREER_FLAG'] = False # Dunno why this has to be like this... if d.get('SEASON_ID', None): d['SEASON_ID'] = make_season_int(d['SEASON_ID']) else: # Temporary Hack d['SEASON_ID'] = make_season_int(season_str) if "Regular" in data['name']: d['SEASON_TYPE'] = "regular" elif "Post" in data['name']: d['SEASON_TYPE'] = "post" elif "AllStar" in data['name']: d['SEASON_TYPE'] = "all_star" d['PLAYER'] = Player.objects.get(player_id=d['PLAYER_ID']) if team: d['TEAM'] = team # Another temporary hack <--- Wtf was I doing here? # d['SEASON_TYPE'] = 'regular' lowercased = convert_dict_keys_to_lowercase(d) seasons_list.append(lowercased) # We now have a list of dicts that each specify a season return seasons_list
def game_headers(self, scoreboard_date=date.today(), season=None): self._fetch_raw_data(scoreboard_date) gh_dict = self.raw_data['resultSets'][0] columns = gh_dict['headers'] rows = gh_dict['rowSet'] uppercase_dicts = [dict(zip(columns, row)) for row in rows] season_type = season.determine_season_type_for_date(scoreboard_date) for data in uppercase_dicts: data['HOME_TEAM'] = Team.objects.get(team_id=data['HOME_TEAM_ID']) data['VISITOR_TEAM'] = Team.objects.get( team_id=data['VISITOR_TEAM_ID']) data['GAME_DATE_EST'] = convert_date(data['GAME_DATE_EST']) data['SEASON'] = season data['SEASON_TYPE'] = season_type converted_dicts = [ convert_dict_keys_to_lowercase(data, isgame=True) for data in uppercase_dicts ] return converted_dicts
def shotcharts(self, game_ids=None): shotchart_details = [] team = None game = None for detail in self.raw_data: if game_ids is not None and detail['GAME_ID'] not in game_ids: continue detail['PLAYER'] = self.player if team is None or detail['TEAM_ID'] != team.team_id: log.debug(detail['TEAM_ID']) team = Team.objects.get(team_id=detail['TEAM_ID']) log.debug(team) detail['TEAM'] = team if game is None or detail['GAME_ID'] != getattr(game, 'game_id', None): try: game = Game.objects.get(game_id=detail['GAME_ID']) except Exception as e: log.debug(("DATA", detail)) log.exception(e) raise e detail['GAME'] = game converted_dict = convert_dict_keys_to_lowercase(detail) filter_dict = make_unique_filter_dict(PlayerShotChartDetail, converted_dict) shotchart_dtl, created = PlayerShotChartDetail.objects.get_or_create(**filter_dict, defaults=converted_dict) if created: log.debug(("Created a new shot chart detail: ", filter_dict)) shotchart_details.append(shotchart_dtl) return shotchart_details
def boxscore(self, box_type="Traditional"): player_box_scores = [] team_box_scores = [] bkey = "playertrack" if box_type == "Tracking" else box_type.lower() log.debug(("bkey, btype", bkey, box_type)) data = self.raw_data.get(bkey.replace(" ", "")) if box_type == "Hustle": hustle_status = data['resultSets'][0]['rowSet'][0][1] if not hustle_status: player_stats = None else: player_stats = data['resultSets'][1] else: player_stats = data['resultSets'][0] if player_stats: player_headers = player_stats['headers'] if box_type == "Hustle": team_stats = data['resultSets'][2] else: team_stats = data['resultSets'][1] team_headers = team_stats['headers'] for p in player_stats['rowSet']: temp_dict = dict(zip(player_headers, p)) temp_dict['GAME'] = self.game temp_dict['TEAM'] = Team.objects.filter( team_id=temp_dict['TEAM_ID']).first() player = Player.objects.filter( player_id=temp_dict['PLAYER_ID']).first() if not player: # TODO: Properly handle the situation where the given player doesn't exist yet log.debug( "Player id and name about to look up: %s %s" % (temp_dict['PLAYER_ID'], temp_dict['PLAYER_NAME'])) # Laziest possible player creation... # should probably write a script that goes through the db and # attempts to fill in missing player data player = Player( display_first_last=temp_dict['PLAYER_NAME'], player_id=temp_dict['PLAYER_ID'], team=temp_dict['TEAM']) player.save() temp_dict['PLAYER'] = player if box_type == "Tracking": converted_dict = convert_tracking_dict_to_nbapex_fields( temp_dict, player_flg=True) else: converted_dict = temp_dict p_box_data = convert_dict_keys_to_lowercase(converted_dict) if box_type == 'Hustle': p_box_data['minutes'] = convert_min_sec_to_float( p_box_data['minutes']) else: p_box_data['min'] = convert_min_sec_to_float( p_box_data['min']) pbox = instantiate_correct_boxscore_type(p_box_data, box_type) player_box_scores.append(pbox) for t in team_stats['rowSet']: temp_dict = dict(zip(team_headers, t)) temp_dict['GAME'] = self.game temp_dict['TEAM'] = Team.objects.filter( team_id=temp_dict['TEAM_ID']).first() if box_type == 'Tracking': converted_dict = convert_tracking_dict_to_nbapex_fields( temp_dict, player_flg=False) else: converted_dict = temp_dict t_box_data = convert_dict_keys_to_lowercase(converted_dict) if box_type == "Hustle": t_box_data['minutes'] = convert_min_sec_to_float( t_box_data['minutes']) else: t_box_data['min'] = convert_min_sec_to_float( t_box_data['min']) tbox = instantiate_correct_boxscore_type(t_box_data, box_type) team_box_scores.append(tbox) else: log.debug( "Box scores of type {bt} not available for game {gm}".format( bt=box_type, gm=self.game.game_id)) ret_dict = {'players': player_box_scores, 'teams': team_box_scores} return ret_dict
def create_games(game_info): headers = game_info['resultSets'][0]['headers'] list_game_info = game_info['resultSets'][0]['rowSet'] games = [] line_scores = [] all_box_scores = [] pbp_events = [] other_stats = [] official_xrefs = [] for info in list_game_info: info[0] = convert_datetime_string_to_date_instance(info[0]) temp_dict = dict(zip(headers, info)) existing_game = Game.objects.filter(game_id=temp_dict['GAME_ID']) if existing_game.exists(): log.debug("Game {game} already exists!".format( game=existing_game.first())) log.debug("Avoided creating a new one. Moving on...") else: temp_dict['HOME_TEAM'] = Team.objects.get( team_id=temp_dict['HOME_TEAM_ID']) temp_dict['VISITOR_TEAM'] = Team.objects.get( team_id=temp_dict['VISITOR_TEAM_ID']) url = (NBA_BASE_URL + SUMMARY_URL).format(game_id=temp_dict['GAME_ID']) ancillary_game_data = get_json_response(url) ancillary_game_data = ancillary_game_data['resultSets'] # TODO: I'm sure we are going to get index OOB Exception here at some point. other_stats_data = ancillary_game_data[1] officials_data = ancillary_game_data[2] game_info_data = dict( zip(ancillary_game_data[4]['headers'], ancillary_game_data[4]['rowSet'][0])) line_score_data = ancillary_game_data[5] temp_dict['ATTENDANCE'] = game_info_data['ATTENDANCE'] temp_dict['GAME_TIME'] = convert_colon_tstamp_to_duration( game_info_data['GAME_TIME']) game_data = convert_dict_keys_to_lowercase(temp_dict, isgame=True) game = Game(**game_data) # Have to save the game now so that other object that reference can point to its id. # I'd like to find a way around this, but for now I don't know of one. game.save() games.append(game) line_scores += create_line_scores_for_game(game, line_score_data) # This is a dict of dicts containing a dictionary for each type of box score. # Each of those 5 dicts contains team and player box scores. all_box_scores_dict = create_all_box_scores_for_game(game) all_box_scores.append(all_box_scores_dict) pbp_events += create_play_by_play_for_game(game) other_stats += create_other_stats_for_game(game, other_stats_data) official_xrefs += create_game_official_xrefs(game, officials_data) ret_dict = { 'games': games, 'line_scores': line_scores, 'box_scores': all_box_scores, 'pbp_events': pbp_events, 'other_stats': other_stats, 'official_xrefs': official_xrefs } return ret_dict
def create_box_scores_for_game(game, url_suffix, season_type="Regular+Season", box_type="Traditional"): player_box_scores = [] team_box_scores = [] url = (NBA_BASE_URL + url_suffix).format(game_id=game.game_id, season=make_season_str( game.season), season_type=season_type) data = get_json_response(url) if box_type == "Hustle": hustle_status = data['resultSets'][0]['rowSet'][0][1] if not hustle_status: player_stats = None else: player_stats = data['resultSets'][1] else: player_stats = data['resultSets'][0] if player_stats: player_headers = player_stats['headers'] if box_type == "Hustle": team_stats = data['resultSets'][2] else: team_stats = data['resultSets'][1] team_headers = team_stats['headers'] for p in player_stats['rowSet']: temp_dict = dict(zip(player_headers, p)) temp_dict['GAME'] = game temp_dict['TEAM'] = Team.objects.filter( team_id=temp_dict['TEAM_ID']).first() player = Player.objects.filter( player_id=temp_dict['PLAYER_ID']).first() if not player: # TODO: Properly handle the situation where the given player doesn't exist yet log.debug("Player id and name about to look up: %s %s" % (temp_dict['PLAYER_ID'], temp_dict['PLAYER_NAME'])) # Laziest possible player creation... # should probably write a script that goes through the db and # attempts to fill in missing player data player = Player(display_first_last=temp_dict['PLAYER_NAME'], player_id=temp_dict['PLAYER_ID'], team=temp_dict['TEAM']) player.save() temp_dict['PLAYER'] = player if box_type == "Tracking": converted_dict = convert_tracking_dict_to_nbapex_fields( temp_dict, player_flg=True) else: converted_dict = temp_dict p_box_data = convert_dict_keys_to_lowercase(converted_dict) if box_type == 'Hustle': p_box_data['minutes'] = convert_min_sec_to_float( p_box_data['minutes']) else: p_box_data['min'] = convert_min_sec_to_float(p_box_data['min']) pbox = create_correct_type_of_player_box_score( p_box_data, box_type) player_box_scores.append(pbox) for t in team_stats['rowSet']: temp_dict = dict(zip(team_headers, t)) temp_dict['GAME'] = game temp_dict['TEAM'] = Team.objects.filter( team_id=temp_dict['TEAM_ID']).first() if box_type == 'Tracking': converted_dict = convert_tracking_dict_to_nbapex_fields( temp_dict, player_flg=False) else: converted_dict = temp_dict t_box_data = convert_dict_keys_to_lowercase(converted_dict) if box_type == "Hustle": t_box_data['minutes'] = convert_min_sec_to_float( t_box_data['minutes']) else: t_box_data['min'] = convert_min_sec_to_float(t_box_data['min']) tbox = create_correct_type_of_team_box_score(t_box_data, box_type) team_box_scores.append(tbox) else: log.debug("Box scores of type {bt} not available for game {gm}".format( bt=box_type, gm=game.game_id)) ret_dict = {'players': player_box_scores, 'teams': team_box_scores} return ret_dict
def fetch_raw_data(self, season=None, season_type=REGULAR_SEASON, pt_measure_type=SPEED_DISTANCE, per_mode=TOTALS, group_set="Overall", group_value=None): self.raw_data = [] self.pt_measure_type = pt_measure_type if self.pt_measure_type in ["CatchShoot", "PullUpShot"]: middle = "ShotType" elif "Touch" in self.pt_measure_type or "Drive" in self.pt_measure_type: middle = "Touch" else: middle = self.pt_measure_type self.model = get_model(prefix="Player", middle=middle, suffix="Tracking") # Silly way to do this, I know url = self.base_url + self.endpoint if season is None: season = make_season_str(determine_season_for_date(date.today())) if group_set == "Overall" and group_value is None: group_value = season self.params['Season'] = str(season) self.params['SeasonType'] = season_type self.params['PerMode'] = per_mode self.params['PtMeasureType'] = pt_measure_type if group_set != "Overall": self.params[group_set] = group_value if group_set != "Month": group_value = GROUP_VALUES[group_value] log.debug(("Fetching Player Stats: ", season, season_type, pt_measure_type, per_mode, group_set, group_value)) raw_json = get_json_response(url, self.params) rset = raw_json['resultSets'][0] player_rows = dictify(result_set=rset) processed_rows = [] for row in player_rows: row['PLAYER'] = Player.objects.get(player_id=row['PLAYER_ID']) row['GROUP_SET'] = group_set row['SEASON'] = season row['SEASON_TYPE'] = season_type row['PT_MEASURE_TYPE'] = pt_measure_type row['PER_MODE'] = per_mode if group_set == "Month": row['GROUP_VALUE'] = REVERSE_MONTH_MAP[group_value] else: row['GROUP_VALUE'] = group_value if self.pt_measure_type in ["CatchShoot", "PullUpShot"]: row = convert_shot_type_dict_to_apex(row) elif "Touch" in self.pt_measure_type or "Drive" in self.pt_measure_type: row = convert_touch_dict_to_apex(row) proc_row = convert_dict_keys_to_lowercase( row, override_list=['GROUP_SET', 'GROUP_VALUE']) processed_rows.append(proc_row) self.raw_data = processed_rows
def game_info(self): other_info = self._get_data(4)[0] return convert_dict_keys_to_lowercase(other_info)
def fetch_raw_data(self, player_or_team=PlayerStats, season=None, season_type=REGULAR_SEASON, measure_type=BASE, per_mode=TOTALS, group_set="Overall", group_value=None): self.measure_type = measure_type self.player_or_team = player_or_team if season is None: season = make_season_str(determine_season_for_date(date.today())) if group_set == "Overall" and group_value is None: group_value = season entity = "Player" if self.player_or_team == PlayerStats else "Team" log.debug( ("Fetching {entity} Stats: ".format(entity=entity), season, season_type, measure_type, per_mode, group_set, group_value)) if group_set != "Overall": if group_set != "Month": group_value = GROUP_VALUES[group_value] kwargs = {GROUP_SETS[group_set]: group_value} else: kwargs = {} stats = player_or_team(season=str(season), season_type=season_type, measure_type=measure_type, per_mode=per_mode, **kwargs) raw_json = stats.json rset = raw_json['resultSets'][0] stat_rows = dictify(result_set=rset) for row in stat_rows: if player_or_team == PlayerStats: row['PLAYER'] = Player.objects.get(player_id=row['PLAYER_ID']) else: row['TEAM'] = Team.objects.get(team_id=row['TEAM_ID']) row['GROUP_SET'] = group_set row['SEASON'] = season row['SEASON_TYPE'] = season_type row['MEASURE_TYPE'] = measure_type row['PER_MODE'] = per_mode if group_set == "Month": row['GROUP_VALUE'] = REVERSE_MONTH_MAP[group_value] else: row['GROUP_VALUE'] = group_value if measure_type == "Misc": row['PTS_SECOND_CHANCE'] = row['PTS_2ND_CHANCE'] row['OPP_PTS_SECOND_CHANCE'] = row['OPP_PTS_2ND_CHANCE'] elif measure_type == "Scoring": row['PCT_PTS_2PT_MIDRANGE'] = row['PCT_PTS_2PT_MR'] elif measure_type == "Shooting": if row['GROUP_SET'] == "Assisted By": row['GROUP_VALUE'] = row['PLAYER_NAME'] stat_rows = [ convert_dict_keys_to_lowercase( row, override_list=['GROUP_SET', 'GROUP_VALUE']) for row in stat_rows ] self.raw_data = stat_rows