My friends and I are part of a fantasy football league that was started in 2002. With over a decade and a half worth of history, our league is pretty intense. Unfortunately, it lacks a proper record-keeping system to keep track of all statistics. To go back and retrieve the data one would have to manually copy and paste everything, a time-consuming task. In this post, I illustrate how to connect to Yahoo’s Fantasy Sports API with oauth 2 authorization, make a request for league data, receive response object, parse the XML response object and finally write the data to a csv file.
To begin we first have to sign up at Yahoo’s Developer Network to receive a consumer key and consumer secret. Once you have the consumer key and consumer secret open up a text editor and type in the following:
After pasting the consumer key and consumer secret, save the file as oauth2.json and place it in the working directory.
Yahoo_oauth is a great python library package that supports OAuth authentication mechanism necessary to access many of Yahoo services. Once the yahoo_oauth package is downloaded via pip we can start the process of connecting to Yahoo’s Fantasy Sports API.
from yahoo_oauth import OAuth2
With that we’re connected and have established an oauth2 authentication necessary to send requests.
This lengthy documentation explains the parameters that needs to be provided when sending a request. Essentially the building blocks of the API are Collections and Resources. Resources are typically a reference to single entity such as a league, a team or a player. A Collection is simply a wrapper that contain multiple similar resources. A Resource is referred to by a Single Key, while collection will be referred to by Multiple Keys. Using multiple keys I can retrieve data for multiple leagues at once. After researching further and going through the documentation I was able to determine the league keys for our league for every year since 2002. They’re summarized below.
url = "https://fantasysports.yahooapis.com/fantasy/v2/leagues;league_keys=
A status code of 200 will indicate Success - The action was successfully received, understood, and accepted.
After a successful status code of 200 we receive a XML response object. To parse and modify the XML response object, I use ElementTree and re. Finally to save into an excel file, I’ll use the csv package.
# Import packages
Initially the XML had a namespace that effected the naming of elements and attributes. To make life easier, it was removed. A for loop was then written that extracted all the specified information. We stored this information in a dictionary that was subsequently added it to a list. The list is then written into a csv file.
# Generate headers for excel file.
By utilizing this method we were able to collect 16 years worth of league metadata which included the year league was played, owner name, team name, team key, rank, playoff seed, total wins, total losses, total ties, total points scored, total points against, number of moves and number of trades.