Skip to main content

Command Palette

Search for a command to run...

Using Yahoo Fantasy Sports API

Updated
3 min read
Using Yahoo Fantasy Sports API

As a fan of basketball and the NBA, I joined some NBA fantasy leagues on yahoo sport, which helped me understand better basketball, also NBA leagues, teams, and players.

The yahoo fantasy Interface for the user is excellent. But as an administration role, maybe you want to access, retrieve and arrange the data to help your league's members have supportive information.

The Fantasy Sports APIs provide URIs used to access fantasy sports data. The APIs are based on a RESTful model. Therefore, resources like games, leagues, teams, players, etc., form the building blocks for these APIs.

First, you need to install the yahoo fantasy sports API

pip install yahoo-fantasy-api

Registering Your Application

To work with OAuth and Yahoo! services, you must register your application with the Yahoo! Developer Network. You should create a new one at here.

Screen Shot 2022-06-27 at 2.30.06 PM.png

After click to create button, you can obtained your application information, include App ID, Client ID and Client Secret. Screen Shot 2022-06-27 at 2.30.40 PM.png

import yahoo_fantasy_api as yfa
from yahoo_oauth import OAuth2
import json

creds = {'consumer_key': 'YOUR CLIENT ID',\
         'consumer_secret': 'YOUR CLIENT SECRET'}
with open('oauth2.json', "w") as f:
    f.write(json.dumps(creds))
oauth = OAuth2(None, None, from_file='oauth2.json')
if not oauth.token_is_valid():
    oauth.refresh_access_token()

A new yahoo login tab at your browser open, you need to login and yahoo will give a code like below

Screen Shot 2022-06-27 at 2.11.27 PM.png

If your code is rightful access, you will receive a message

[2022-06-27 15:33:15,633 DEBUG] [yahoo_oauth.oauth.token_is_valid] ELAPSED TIME : 0.7043361663818359
[2022-06-27 15:33:15,634 DEBUG] [yahoo_oauth.oauth.token_is_valid] TOKEN IS STILL VALID

The yahoo_fantasy_api is structured with a class hierarchy, which offers fantasy information at these levels: At the top level is the Game class. It is individual leagues you are part of during the time you have joined The next is the League class. It represents a particular season and game code that you played in. The league is found by its unique league ID. The final one is Team class. Within a league there are individual teams.

Finally at the lowest level are individual players (i.e Steph Curry, Lebron James.,etc).

To access the Yahoo! league IDs that the current user played in, use league_ids method with a game instance

gm = yfa.game.Game(oauth, 'nba')
gm.league_ids(year=2020)

The result will return:

['402.l.20641', '402.l.28035']

To construct a League object from a Game, using to_league

lg = gm.to_league('402.l.20641')

At here, I want to create the league stats for any week, so I use nested_lookup moodule to find necessary information.

from nested_lookup import nested_lookup
matchup = lg.matchups(week=1)
stat_name = ["Team","FG%","FT%","3PTM","PTS","REB","AST","ST","BLK","TO","Score"]
league_stat = pd.DataFrame(columns=stat_name)


#Obtain the league stats
name = nested_lookup('name', matchup)
team_stats = nested_lookup('team_stats', matchup)
team_points = nested_lookup('team_points', matchup)


no_team = len(lg.teams().keys())
for i in range (no_team):
    stat = nested_lookup('value', team_stats[i])
    point = nested_lookup('total', team_points[i])

    stat = list(map(str,stat))
    stat = ['0' if x=='-' else x for x in stat]
    stat = list(map(float,[ x for x in stat if '/' not in x ]))
    point = list(map(float,point))
    full_stat = [name[i+1]] + stat + point
    league_stat.loc[len(league_stat)] = full_stat

league_stat

And the result will return

Screen Shot 2022-10-19 at 3.46.34 PM.png

Next time, I will guide you how to explore the data from the league stats. Thank you for reading.

Useful information.

  1. Fantasy Sports API page
  2. Fantasy Sports API documentation