Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    def _get_objects(self, detail_flag=False):
        log.debug("in _get objects")
        log.debug(("len(self.raw_data): ", len(self.raw_data)))
        objects = []
        idx = 1 if detail_flag else 0
        rawdata = self.raw_data[idx]
        parms = rawdata['parameters']
        measure_type = parms['MeasureType']
        on = rawdata['resultSets'][1]
        off = rawdata['resultSets'][2]
        rows = on['rowSet'] + off['rowSet']
        headers = on['headers']

        prefix = "Player"
        middle = measure_type.replace(" ", "") if detail_flag else ""
        suffix = "OnOffDetail" if detail_flag else "OnOffSummary"
        model = get_model(prefix, middle, suffix)

        for row in rows:
            data = dict(zip(headers, row))
            data['SEASON_ID'] = make_season_int(parms['Season'])
            data['MEASURE_TYPE'] = parms['MeasureType']
            data['SEASON_TYPE'] = parms['SeasonType']
            data['PER_MODE'] = parms['PerMode']
            data['TEAM'] = Team.objects.get(team_id=data['TEAM_ID'])
            data['PLAYER'] = Player.objects.get(player_id=data['VS_PLAYER_ID'])

            if detail_flag:
                if measure_type == "Misc":
                    data['PTS_SECOND_CHANCE'] = data['PTS_2ND_CHANCE']
                    data['OPP_PTS_SECOND_CHANCE'] = data['OPP_PTS_2ND_CHANCE']

                elif measure_type == "Scoring":
                    data['PCT_PTS_2PT_MIDRANGE'] = data['PCT_PTS_2PT_MR']

            final_data = auto_strip_and_convert_fields(model,
                                                       data,
                                                       uppercase=True,
                                                       make_instance=False)
            filter_dict = make_unique_filter_dict(model, final_data)
            obj, created = model.objects.update_or_create(**filter_dict,
                                                          defaults=final_data)
            if created:
                log.debug(("Created new OnOff ", filter_dict))

            objects.append(obj)

        return objects
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
def extract_coach_stats_from_bbref(coach, table):
    log.debug("Creating CoachSeasons for " + coach.display_first_last)
    coach_seasons = []
    rows = table.find('tbody').find_all('tr')

    for row in rows:
        # log.debug(row.prettify())
        try:
            season_id = make_season_int(row.find('th', {'data-stat': 'season'}).get_text())
            season_dict = {k: get_data_from_table_cell(row, k)
                           for k in BBREF_COACH_SEASON_DATA_FIELDS}
        except AttributeError as e:
            # An attribute indicates a season in which the coach was an assistant,
            # So while there is a table row present, there are not stats for that season
            continue
        season_dict['season_id'] = season_id
        season_dict['coach'] = coach
        coach_season = CoachSeason(**season_dict)
        coach_seasons.append(coach_season)
    return coach_seasons