Player's Actual Position in Box Player Class #580
-
Hi. I use this API to provide custom statistics that aren't tracked by espn fantasy to the rest of my league. One of the stats they would like is how many points they would have scored if they played a benched player instead of the player they started. It requires a comparison of players at the same position on a weekly basis, which is currently difficult to do because BoxPlayer only has slot_position, not the player's actual position. If Box_Player contained the actual position, I could automate the process to arrive at the stat I am trying to gather much easier. Would love to see this implemented. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @nineteenlights , I implement an automation of this stat in a repo of my own. The relevant code is here. Essentially, you can extract a team's Lineup pretty easily: def get_lineup(
league: League, team: Team, week: int, box_scores: Optional[List[BoxScore]] = None
) -> List[Player]:
"""Return the lineup of the given team during the given week"""
# Get the lineup for the team during the specified week
if box_scores is None:
box_scores = league.box_scores(week)
assert box_scores is not None
lineup = []
for box_score in box_scores:
if team == box_score.home_team:
lineup = box_score.home_lineup
elif team == box_score.away_team:
lineup = box_score.away_lineup
return lineup Let's also define a function to identify the top N players in a lineup for a given position. def get_top_players(lineup: List[Player], slot: str, n: int) -> List[Player]:
"""Takes a list of players and returns a list of the top n players based on points scored."""
# Gather players of the desired position
eligible_players = []
for player in lineup:
if slot in player.eligibleSlots:
eligible_players.append(player)
continue
return sorted(eligible_players, key=lambda x: x.points, reverse=True)[:n] Lastly, let's create a function to iterate through each slot in a league's starting lineup, and identify the best N players (without replacement) for each. def get_best_lineup(league: League, lineup: List[Player]) -> float:
"""Returns the score of the best possible lineup for team during the loaded week."""
# Save full roster
saved_roster = copy(lineup)
# Find Best Lineup
best_lineup = []
# Get best RB before best RB/WR/TE
for slot in sorted(league.roster_settings["starting_roster_slots"].keys(), key=len):
num_players = league.roster_settings["starting_roster_slots"][slot]
best_players = get_top_players(saved_roster, slot, num_players)
best_lineup.extend(best_players)
# Remove selected players from consideration for other slots
for player in best_players:
saved_roster.remove(player)
return np.sum([player.points for player in best_lineup]) |
Beta Was this translation helpful? Give feedback.
-
Also, I forgot to mention that I have a separate function I call when fetching a league object to store the roster settings. This function maps the def get_roster_settings(league: League) -> None:
"""This grabs the roster and starting lineup settings for the league
- Grabs the dictionary containing the number of players of each position a roster contains
- Creates a dictionary roster_slots{} that only inlcludes slotIds that have a non-zero number of players on the roster
- Creates a dictionary starting_roster_slots{} that is a subset of roster_slots{} and only includes slotIds that are on the starting roster
- Add roster_slots{} and starting_roster_slots{} to the League attribute League.rosterSettings
"""
print("[BUILDING LEAGUE] Gathering roster settings information...")
# This dictionary maps each slotId to the position it represents
rosterMap = {
0: "QB",
1: "TQB",
2: "RB",
3: "RB/WR",
4: "WR",
5: "WR/TE",
6: "TE",
7: "OP",
8: "DT",
9: "DE",
10: "LB",
11: "DL",
12: "CB",
13: "S",
14: "DB",
15: "DP",
16: "D/ST",
17: "K",
18: "P",
19: "HC",
20: "BE",
21: "IR",
22: "",
23: "RB/WR/TE",
24: " ",
}
endpoint = "{}view=mMatchupScore&view=mTeam&view=mSettings".format(league.endpoint)
r = requests.get(endpoint, cookies=league.cookies).json()
if type(r) == list:
r = r[0]
settings = r["settings"]
league.name = settings["name"]
# Grab the dictionary containing the number of players of each position a roster contains
roster = settings["rosterSettings"]["lineupSlotCounts"]
# Create an empty dictionary that will replace roster{}
roster_slots = {}
# Create an empty dictionary that will be a subset of roster_slots{} containing only starting players
starting_roster_slots = {}
for positionId in roster:
position = rosterMap[int(positionId)]
# Only inlclude slotIds that have a non-zero number of players on the roster
if roster[positionId] != 0:
roster_slots[position] = roster[positionId]
# Include all slotIds in the starting_roster_slots{} unless they are bench, injured reserve, or ' '
if positionId not in ["20", "21", "24"]:
starting_roster_slots[position] = roster[positionId]
# Add roster_slots{} and starting_roster_slots{} as a league attribute
league.roster_settings = {
"roster_slots": roster_slots,
"starting_roster_slots": starting_roster_slots,
}
return |
Beta Was this translation helpful? Give feedback.
Hi @nineteenlights , I implement an automation of this stat in a repo of my own. The relevant code is here.
Essentially, you can extract a team's Lineup pretty easily: