Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Created May 14, 2025 15:40
Show Gist options
  • Save jimbrig/77be0434855200bbe33f5afb685b8487 to your computer and use it in GitHub Desktop.
Save jimbrig/77be0434855200bbe33f5afb685b8487 to your computer and use it in GitHub Desktop.
Yahoo Fantasy Sports API - Python Scripts
YAHOO_CLIENT_ID=""
YAHOO_CLIENT_SECRET=""
import yahoo_fantasy_api as yfa
from yahoo_oauth import OAuth2
from dotenv import load_dotenv
import json
import os
# Load environment variables from .env file
load_dotenv()
# # get environment variables with credentials
client_id = os.getenv('YAHOO_CLIENT_ID')
client_secret = os.getenv('YAHOO_CLIENT_SECRET')
redirect_uri = os.getenv('YAHOO_REDIRECT_URI')
# Create credentials file
creds = {
'consumer_key': client_id,
'consumer_secret': client_secret
}
with open('oauth2.json', "w") as f:
f.write(json.dumps(creds))
# Initialize OAuth
# oauth=OAuth2(consumer_key=client_id, consumer_secret=client_secret)
oauth = OAuth2(None, None, from_file='oauth2.json')
# Refresh token if needed
if not oauth.token_is_valid():
oauth.refresh_access_token()
# Access your game (for baseball)
gm = yfa.game.Game(oauth, 'mlb')
print('Game ID:', gm.game_id)
# Game ID: <bound method Game.game_id of <yahoo_fantasy_api.game.Game object at 0x00000220E5FF6120>>
# Get your league IDs (adjust year as needed for 2025)
league_ids = gm.league_ids(year=2025)
lg = gm.to_league(league_ids[0]) # Use the first league ID
print('League ID:', lg.league_id)
# League ID: 458.l.56606
# Now you can access league data
teams = lg.teams()
print('Teams:', teams)
standings = lg.standings()
print('Standings:', standings)
draft_results = lg.draft_results()
print('Draft Results:', draft_results)
# collect data for export
data = {
'league_id': lg.league_id,
'teams': json.dumps(teams),
'draft_results': json.dumps(draft_results),
}
# save all data to json file
json_file = 'extracted_data.json'
# save all data to json file
def save_to_json(data, filename):
import json
with open(filename, 'w') as f:
json.dump(data, f, indent=4)
print(f"Data saved to {filename}")
# save_to_json(data, json_file)
save_to_json(draft_results, 'draft_results.json')
[project]
name = "yahoo-fdbr-ai"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = []
yahoo-fantasy-api
yahoo_oauth
yfpy
dotenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment