-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmlb_api.py
2270 lines (1894 loc) · 73.4 KB
/
mlb_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import datetime
from typing import List, Union
from mlbstatsapi.models.people import Person, Player, Coach
from mlbstatsapi.models.teams import Team
from mlbstatsapi.models.sports import Sport
from mlbstatsapi.models.leagues import League
from mlbstatsapi.models.game import Game, Plays, Linescore, BoxScore
from mlbstatsapi.models.venues import Venue
from mlbstatsapi.models.divisions import Division
from mlbstatsapi.models.schedules import Schedule, ScheduleGames
from mlbstatsapi.models.attendances import Attendance
from mlbstatsapi.models.stats import Stat
from mlbstatsapi.models.seasons import Season
from mlbstatsapi.models.drafts import Round
from mlbstatsapi.models.awards import Award
from mlbstatsapi.models.gamepace import Gamepace
from mlbstatsapi.models.homerunderby import Homerunderby
from mlbstatsapi.models.standings import Standings
from .mlb_dataadapter import MlbDataAdapter
# from .exceptions import TheMlbStatsApiException
from . import mlb_module
class Mlb:
"""
A class used to retrive MLB Stats API objects
...
Attributes
----------
hostname: str
hostname of statsapi.mlb.com
ver: str
api version
logger: logging.Loger
logger
"""
def __init__(self, hostname: str = 'statsapi.mlb.com', logger: logging.Logger = None):
self._mlb_adapter_v1 = MlbDataAdapter(hostname, 'v1', logger)
self._mlb_adapter_v1_1 = MlbDataAdapter(hostname, 'v1.1', logger)
self._logger = logger or logging.getLogger(__name__)
self._logger.setLevel(logging.DEBUG)
def get_people(self, sport_id: int = 1, **params) -> List[Person]:
"""
return the all players for sportid
Parameters
----------
sport_id : int
Insert a sportId to return player information for a particular
sport.
Other Parameters
----------------
season : str
Insert year to return player information for a particular season.
gameType : str
Insert gameType to return player information for a particular
gameType.
Returns
-------
list
Returns a list of People
See Also
--------
Mlb.get_person : Return Person from id.
Mlb.get_people_id : Return person id from name.
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_people()
[Person, Person, Person]
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'sports/{sport_id}/players', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
people = []
if 'people' in mlb_data.data and mlb_data.data['people']:
people = [Person(**person) for person in mlb_data.data['people']]
return people
def get_person(self, player_id: int, **params) -> Union[Person, None]:
"""
This endpoint returns statistical data and biographical information
for a player,coach or umpire based on playerId.
Parameters
----------
person_id : int
Insert personId for a specific player, coach or umpire based on
playerId.
Returns
-------
Person
Returns a Person
See Also
--------
Mlb.get_people : Return a list of People from sport id.
Mlb.get_people_id : Return person id from name.
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_person(660271)
Person
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'people/{player_id}', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return None
if 'people' in mlb_data.data and mlb_data.data['people']:
for person in mlb_data.data['people']:
return Person(**person)
def get_persons(self, person_ids: Union[str, List[int]], **params) -> List[Person]:
"""
This endpoint returns statistical data and biographical information
for a players,umpires, and coaches based on playerId.
Parameters
----------
person_ids : str, List[int]
Insert personId(s) to return biographical information for a
specific player. Format '605151,592450' or [605151,592450]
Other Parameters
----------------
hydrate : str
Insert hydration(s) to return statistical or biographical data
for a specific player(s).
Format stats(group=["statGroup1","statGroup2"],
type=["statType1","statType2"]).
Available Hydrations:
hydrations
currentTeam
team
rosterEntries
relatives
transactions
social
education
draft
mixedFeed
articles
videos
xrefId
fields : str
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
Returns
-------
Person
Returns a Person
See Also
--------
Mlb.get_people : Return a list of People from sport id.
Mlb.get_people_id : Return person id from name.
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_person(660271)
Person
"""
params['personIds'] = person_ids
mlb_data = self._mlb_adapter_v1.get(endpoint=f'people', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
person_list = []
if 'people' in mlb_data.data and mlb_data.data['people']:
for person in mlb_data.data['people']:
person_list.append(Person(**person))
return person_list
def get_people_id(self, fullname: str, sport_id: int = 1,
search_key: str = 'fullname', **params) -> List[int]:
"""
Returns specific player information based on players fullname
Parameters
----------
fullname : str
Person full name
sport_id : int
Insert sportId to return player information for particular sport.
Other Parameters
----------------
season : int
Insert year to return player information for a particular season.
gameType : str
Insert gameType to return player information for a particular
gameType.
Returns
-------
list of int
Returns a list of person ids
See Also
--------
Mlb.get_people : Return a list of People from sport id.
Mlb.get_person : Return Person from id.
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_people_id("Ty France")
[664034]
"""
# Used to reduce the amount of unneccessary data requested from api
params['fields'] = 'people,id,fullName'
mlb_data = self._mlb_adapter_v1.get(endpoint=f'sports/{sport_id}/players', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
player_ids = []
if 'people' in mlb_data.data and mlb_data.data['people']:
for person in mlb_data.data['people']:
try:
if person[search_key].lower() == fullname.lower():
player_ids.append(person['id'])
except KeyError:
continue
return player_ids
def get_teams(self, sport_id: int = 1, **params) -> List[Team]:
"""
return the all Teams
Parameters
----------
sport_id : int
Insert sportId to return team information for a particular sportId
Other Parameters
----------------
season : str
Insert year to return team information for a particular season.
leagueIds : int
Insert leagueId to return team information for particular league.
activeStatus : str
Insert activeStatus to populate a teams based on active/inactive
status for a given season. There are three status types: Y, N, B
allStarStatuses : str
Insert allStarStatuses to populate a teams based on Allstar status
for a given season. There are two status types: Y and N
sportIds : str
Insert sportId to return team information for a particular sportId
Usage: '1' or '1,11,12'
gameType : str
Insert gameType to return team information for a particular
gameType. For a list of all gameTypes:
https://statsapi.mlb.com/api/v1/gameTypes
hydrate : str
Insert Hydration(s) to return data for any available team
hydration. Format "league,venue"
Available Hydrations:
previousSchedule
nextSchedule
venue
social
deviceProperties
game(promotions)
game(atBatPromotions)
game(tickets)
game(atBatTickets)
game(sponsorships)
league
person
sport
division
fields : str
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
Returns
-------
list of Teams
returns a list of teams
See Also
--------
Mlb.get_team : Return a Team from id
Mlb.get_team_id : Return a list of Teams from sport id.
Mlb.get_team_coaches : Return a list of Coaches from team id
Mlb.get_team_roster : Return a list of Players from team id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_teams()
[Team, Team, Team]
"""
params['sportId'] = sport_id
mlb_data = self._mlb_adapter_v1.get(endpoint=f'teams', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
teams = []
if 'teams' in mlb_data.data and mlb_data.data['teams']:
teams = [Team(**team) for team in mlb_data.data['teams']]
return teams
def get_team(self, team_id: int, **params) -> Union[Team, None]:
"""
Returns a team based on teamId.
Parameters
----------
team_id : int
Insert teamId to return a directory of team information for a
particular club.
Other Parameters
----------------
season : int
Insert year to return a directory of team information for a
particular club in a specific season.
sportId : int
Insert a sportId to return a directory of team information for a
particular club in a sport.
hydrate : str
Insert Hydration(s) to return data for any available team
hydration. Format "league,venue"
Available Hydrations:
previousSchedule
nextSchedule
venue
social
deviceProperties
game(promotions)
game(atBatPromotions)
game(tickets)
game(atBatTickets)
game(sponsorships)
league
person
sport
division
fields : str
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
Returns
-------
Team
returns a Team from team id
See Also
--------
Mlb.get_teams : Return a list of Teams from sport id.
Mlb.get_team_id : Return a list of team ids from name.
Mlb.get_team_coaches : Return a list of Coaches from team id
Mlb.get_team_roster : Return a list of Players from team id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_team(133)
Team
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'teams/{team_id}',
ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return None
if 'teams' in mlb_data.data and mlb_data.data['teams']:
for team in mlb_data.data['teams']:
return Team(**team)
def get_team_id(self, team_name: str,
search_key: str = 'name', **params) -> List[int]:
"""
return a team Id
Parameters
----------
team_name : str
Teams name
search_key : str
search key search json for matching team_name
Other Parameters
----------------
sportId : int
sport id number for team search
Returns
-------
list of ints
returns a list of matching team ids
See Also
--------
Mlb.get_teams : Return a list of Teams from sport id.
Mlb.get_team : Return a Team from id
Mlb.get_team_coaches : Return a list of Coaches from team id
Mlb.get_team_roster : Return a list of Players from team id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_team_id("Oakland Athletics")
[133]
"""
# Used to reduce the amount of unneccessary data requested from api
params['fields'] = 'teams,id,name'
mlb_data = self._mlb_adapter_v1.get(endpoint='teams', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
team_ids = []
if 'teams' in mlb_data.data and mlb_data.data['teams']:
for team in mlb_data.data['teams']:
try:
if team[search_key].lower() == team_name.lower():
team_ids.append(team['id'])
except (KeyError):
continue
return team_ids
def get_team_roster(self, team_id: int, **params) -> List[Player]:
"""
return the team player roster
Parameters
----------
team_id : int
teamId to return a directory of players based on roster status for
a particular club.
Other Parameters
----------------
rosterType : str
Insert teamId to return a directory of players based on roster
status for a particular club. rosterType's include 40Man,
fullSeason, fullRoster, nonRosterInvitees, active, allTime,
depthChart, gameday, and coach.
season : str
Insert year to return a directory of players based on roster
status for a particular club in a specific season.
date : str
Insert date to return a directory of players based on roster
status for a particular club on a specific date.
hydrate : str
Insert Hydration(s) to return data for any available team
hydration. The hydration for Teams contains "person" which has
subhydrations Format "person(subHydration1, subHydrations2)"
Available Hydrations:
"person"
Hydrations Available Through Person
hydrations
awards
currentTeam
team
rosterEntries
relatives
transactions
social
education
stats
draft
mixedFeed
articles
video
xrefId
fields : str
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
Returns
-------
list of players
See Also
--------
Mlb.get_teams : Return a list of Teams from sport id.
Mlb.get_team : Return a Team from id
Mlb.get_team_coaches : Return a list of Coaches from team id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_team_roster(133)
[Player, Player, Player]
>>> mlb.get_team_roster(133, rosterType=40Man, season=2018)
[Player, Player, Player]
>>> mlb.get_team_roster(133, date='06/05/2018')
[Player, Player, Player]
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'teams/{team_id}/roster', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
players = []
if 'roster' in mlb_data.data and mlb_data.data['roster']:
for player in mlb_data.data['roster']:
players.append(Player(**mlb_module.merge_keys(player, ['person'])))
return players
def get_team_coaches(self, team_id: int, **params) -> List[Coach]:
"""
Return a directory of coaches for a particular team.
Parameters
----------
team_id : int
Insert teamId to return a directory of coaches for a given team.
Other Parameters
----------------
season : str
Insert year to return a directory of players based on roster status for a particular club in a specific season.
date : str
Insert date to return a directory of players based on roster status for a particular club on a specific date.
fields : str
Comma delimited list of specific fields to be returned. Format: topLevelNode, childNode, attribute
Returns
-------
list of Coaches
returns a list of Coaches
See Also
--------
Mlb.get_teams : Return a list of Teams from sport id.
Mlb.get_team : Return a Team from id
Mlb.get_team_roster : Return a list of Players from team id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_team_coaches(133)
[Coach, Coach, Coach]
>>> mlb.get_team_coaches(133, season='2018')
[Coach, Coach, Coach]
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'teams/{team_id}/coaches', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
coaches = []
if 'roster' in mlb_data.data and mlb_data.data['roster']:
for coach in mlb_data.data['roster']:
coaches.append(Coach(**mlb_module.merge_keys(coach, ['person'])))
return coaches
def get_schedule(self,
date: str = None,
start_date: str = None,
end_date: str = None,
sport_id: int = 1,
team_id: int = None, **params) -> Union[Schedule, None]:
"""
return the schedule created from the included params.
Calling get_schedule without startDate or endDate results in a schedule returned
for todays date. Calling with startDate and endDate as the same date returns a
schedule for just that desired date. Different results in the schedule for multiple
days.
Parameters
----------
date : str
Date
start_date : str "yyyy-mm-dd"
Start date
end_date : str "yyyy-mm-dd"
End date
sport_id : int
sport id of schedule defaults to 1
team_id : int
get schedule for team with team_id
Other Parameters
----------------
leagueId : int,str
Insert leagueId to return all schedules based on a particular
scheduleType for a specific league. Usage: 1 or '1,11
gamePks : int,str
Insert gamePks to return all schedules based on a particular
scheduleType for specific games. Usage: 531493 or '531493,531497'
venueIds : int
Insert venueId to return all schedules based on a particular
scheduleType for a specific venueId.
gameTypes : str
Insert gameTypes to return schedule information for all games in
particular gameTypes. For a list of all gameTypes:
https://statsapi.mlb.com/api/v1/gameTypes
scheduleType : str
Insert one or mutliple of the three available scheduleTypes to
return data for a particular schedule. Format "games,events,xref"
eventTypes : str
Insert one or mutliple of the three available eventTypes to
return data for a particular schedule. Format "primary,secondary"
There are two different schedule eventTypes:
primary- returns calendar/schedule pages.
secondary returns ticket pages.
hydrate : str
Insert Hydration(s) to return data for any available schedule
hydration. The hydrations for schedule contain "venue" and "team"
which have subhydrations.
Format "team(subHydration1, subHydrations2)"
Available Hydrations:
tickets
game(content)
game(content(all))
game(content(media(all)))
game(content(editorial(all)))
game(content(highlights(all)))
game(content(editorial(preview)))
game(content(editorial(recap)))
game(content(editorial(articles)))
game(content(editorial(wrap)))
game(content(media(epg)))
game(content(media(milestones)))
game(content(highlights(scoreboard)))
game(content(highlights(scoreboardPreview)))
game(content(highlights(highlights)))
game(content(highlights(gamecenter)))
game(content(highlights(milestone)))
game(content(highlights(live)))
game(content(media(featured)))
game(content(summary))
game(content(gamenotes))
game(tickets)
game(atBatTickets)
game(promotions)
game(atBatPromotions)
game(sponsorships)
lineup
linescore
linescore(matchup)
linescore(runners)
linescore(defense)
decisions
scoringplays
broadcasts
broadcasts(all)
radioBroadcasts
metadata
game(seriesSummary)
seriesStatus
event(performers)
event(promotions)
event(timezone)
event(tickets)
event(venue)
event(designations)
event(game)
event(status)
weather
officials
probablePitcher
venue
relatedVenues
parentVenues
residentVenues
relatedVenues(venue)
parentVenues(venue)
residentVenues(venue)
location
social
relatedApplications
timezone
menu
metadata
performers
images
schedule
nextSchedule
previousSchedule
ticketManagement
xrefId
team
previousSchedule
nextSchedule
venue
springVenue
social
deviceProperties
game(promotions)
game(promotions)
game(atBatPromotions)
game(tickets)
game(atBatTickets)
game(sponsorships)
league
videos
person
sport
standings
division
xref
Returns
-------
Schedule
returns the Schedule for the dates
See Also
--------
Mlb.get_scheduled_games_by_date : return a list of scheduledgames
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_schedule(start_date="2021-08-01", end_date="2021-08-11")
Schedule
"""
if start_date and end_date:
params['startDate'] = start_date
params['endDate'] = end_date
elif date and not (start_date or end_date):
params['date'] = date
else:
return None
if team_id:
params['teamId'] = team_id
params['sportId'] = sport_id
mlb_data = self._mlb_adapter_v1.get(endpoint='schedule', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return None
# if mlb_data is not empty, and 'dates' key is in mlb_data.data and mlb_data.data['dates]
# can sometimes be an empty list when there are no scheduled game for the date(s).
# Only check for existance 'dates' key for this reason.
if 'dates' in mlb_data.data and mlb_data.data['dates']:
return Schedule(**mlb_data.data)
def get_scheduled_games_by_date(self, date: str = None,
start_date: str = None,
end_date: str = None,
sport_id: int = 1,
**params) -> List[ScheduleGames]:
"""
return game ids for a specific date and game status
Parameters
----------
date : str
start date, 'yyyy-mm-dd'
start_date : str "yyyy-mm-dd"
Start date
end_date : str
end date, 'yyyy-mm-dd'
spord_id : int
spord id of schedule defaults to 1
Other Parameters
----------------
leagueId : int,str
Insert leagueId to return all schedules based on a particular
scheduleType for a specific league. Usage: 1 or '1,11
gamePks : int,str
Insert gamePks to return all schedules based on a particular
scheduleType for specific games. Usage: 531493 or '531493,531497'
venueIds : int
Insert venueId to return all schedules based on a particular
scheduleType for a specific venueId.
gameTypes : str
Insert gameTypes to return schedule information for all games in
particular gameTypes. For a list of all gameTypes:
https://statsapi.mlb.com/api/v1/gameTypes
Returns
-------
list of ScheduleGames
returns a list of matching game ids
See Also
--------
Mlb.get_game_play_by_play : return play by play data for a game
Mlb.get_game_line_score : return a linescore for a game
Mlb.get_todays_game_ids : return a list of game ids for today
Mlb.get_game : return a specific game from game id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_game_ids()
"""
if start_date and end_date:
params['startDate'] = start_date
params['endDate'] = end_date
elif date and not (start_date or end_date):
params['date'] = date
else:
return None
params['sportId'] = sport_id
games = []
mlb_data = self._mlb_adapter_v1.get(endpoint='schedule', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return []
if 'dates' in mlb_data.data and mlb_data.data['dates']:
for date in mlb_data.data['dates']:
for game in date['games']:
games.append(ScheduleGames(**game))
return games
def get_game(self, game_id: int, **params) -> Union[Game, None]:
"""
Return the game for a specific game id
Gumbo Live Feed for a specific gamePk.
Parameters
----------
game_id : int
Insert gamePk to return the GUMBO live feed for a specific game.
Other Parameters
----------------
timecode : str
Use this parameter to return a snapshot of the data at the
specified time. Format: YYYYMMDD_HHMMSS.
Return timecodes from timecodes endpoint
https://statsapi.mlb.com/api/v1.1/game/534196/feed/live/timestamps
hydrate : str
Insert hydration(s) to return putout credits or defensive
positioning data for all plays in a particular game.
Format 'credits,alignment,flags'
Available Hydrations:
credits
alignment
flags
officials
fields : str
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
!!! To use this we would need to make almost every !!!
!!! data class attr optonal? Do we want this? !!!
Returns
-------
Game
See Also
--------
Mlb.get_game_play_by_play : return play by play data for a game
Mlb.get_game_line_score : return a linescore for a game
Mlb.get_game_box_score : return a boxscore for a game
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_game(662242)
Game
"""
mlb_data = self._mlb_adapter_v1_1.get(endpoint=f'game/{game_id}/feed/live', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return None
if 'gamepk' in mlb_data.data and mlb_data.data['gamepk'] == game_id:
return Game(**mlb_data.data)
def get_game_play_by_play(self, game_id: int, **params) -> Union[Plays, None]:
"""
return the playbyplay of a game for a specific game id
Parameters
----------
game_id : int
Game id number
Other Parameters
----------------
timecode : int
Use this parameter to return a snapshot of the data at the
specified time. Format: YYYYMMDD_HHMMSS
fields :
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
!!! To use this we would need to make almost every !!!
!!! data class attr optonal? Do we want this? !!!
Returns
-------
Plays
See Also
--------
Mlb.get_game_line_score : return a linescore for a game
Mlb.get_game_box_score : return a boxscore for a game
Mlb.get_game : return a specific game from game id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_game_play_by_play(662242)
Plays
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'game/{game_id}/playByPlay', ep_params=params)
if 400 <= mlb_data.status_code <= 499:
return None
if 'allplays' in mlb_data.data and mlb_data.data['allplays']:
return Plays(**mlb_data.data)
def get_game_line_score(self, game_id: int, **params) -> Union[Linescore, None]:
"""
return the Linescore of a game for a specific game id
Parameters
----------
game_id : int
Game id number
Other Parameters
----------------
timecode : int
Use this parameter to return a snapshot of the data at the
specified time. Format: YYYYMMDD_HHMMSS
fields :
Comma delimited list of specific fields to be returned.
Format: topLevelNode, childNode, attribute
!!! To use this we would need to make almost every !!!
!!! data class attr optonal? Do we want this? !!!
Returns
-------
Linescore
See Also
--------
Mlb.get_game_play_by_play : return play by play data for a game
Mlb.get_game_box_score : return a boxscore for a game
Mlb.get_game : return a specific game from game id
Examples
--------
>>> mlb = Mlb()
>>> mlb.get_game_line_scrore(662242)
Linescore
"""
mlb_data = self._mlb_adapter_v1.get(endpoint=f'game/{game_id}/linescore', ep_params=params)
if 'teams' in mlb_data.data and mlb_data.data['teams']:
return Linescore(**mlb_data.data)
def get_game_box_score(self, game_id: int, **params) -> Union[BoxScore, None]:
"""