cwendt94 / espn-api

ESPN Fantasy API! (Football, Basketball)
MIT License
544 stars 183 forks source link

Initial Baseball Implementation #195

Open cwendt94 opened 3 years ago

ProgrammerMatt commented 3 years ago

Looking to possibly use this!

tomrulz commented 3 years ago

league = League(league_id=333670215, year=2021, debug=True) Traceback (most recent call last): File "", line 1, in File "C:\Users\Tom\Anaconda3\lib\site-packages\espn_api\baseball\league.py", line 22, in init self._fetch_teams(data) File "C:\Users\Tom\Anaconda3\lib\site-packages\espn_api\baseball\league.py", line 31, in _fetch_teams super()._fetch_teams(data, TeamClass=Team) File "C:\Users\Tom\Anaconda3\lib\site-packages\espn_api\base_league.py", line 65, in _fetch_teams self.teams.append(TeamClass(team, roster=roster, member=member, schedule=schedule, year=seasonId)) File "C:\Users\Tom\Anaconda3\lib\site-packages\espn_api\baseball\team.py", line 26, in init self.stats = {STATS_MAP[i]: j for i, j in data['valuesByStat'].items()} File "C:\Users\Tom\Anaconda3\lib\site-packages\espn_api\baseball\team.py", line 26, in self.stats = {STATS_MAP[i]: j for i, j in data['valuesByStat'].items()} KeyError: '0'

tomrulz commented 3 years ago

Hello. When trying to initialize the league I get a keyerror 0. I searched through some previous issues, didn't see anything similar. Any advice? Thank you in advance.

I have tried to use the API with your test league from the implementation ticket and I got it to work! But when I try my league info I the KeyError.

cwendt94 commented 3 years ago

@tomrulz thanks for reporting this! I found the issue, the team object was trying to map stats which has not been added yet for baseball.

Package version v0.14.1 has the fix!

tomrulz commented 3 years ago

you the man!

LastBastion1005 commented 2 years ago

Is there still going to be baseball implementation eventually? Very interested!

cwendt94 commented 2 years ago

There is already some basic baseball features implemented that you can use for one of your old leagues. The wiki still needs to be updated but you can check out the available functions in the code here. Initialization is like the other leagues

from espn_api.baseball import League
league = League(league_id: int, year: int, espn_s2: str = None, swid: str = None, username: str = None, password: str = None, debug=False)

Some of the available features are recent_activity , free_agents, box_scores, and able to access teams and rosters by the league object league.teams[0].roster

ChandlerCGray commented 3 months ago

Hey @cwendt94 . I've really been enjoying playing around with the baseball side!

However, I did find a strange bug in league.free_agents in that position will oftentimes be wrong, especially for pitchers.

from espn_api.baseball import League

league = League(league_id=1234, year=2024, espn_s2='<redacted>', swid='<redacted>')

# Gather free agent pitchers
free_agents = league.free_agents(position_id=14, size=1)
print("\nFree Agents")
for free_agent in free_agents:
    print(f"Name: {free_agent.name}, Eligible Positions: {free_agent.eligibleSlots}, Position: {free_agent.position}, Team: {free_agent.proTeam}, PlayerId {free_agent.playerId}")
    print(f"Name: {free_agent.name}, Eligible Positions: {free_agent.eligibleSlots}, Position: {free_agent.eligibleSlots[0]}, Team: {free_agent.proTeam}, PlayerId {free_agent.playerId}")

Output:

Free Agents
Name: Nestor Cortes, Eligible Positions: ['P', 'SP', 'BE', 'IL'], Position: C, Team: NYY, PlayerId 36480
Name: Nestor Cortes, Eligible Positions: ['P', 'SP', 'BE', 'IL'], Position: P, Team: NYY, PlayerId 36480

As you can see, I was able to work around this using {free_agent.eligibleSlots[0]}.

Side note: Any chance we will get the docs soon? I'd love to dive a little deeper.

cwendt94 commented 2 months ago

I just looked into it and it seems like for pitchers ESPN sets their default position id to 1 in the data they are returning, which in the position maps to 1B https://github.com/cwendt94/espn-api/blob/master/espn_api/baseball/constant.py#L3. Thats not a eligible position. Not sure exactly why they are returning that.