Skip to content

Commit b0e862f

Browse files
committed
Build out standings()
1 parent 0a291c6 commit b0e862f

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ most_recent_game = statsapi.get('schedule',{'teamId':133,'sportId':1,'hydration'
7474
print(statsapi.boxscore(most_recent_game))
7575
print(statsapi.linescore(most_recent_game))
7676
```
77+
78+
### Find the team with the longest name
79+
80+
Use `statsapi.get('teams')` to retrieve all active team names,
81+
then feed into max() to find the longest value and its length
82+
83+
```
84+
longest_team_name = max([x['name'] for x in statsapi.get('teams',{'sportIds':1,'activeStatus':'Yes','fields':'teams,name'})['teams']],key=len)
85+
print('The team with the longest name is %s, at %s characters.' % (longest_team_name, len(longest_team_name)))
86+
```

statsapi/__init__.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -658,20 +658,54 @@ def league_leaders(leaderCategories,leagueId=None,season=None):
658658

659659
return "This function is not yet available."
660660

661-
def standings(leagueId=None,season=None,standingsTypes=None):
662-
"""Get standings for a given league and season
661+
def standings(leagueId=None,season=None,standingsTypes=None,date=None):
662+
"""Get standings for a given league and season.
663+
664+
Format for date = 'MM/DD/YYYY', e.g. '04/24/2019'
663665
"""
664666
if not leagueId: leagueId = '103,104'
665-
if not season: season = datetime.now().year
667+
params = {'leagueId':leagueId}
668+
if date: params.update({'date':date})
669+
if not season:
670+
if date:
671+
season = date[-4:]
672+
else:
673+
season = datetime.now().year
666674
if not standingsTypes: standingsTypes = 'regularSeason'
667-
params = {'leagueId':leagueId,'season':season}
675+
params.update({'season':season,'standingsTypes':standingsTypes})
668676
if leagueId: params.update({'leagueId':leagueId})
677+
params.update({'hydrate':'team(division)','fields':'records,standingsType,teamRecords,team,name,division,id,nameShort,abbreviation,divisionRank,gamesBack,wildCardRank,wildCardGamesBack,wildCardEliminationNumber,divisionGamesBack,clinched,eliminationNumber,winningPercentage,type,wins,losses'})
669678

670679
r = get('standings',params)
671680

672-
673-
674-
return "This function is not yet available."
681+
standings = ''
682+
divisions = {}
683+
684+
for y in r['records']:
685+
for x in y['teamRecords']:
686+
if x['team']['division']['id'] not in divisions.keys():
687+
divisions.update({x['team']['division']['id']:{'div_name':x['team']['division']['name'],'teams':[]}})
688+
team = {
689+
'name' : x['team']['name'],
690+
'div_rank' : x['divisionRank'],
691+
'w' : x['wins'],
692+
'l' : x['losses'],
693+
'gb' : x['gamesBack'],
694+
'wc_rank' : x.get('wildCardRank','-'),
695+
'wc_gb' : x.get('wildCardGamesBack','-'),
696+
'wc_elim_num' : x.get('wildCardEliminationNumber','-'),
697+
'elim_num' : x['eliminationNumber']
698+
}
699+
divisions[x['team']['division']['id']]['teams'].append(team)
700+
701+
for div_id,div in divisions.items():
702+
standings += div['div_name'] + '\n'
703+
standings += '{:^4} {:<21} {:^3} {:^3} {:^4} {:^4} {:^7} {:^5} {:^4}\n'.format(*['Rank','Team','W','L','GB','(E#)','WC Rank','WC GB','(E#)'])
704+
for t in div['teams']:
705+
standings += '{div_rank:^4} {name:<21} {w:^3} {l:^3} {gb:^4} {elim_num:^4} {wc_rank:^7} {wc_gb:^5} {wc_elim_num:^4}\n'.format(**t)
706+
standings += '\n'
707+
708+
return standings
675709

676710
def roster(teamId,rosterType=None,season=None,date=None):
677711
"""Get the roster for a given team.

0 commit comments

Comments
 (0)