nesticot commited on
Commit
41ef760
·
verified ·
1 Parent(s): 6df4f65

Update api_scraper.py

Browse files
Files changed (1) hide show
  1. api_scraper.py +39 -32
api_scraper.py CHANGED
@@ -874,42 +874,49 @@ class MLB_Scrape:
874
  game_type_str = ','.join([str(x) for x in game_type])
875
 
876
  if game_type_str == 'S':
877
- player_data_1 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{1}/players?season={season}&gameType=[{game_type_str}]').json()['people']
878
- player_data_2 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{11}/players?season={season}&gameType=[{game_type_str}]').json()['people']
879
- player_data_3 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{12}/players?season={season}&gameType=[{game_type_str}]').json()['people']
880
- player_data_4 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{13}/players?season={season}&gameType=[{game_type_str}]').json()['people']
881
- player_data_5 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{14}/players?season={season}&gameType=[{game_type_str}]').json() ['people']
882
- player_data = player_data_1 + player_data_2 + player_data_3 + player_data_4 + player_data_5
883
-
 
 
 
 
 
 
 
884
 
885
  else:
886
  player_data = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{sport_id}/players?season={season}&gameType=[{game_type_str}]').json()['people']
887
 
888
- #Select relevant data that will help distinguish players from one another
889
-
890
- fullName_list = [x['fullName'] if 'fullName' in x else None for x in player_data]
891
- firstName_list = [x['firstName'] if 'firstName' in x else None for x in player_data]
892
- lastName_list = [x['lastName'] if 'lastName' in x else None for x in player_data]
893
- id_list = [x['id'] if 'id' in x else None for x in player_data]
894
- position_list = [x['primaryPosition']['abbreviation'] if 'primaryPosition' in x and 'abbreviation' in x['primaryPosition'] else None for x in player_data]
895
- team_list = [x['currentTeam']['id'] if 'currentTeam' in x and 'id' in x['currentTeam'] else None for x in player_data]
896
- weight_list = [x['weight'] if 'weight' in x else None for x in player_data]
897
- height_list = [x['height'] if 'height' in x else None for x in player_data]
898
- age_list = [x['currentAge'] if 'currentAge' in x else None for x in player_data]
899
- birthDate_list = [x['birthDate'] if 'birthDate' in x else None for x in player_data]
900
-
901
-
902
-
903
- df = pl.DataFrame(data={'player_id':id_list,
904
- 'first_name':firstName_list,
905
- 'last_name':lastName_list,
906
- 'name':fullName_list,
907
- 'position':position_list,
908
- 'team':team_list,
909
- 'weight':weight_list,
910
- 'height':height_list,
911
- 'age':age_list,
912
- 'birthDate':birthDate_list})
913
 
914
  return df
915
 
 
874
  game_type_str = ','.join([str(x) for x in game_type])
875
 
876
  if game_type_str == 'S':
877
+ player_data = requests.get(f'https://bdfed.stitch.mlbinfra.com/bdfed/stats/player?&env=prod&season={season}&sportId=1&stats=season&group=pitching&gameType=S&limit=1000000&offset=0&sortStat=inningsPitched&order=asc').json()
878
+ fullName_list = [x['playerFullName'] if 'playerFullName' in x else None for x in player_data['stats']]
879
+ firstName_list = [x['playerFirstName'] if 'playerFirstName' in x else None for x in player_data['stats']]
880
+ lastName_list = [x['playerLastName'] if 'playerLastName' in x else None for x in player_data['stats']]
881
+ id_list = [x['playerId'] if 'playerId' in x else None for x in player_data['stats']]
882
+ position_list = [x['primaryPositionAbbrev'] if 'primaryPositionAbbrev' in x else None for x in player_data['stats']]
883
+ team_list = [x['teamId'] if 'teamId' in x else None for x in player_data['stats']]
884
+
885
+ df = pl.DataFrame(data={'player_id':id_list,
886
+ 'first_name':firstName_list,
887
+ 'last_name':lastName_list,
888
+ 'name':fullName_list,
889
+ 'position':position_list,
890
+ 'team':team_list})
891
 
892
  else:
893
  player_data = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{sport_id}/players?season={season}&gameType=[{game_type_str}]').json()['people']
894
 
895
+ #Select relevant data that will help distinguish players from one another
896
+
897
+ fullName_list = [x['fullName'] if 'fullName' in x else None for x in player_data]
898
+ firstName_list = [x['firstName'] if 'firstName' in x else None for x in player_data]
899
+ lastName_list = [x['lastName'] if 'lastName' in x else None for x in player_data]
900
+ id_list = [x['id'] if 'id' in x else None for x in player_data]
901
+ position_list = [x['primaryPosition']['abbreviation'] if 'primaryPosition' in x and 'abbreviation' in x['primaryPosition'] else None for x in player_data]
902
+ team_list = [x['currentTeam']['id'] if 'currentTeam' in x and 'id' in x['currentTeam'] else None for x in player_data]
903
+ weight_list = [x['weight'] if 'weight' in x else None for x in player_data]
904
+ height_list = [x['height'] if 'height' in x else None for x in player_data]
905
+ age_list = [x['currentAge'] if 'currentAge' in x else None for x in player_data]
906
+ birthDate_list = [x['birthDate'] if 'birthDate' in x else None for x in player_data]
907
+
908
+
909
+
910
+ df = pl.DataFrame(data={'player_id':id_list,
911
+ 'first_name':firstName_list,
912
+ 'last_name':lastName_list,
913
+ 'name':fullName_list,
914
+ 'position':position_list,
915
+ 'team':team_list,
916
+ 'weight':weight_list,
917
+ 'height':height_list,
918
+ 'age':age_list,
919
+ 'birthDate':birthDate_list})
920
 
921
  return df
922