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

In the following articles, I will guide how to retrieve information from fantasy sports API.

Useful information.

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

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐