toddrob99 / MLB-StatsAPI

Python wrapper for MLB Stats API
GNU General Public License v3.0
539 stars 100 forks source link

[QUESTION] Player not found in lookup_player when they hit the IL #107

Closed kfinneman closed 1 year ago

kfinneman commented 1 year ago

Is there a reason a player cannot be found when they hit the IL? building a daily script to group by monthly and I am losing players when they hit the IL when they still have that boxscore data and a person_id there but can't be found in the lookup_player endpoint

toddrob99 commented 1 year ago

The statsapi.lookup_player() method uses the sports_players endpoint, which only seems to include active rosters. The only endpoint I've been able to find Bryce Harper (currently on IL) is team_roster with rosterType=allTime (statsapi.get("team_roster", {"teamId": 143, "rosterType": "allTime"})).

I could potentially update statsapi.lookup_player() to pull data from all the teams' allTime rosters, but there would be a significant speed reduction compared to the current method.

In any case, if you already have the personId, there's no reason to use statsapi.lookup_player(). Instead use the person endpoint (statsapi.get("person", {"personId": personId})).

justintran12 commented 1 year ago

Is there a way to get a player's personId from their name? I think the lookup_player is the only way to do that correct? But I think am running into the same issue as OP when I try running lookup_player() for any player name, I get the following error:

File "C:...\statsapi\init.py", line 1227, in lookup_player if str(lookup_value).lower() in str(v).lower(): UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)

toddrob99 commented 1 year ago

Is there a way to get a player's personId from their name? I think the lookup_player is the only way to do that correct? But I think am running into the same issue as OP when I try running lookup_player() for any player name, I get the following error:

File "C:...\statsapi**init**.py", line 1227, in lookup_player if str(lookup_value).lower() in str(v).lower(): UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)

I think this is a different error. What code are you running that throws the error? @justintran12

justintran12 commented 1 year ago

Is there a way to get a player's personId from their name? I think the lookup_player is the only way to do that correct? But I think am running into the same issue as OP when I try running lookup_player() for any player name, I get the following error: File "C:...\statsapiinit.py", line 1227, in lookup_player if str(lookup_value).lower() in str(v).lower(): UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)

I think this is a different error. What code are you running that throws the error? @justintran12

I get the error when I run statsapi.lookup_player('Harper') for example, I get the error for every name too. Regarding my other question though, I am able to find player stats using a name with this example in your documentation: statsapi.player_stats(next(x['id'] for x in statsapi.get('sports_players',{'season':2022,'gameType':'W'})['people'] if x['fullName']== player_name), 'hitting', 'career')

toddrob99 commented 1 year ago

I'm not sure why you're getting an error when you run lookup_player, because it works fine for me. It seems like whatever app you are running the code in has a unicode encoding issue, but it doesn't make sense because the u'\xe9' character it's complaining about is é, which does not appear in the result for the Harper lookup.

> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsapi
>>> statsapi.lookup_player('Harper')
[{'id': 547180, 'fullName': 'Bryce Harper', 'firstName': 'Bryce', 'lastName': 'Harper', 'primaryNumber': '3', 'currentTeam': {'id': 143}, 'primaryPosition': {'code': '10', 'abbreviation': 'DH'}, 'useName': 'Bryce', 'boxscoreName': 'Harper', 'nickName': 'Harp', 'mlbDebutDate': '2012-04-28', 'nameFirstLast': 'Bryce Harper', 'firstLastName': 'Bryce Harper', 'lastFirstName': 'Harper, Bryce', 'lastInitName': 'Harper, B', 'initLastName': 'B Harper', 'fullFMLName': 'Bryce Aron Max Harper', 'fullLFMName': 'Harper, Bryce Aron Max'}]
>>>
justintran12 commented 1 year ago

I'm not sure why you're getting an error when you run lookup_player, because it works fine for me. It seems like whatever app you are running the code in has a unicode encoding issue, but it doesn't make sense because the u'\xe9' character it's complaining about is é, which does not appear in the result for the Harper lookup.

> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsapi
>>> statsapi.lookup_player('Harper')
[{'id': 547180, 'fullName': 'Bryce Harper', 'firstName': 'Bryce', 'lastName': 'Harper', 'primaryNumber': '3', 'currentTeam': {'id': 143}, 'primaryPosition': {'code': '10', 'abbreviation': 'DH'}, 'useName': 'Bryce', 'boxscoreName': 'Harper', 'nickName': 'Harp', 'mlbDebutDate': '2012-04-28', 'nameFirstLast': 'Bryce Harper', 'firstLastName': 'Bryce Harper', 'lastFirstName': 'Harper, Bryce', 'lastInitName': 'Harper, B', 'initLastName': 'B Harper', 'fullFMLName': 'Bryce Aron Max Harper', 'fullLFMName': 'Harper, Bryce Aron Max'}]
>>>

I just tested it again today and it works for me now... Both times I tested it, I did not use any other frameworks or apps. Perhaps it is an issue on MLB's API side? I noticed the structure of the data coming from MLB's API sometimes changes as the season goes on.

toddrob99 commented 1 year ago

Closing for now