Skip to main content

An SDK for the Yahoo! Fantasy Sports API

Project description

Yahoo Fantasy API Wrapper

The Yahoo Fantasy Sports API is difficult to comprehend, has this strange one-page documentation setup that is hard to navigate, and seems to only want to conform to a small portion of the OAuth spec. This library/SDK makes your life easier if you want to write an app that interfaces with the Yahoo Fantasy Sports API.

This library will work for any Yahoo Fantasy Sports API leagues/teams. It contains some common constructs and helper methods for head-to-head MLB leagues at this time. More sports and league types are planned for the future.

Installation

pip install yahoofantasy

You will also need a application registered on the Yahoo Developer Site. You'll need your client ID and secret. The app just needs to have read permissions. See below for instructions on how to set up your Yahoo Developer application if you don't have one already.

Basic Usage

You're going to want to start off by logging in to your Yahoo Developer application, then creating a context. This context is where all of your API requests will originate and league information will live.

$ yahoofantasy login

Once you've logged in, create a context and use that to make requests. For example, to fetch all of your leagues for a given game/season:

from yahoofantasy import Context

ctx = Context()
leagues = ctx.get_leagues('mlb', 2020)
for league in leagues:
    print(league.name + " -- " + league.league_type)

Retreiving Access and Refresh Tokens

You can use the built-in yahoofantasy CLI to obtain an access token and refresh token for your application. Follow these steps:

  1. Set up your Yahoo application to have a callback/redirect URI of https://localhost:8000. If you already have an app that points to your local host on a different port or different path that's ok, you can customize later on.
  2. Install yahoofantasy if you haven't already
$ pip install yahoofantasy
  1. Log in with your Yahoo account. This command will launch a browser that will ask you to authenticate to your app. It will then store the token in a local file that can be consumed by the yahoofantasy SDK.
$ yahoofantasy login

NOTE: If you see a browser certificate warning that is ok, proceed anyway past the warning to save your token. This warning happens because Yahoo requires an HTTPS redirect URI and the local server uses an untrusted certificate.

Try yahoofantasy login --help for some advanced options, like customizing the port or redirect URI

Concepts

There is a general hierarchy that head-to-head leagues will follow. This hierarchy is represented with classes within this library. This code walkthrough will help you understand the organization of the library. The following examples are intended to be read sequentially and assume you have a Context with your logged in Yahoo credentials called ctx.

  • Your account will belong to one or more League objects.
for league in ctx.get_leagues('mlb', 2019):
    print(f"{league.id} - {league.name} ({league.league_type})")
  • A League will contain multiple Team objects.
from yahoofantasy import League

league = League(ctx, '388.l.25000')  # Use a manual league ID or get it from league.id above
for team in league.teams():
    print(f"Team Name: {team.name}\tManager: {team.manager.nickname}")
  • A Team has multiple Player objects that define their lineup. This is their current lineup and not a lineup for a given week
for team in league.standings():
		players = team.players()
		for player in players:
				print(f"Player: {player.name.full}")
  • A League has Standings, which is an ordered list of Team objects.
for team in league.standings():
    outcomes = team.team_standings.outcome_totals
    print(f"#{team.team_standings.rank}\t{team.name}\t"
          f"({outcomes.wins}-{outcomes.losses}-{outcomes.ties})")
  • A League will contain multiple Week objects. A Week contains multiple Matchup objects, which are a head-to-head matchup of two Team objects for that week.
week_3 = league.weeks()[2]
for matchup in week_3.matchups:
    print(f"{matchup.team1.name} vs {matchup.team2.name}")
  • A Matchup will have multiple Stat objects for the two teams. A Stat object contains the display name of the stat as well as the value for the team.
matchup = week_3.matchups[0]
print(f"{matchup.team1.name}\tvs\t{matchup.team2.name}")
for team1_stat, team2_stat in zip(matchup.team1_stats, matchup.team2_stats):
    print(f"{team1_stat.value}\t{team1_stat.display}\t{team2_stat.value}")

The full sequence of these examples can be run in the examples folder under the readme.py script, like so:

$ cd examples
$ yahoofantasy login
$ python readme.py

Development

Issues, pull requests, and contributions are more than welcome.

To run the tests, after install:

$ py.test

Or to keep running tests using testmon and drop into a pdb shell on failure (my preferred mode):

$ pytest-watch --pdb -- --testmon -s

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yahoofantasy-1.1.1.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

yahoofantasy-1.1.1-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file yahoofantasy-1.1.1.tar.gz.

File metadata

  • Download URL: yahoofantasy-1.1.1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for yahoofantasy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9b860070325e5eabfdaeecce113e142ef95372f10e8bd075cfdbc2cee1690c8d
MD5 e18b8d1ac0f0d9c234e7ca86c0355d02
BLAKE2b-256 4041a19adb914659d99b6e6ad99cb7601838494a8a014020bd49194edbb4cd9c

See more details on using hashes here.

File details

Details for the file yahoofantasy-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: yahoofantasy-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for yahoofantasy-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d276fc7334dd9c2db444207e5174d4787f384881c8f8a4acfa87a57b03bd300a
MD5 ba61c0f7291f3dff7a4fde2ab3c8ced1
BLAKE2b-256 9f698ddbfd227ee57c04405b4ae4c96f78113c36242c6e2661c1faedf4993a81

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page