Skip to content

Commit

Permalink
Merge pull request #342 from cwendt94/player_info_multiple
Browse files Browse the repository at this point in the history
FB Allow Multiple Players for Info
  • Loading branch information
cwendt94 authored Jul 7, 2022
2 parents 7d84097 + 7a43b55 commit b57f504
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import time
import json
from typing import List, Tuple
from typing import List, Tuple, Union

from ..base_league import BaseLeague
from .team import Team
Expand Down Expand Up @@ -274,19 +274,24 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position

return [BoxPlayer(player, pro_schedule, positional_rankings, week, self.year) for player in players]

def player_info(self, name: str = None, playerId: int = None):
def player_info(self, name: str = None, playerId: Union[int, list] = None) -> Union[Player, List[Player]]:
''' Returns Player class if name found '''

if name:
playerId = self.player_map.get(name)
if playerId is None or isinstance(playerId, str):
return None
if not isinstance(playerId, list):
playerId = [playerId]

params = { 'view': 'kona_playercard' }
filters = {'players':{'filterIds':{'value':[playerId]}, 'filterStatsForTopScoringPeriodIds':{'value':17, "additionalValue":["00{}".format(self.year), "10{}".format(self.year)]}}}
filters = {'players':{'filterIds':{'value': playerId}, 'filterStatsForTopScoringPeriodIds':{'value':17, "additionalValue":["00{}".format(self.year), "10{}".format(self.year)]}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

data = self.espn_request.league_get(params=params, headers=headers)

if len(data['players']) > 0:
if len(data['players']) == 1:
return Player(data['players'][0], self.year)
if len(data['players']) > 1:
return [Player(player, self.year) for player in data['players']]

13 changes: 13 additions & 0 deletions tests/football/integration/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@ def test_box_scores(self):
self.assertEqual(repr(box_scores[1].away_team), 'Team(TEAM BERRY)')
self.assertEqual(repr(box_scores[1].away_lineup[1]), 'Player(Odell Beckham Jr., points:29.0, projected:16.72)')
self.assertEqual(repr(box_scores[1]), 'Box Score(Team(TEAM BERRY) at Team(TEAM HOLLAND))')

def test_player_info(self):
league = League(48153503, 2019)

# Single ID
player = league.player_info(playerId=3139477)
self.assertEqual(player.name, 'Patrick Mahomes')

# Two ID
players = league.player_info(playerId=[3139477, 3068267])
self.assertEqual(len(players), 2)
self.assertEqual(players[0].name, 'Patrick Mahomes')
self.assertEqual(players[1].name, 'Austin Ekeler')

0 comments on commit b57f504

Please sign in to comment.