cwendt94/espn-api

Every Player's 'lineupSlotId' shows as "0" or "PG"

lightsout7 opened this issue · 1 comments

Sport

Basketball

Summary

I used the following code to find the respective player's position. However, the result for every player is "0" or in real terms "PG".

box = league.box_scores(matchup_period=1)

lineup = box[4].away_lineup

count = 0
for player in lineup:
    print(lineup[count].lineupSlot,':',lineup[count].name)
    count += 1

ALTERNATIVELY using the 'player.slot_position:

count = 0
for player in lineup:
    print(lineup[count].slot_position,':',lineup[count].name)
    count += 1

Which both resulted in the following:

PG : Jordan Poole
PG : Keldon Johnson
PG : Jaren Jackson Jr.
PG : Spencer Dinwiddie
PG : DeMar DeRozan
PG : Russell Westbrook
PG : Victor Wembanyama
PG : Kyle Kuzma
PG : Wendell Carter Jr.
PG : Shai Gilgeous-Alexander
PG : Talen Horton-Tucker
PG : P.J. Washington
PG : Jalen Green

I will preface that I am fairly intermediate in my coding knowledge. However, through some digging into the 'espn-api' functionality, I noticed that the 'PLAYER' class was retrieving 'lineupSlotId: 0' for every player.

I created my own retrieval tool for the respective player's lineupSlot through the api and have shown it below. However, I do hope this bug gets corrected in the 'espn-api' package.

scoringPeriod_rosters = {}

url_TeamId = 1
season_id = 2024
max_teams = len(league.teams)

rosters_df = pd.DataFrame(columns= ['Team1', 'Team2', 'Team3', 'Team4', 'Team5', 'Team6', 'Team7', 'Team8', 'Team9', 'Team10', 'Team11', 'Team12'])

scoringId_start = 1
max_scoringId = 5

scoringId = scoringId_start

for team in league_keys:
    
    scoringId = scoringId_start
    TeamId = url_TeamId - 1
    while scoringId < max_scoringId:
        print(f'Team{TeamId} in Period {scoringId}')
        url = f"https://lm-api-reads.fantasy.espn.com/apis/v3/games/fba/seasons/{season_id}/segments/0/leagues/{league_id}?forTeamId={url_TeamId}&scoringPeriodId={scoringId}&view=mRoster" 
            
        response = requests.get(url, cookies=espn_cookies)
        if API_status(response) == 200:
            data = response.json()

        entries = data['teams'][0]['roster']['entries']

        try:
            league_dict[f'Team{TeamId}'][f'{season_id}']
        except NameError:
            league_dict[f'Team{TeamId}'][f'{season_id}'] = {}
        else:
            pass
        
        
        league_dict[f'Team{TeamId}'][f'{season_id}'][f'period_{scoringId}'] = {}
        league_dict[f'Team{TeamId}'][f'{season_id}'][f'period_{scoringId}'][f'players'] = {}

        count = 0
        for key in entries:
            player_name = entries[count].get('playerPoolEntry').get('player').get('fullName')
            league_dict[f'Team{TeamId}'][f'{season_id}'][f'period_{scoringId}'][f'players'][f'{player_name}'] = {}
            league_dict[f'Team{TeamId}'][f'{season_id}'][f'period_{scoringId}'][f'players'][f'{player_name}']['lineupSlot'] = posId_to_pos(data['teams'][0]['roster']['entries'][count].get('lineupSlotId'))
            count += 1



        ### USED TO CREATE A DATAFRAME OF ROSTERS FOR EACH SCORIING PERIOD
        #
        #count = 0
        #loop_list = []
        #
        #for key in entries:
        #    loop_list.append(entries[count].get('playerPoolEntry').get('player').get('fullName'))
        #    count+=1
        #
        #fill_count = 0
        #if len(loop_list) < 14:
        #    max_fill_count = 14 - len(loop_list)
        #    while fill_count < max_fill_count:
        #        loop_list.append(np.NaN)
        #        fill_count += 1
        #        print("fill count loop")
        #
        #rosters_df[f'Team{TeamId}'] = loop_list
        
        scoringId += 1
    url_TeamId += 1

I see whats happening, box score functionality will grab the players stats and information for the whole matchup period if matchup_total=False is not set. For the whole matchup period is seems like ESPN API sets everyone to position zero. Try updating your box_score call with

league.box_scores(matchup_period=1, matchup_total=False)