cwendt94 / espn-api

ESPN Fantasy API! (Football, Basketball)
MIT License
614 stars 198 forks source link

Method to determine if a player was a starter or not for a given week #454

Closed Sharwin24 closed 1 year ago

Sharwin24 commented 1 year ago

Sport

Football

Summary

Currently, it seems like the only way to view a player's position is through the BoxPlayer class which contains a slotPosition field which I think is updated live as the player's position changes throughout the game. I'm not sure how to obtain a BoxPlayer object for a given league, a given week, and a given team (by team_id).

My intention is to generate the list of starters and bench players for each week for each team.

dtcarls commented 1 year ago

The player.slot_positions are stored with the box_score of that week, not just current lineup. You can get the different box scores by providing the week in the parameters. As an example league.box_scores(week=12)

I validated this for football at least.

Sharwin24 commented 1 year ago

Ah ok that makes sense and I was able to implement what I wanted:

# Given a team and a week, collect the starters and the bench players for each team
def getStartersAndBench(team_owner: str, week: int) -> (list, list):
  starters = []
  bench = []
  # Select the roster depending on the week
  myLeague.load_roster_week(week)
  boxScores = myLeague.box_scores(week)
  for boxScore in boxScores:
    if boxScore.home_team.owner == team_owner:
      lineup = boxScore.home_lineup
      break
    elif boxScore.away_team.owner == team_owner:
      lineup = boxScore.away_lineup
      break
  # Get the starters and bench
  for player in lineup:
    if player.slot_position == 'BE' or player.slot_position == 'IR':
      bench.append(player)
    else:
      starters.append(player)
  return (starters, bench)

Wondering if you have any feedback for this method to make it cleaner or more efficient