Skip to content

Commit

Permalink
rename library to yfpy
Browse files Browse the repository at this point in the history
  • Loading branch information
uberfastman committed Nov 20, 2019
1 parent f0a2039 commit 538d930
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 163 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
```
__ ________ ______ _______ __
\ \ / / ____| ____| __ \ \ / /
\ \_/ /| |__ | |__ | |__) \ \_/ /
\ / | __| | __| | ___/ \ /
| | | | | | | | | |
|_| |_| |_| |_| |_|
__ ________ _______ __
\ \ / / ____| __ \ \ / /
\ \_/ /| |__ | |__) \ \_/ /
\ / | __| | ___/ \ /
| | | | | | | |
|_| |_| |_| |_|
```

### Python API wrapper for the Yahoo Fantasy Football public API
### Python API wrapper for the Yahoo Fantasy Sports public API
##### By Wren J. R. (uberfastman)

---

## Yahoo Fantasy Football Metrics Report Generator
## Yahoo Fantasy Sports API Wrapper (YFPY)

### Table of Contents
* [About](#about)
Expand All @@ -26,7 +26,7 @@
<a name="about"></a>
### About

YFFPY is a comprehensive wrapper around the Yahoo Fantasy Football API. It allows for easy retrieval and parsing of almost any data you might wish to extract and use from any Yahoo fantasy football league to which your Yahoo account has access (or for public leagues).
YFPY is a comprehensive wrapper around the Yahoo Fantasy Sports API. It allows for easy retrieval and parsing of almost any data you might wish to extract and use from any Yahoo fantasy league to which your Yahoo account has access (or for public leagues). The primary focus of this wrapper is on fantasy football (NFL), but it also supports usage with fantasy hockey (NHL), fantasy baseball (MLB), and fantasy basketball (NBA).

---

Expand All @@ -36,39 +36,39 @@ YFFPY is a comprehensive wrapper around the Yahoo Fantasy Football API. It allow
* In your project directory, run

```
pip install yffpy
pip install yfpy
```
or add `yffpy` to your project `requirements.txt`.
or add `yfpy` to your project `requirements.txt`.
* Follow the instructions in the below [Setup](#setup) section.
* See `test/test.py` for fully functional code snippets within the query tests that demonstrate how to use YFFPY.
* PLEASE NOTE: Assuming you followed the setup instructions correctly, the ***first*** time you use YFFPY, a browser window will open up asking you to allow your app to access your Yahoo fantasy football data. You ***MUST*** hit allow, and then copy the verification code that pops up into the command line prompt where it will now be asking for verification, hit enter, and the OAuth2 three-legged handshake should be complete and your data should have been successfully retrieved.
* YFFPY should have now generated a `token.json` for you in the same directory where you stored your `private.json` credentials, and for all subsequent runs of your app, you should be able to keep retrieving Yahoo fantasy football data using YFFPY without re-verifying, since the generated refresh token should now just renew whenever you use the same `token.json` file to authenticate your app.
* See `test/test.py` for fully functional code snippets within the query tests that demonstrate how to use YFPY.
* PLEASE NOTE: Assuming you followed the setup instructions correctly, the ***first*** time you use YFPY, a browser window will open up asking you to allow your app to access your Yahoo fantasy sports data. You ***MUST*** hit allow, and then copy the verification code that pops up into the command line prompt where it will now be asking for verification, hit enter, and the OAuth2 three-legged handshake should be complete and your data should have been successfully retrieved.
* YFPY should have now generated a `token.json` for you in the same directory where you stored your `private.json` credentials, and for all subsequent runs of your app, you should be able to keep retrieving Yahoo fantasy sports data using YFPY without re-verifying, since the generated refresh token should now just renew whenever you use the same `token.json` file to authenticate your app.
---
<a name="setup"></a>
### Setup
* Log in to a Yahoo account with access to whatever fantasy football leagues from which you wish to retrieve data.
* Log in to a Yahoo account with access to whatever fantasy leagues from which you wish to retrieve data.
* Go to [https://developer.yahoo.com/apps/create/](https://developer.yahoo.com/apps/create/) and create an app (you must be logged into your Yahoo account as stated above). For the app, select the following options:
* `Application Name` (**Required**): `yffpy` (you can name your app whatever you want, but this is just an example).
* `Application Name` (**Required**): `yfpy` (you can name your app whatever you want, but this is just an example).
* `Application Type` (**Required**): select the `Installed Application` radio button.
* `Description` (*Optional*): you *may* write a short description of what the app does.
* `Home Page URL` (*Optional*): if you have a web address related to your app you *may* add it here.
* `Redirect URI(s)` (**Required**): this field must contain a valid redirect address, so you can use `localhost:8080`
* `API Permissions` (**Required**): check the `Fantasy Sports` checkbox. You can leave the `Read` option selected (appears in an accordion expansion underneath the `Fantasy Sports` checkbox once you select it).
* Click the `Create App` button.
* Once the app is created, it should redirect you to a page for your app, which will show both a `Client ID` and a `Client Secret`.
* Make a copy of `examples/EXAMPLE-private.json`, rename it to just `private.json`, and copy the `Client ID` and `Client Secret` values to their respective fields (make sure the strings are wrapped regular quotes (`""`), NOT formatted quotes (`“”`)). The path to this file will be needed to point YFFPY to your credentials.
* Now you should be ready to initialize the OAuth2 connection between YFFPY your Yahoo account.
* Make a copy of `examples/EXAMPLE-private.json`, rename it to just `private.json`, and copy the `Client ID` and `Client Secret` values to their respective fields (make sure the strings are wrapped regular quotes (`""`), NOT formatted quotes (`“”`)). The path to this file will be needed to point YFPY to your credentials.
* Now you should be ready to initialize the OAuth2 connection between YFPY your Yahoo account.
---
<a name="dependencies"></a>
### Dependencies
YFFPY has only been tested on macOS, but is written to be platform agnostic. It runs only in Python 3, and has only been tested with Python 3.7.
YFPY has only been tested on macOS, but is written to be platform agnostic. It runs only in Python 3, and has only been tested with Python 3.7.
Direct project dependencies can be viewed in `requirements.txt`, and all dependencies, including transitive dependencies, can be viewed in `dev-requirements.txt`.
Expand All @@ -77,14 +77,14 @@ Direct project dependencies can be viewed in `requirements.txt`, and all depende
<a name="troubleshooting"></a>
### Troubleshooting
Occasionally when you use the Yahoo fantasy football API, there are hangups on the other end that can cause data not to transmit, and you might encounter an error similar to this:
Occasionally when you use the Yahoo fantasy sports API, there are hangups on the other end that can cause data not to transmit, and you might encounter an error similar to this:
```
Traceback (most recent call last):
File "yffpy-app.py", line 114, in <module>
File "yfpy-app.py", line 114, in <module>
var = app.run()
File "/Users/your_username/PATH/T0/LOCAL/PROJECT/yffpy-app.py", line 429, in run
File "/Users/your_username/PATH/T0/LOCAL/PROJECT/yfpy-app.py", line 429, in run
for team in team_standings:
IndexError: list index out of range
```
Typically when the above error (or a similar error) occurs, it simply means that one of the Yahoo Fantasy Football API calls failed and so no data was retrieved. This can be fixed by simply re-running data query.
Typically when the above error (or a similar error) occurs, it simply means that one of the Yahoo Fantasy Sports API calls failed and so no data was retrieved. This can be fixed by simply re-running data query.
16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
print("Removing stale \"dist\" directory before packaging...")
shutil.rmtree("dist")

if os.path.exists("yffpy.egg-info"):
print("Removing stale \"yffpy.egg-info\" directory before packaging...")
shutil.rmtree("yffpy.egg-info")
if os.path.exists("yfpy.egg-info"):
print("Removing stale \"yfpy.egg-info\" directory before packaging...")
shutil.rmtree("yfpy.egg-info")

with open("README.md", "r") as docs:
long_description = docs.read()
Expand All @@ -22,15 +22,15 @@
required = reqs.read().splitlines()

setuptools.setup(
name="yffpy",
version="2.10.0",
name="yfpy",
version="3.0.0",
author="Wren J. R.",
author_email="[email protected]",
description="Python API wrapper for the Yahoo Fantasy Football public API.",
description="Python API wrapper for the Yahoo Fantasy Sports public API.",
long_description=long_description,
long_description_content_type="text/markdown",
keywords="yahoo fantasy football api wrapper sports",
url="https://github.com/uberfastman/yffpy",
keywords="yahoo fantasy sports api wrapper nfl football nhl hockey mlb baseball nba basketball",
url="https://github.com/uberfastman/yfpy",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
16 changes: 8 additions & 8 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import warnings
from unittest import skip, TestCase

from yffpy import Data
from yffpy.models import Game, StatCategories, User, Scoreboard, Settings, Standings, League, Player, Team, \
from yfpy import Data
from yfpy.models import Game, StatCategories, User, Scoreboard, Settings, Standings, League, Player, Team, \
TeamPoints, TeamStandings, Roster
from yffpy.query import YahooFantasyFootballQuery
from yfpy.query import YahooFantasySportsQuery


class QueryTestCase(TestCase):

def setUp(self):
# Suppress YahooFantasyFootballQuery debug logging
logging.getLogger("yffpy.query").setLevel(level=logging.INFO)
# Suppress YahooFantasySportsQuery debug logging
logging.getLogger("yfpy.query").setLevel(level=logging.INFO)

# Ignore resource warnings from unittest module
warnings.simplefilter("ignore", ResourceWarning)
Expand Down Expand Up @@ -59,10 +59,10 @@ def setUp(self):
# self.player_id = "4588" # NHL: Braden Holtby
self.player_key = self.game_id + ".p." + self.player_id

# Instantiate yffpy objects
# Instantiate yfpy objects
self.yahoo_data = Data(self.data_dir)
self.yahoo_query = YahooFantasyFootballQuery(auth_dir, self.league_id, game_id=self.game_id,
game_code=self.game_code, offline=False)
self.yahoo_query = YahooFantasySportsQuery(auth_dir, self.league_id, game_id=self.game_id,
game_code=self.game_code, offline=False)

# Manually override league key for example code to work
self.yahoo_query.league_key = self.game_id + ".l." + self.league_id
Expand Down
6 changes: 3 additions & 3 deletions yffpy/__init__.py → yfpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
__author__ = "Wren J. R. (uberfastman)"
__email__ = "[email protected]"

from yffpy.data import Data
from yffpy.models import User, Game, League, Team, Standings, Manager, RosterAdds, TeamLogo, TeamPoints, \
from yfpy.data import Data
from yfpy.models import User, Game, League, Team, Standings, Manager, RosterAdds, TeamLogo, TeamPoints, \
TeamStandings, OutcomeTotals, Streak, Settings, RosterPosition, StatCategories, StatModifiers, Stat, \
StatPositionType, Bonus, Matchup, MatchupGrade, Player, ByeWeeks, Headshot, Name, PlayerPoints, PlayerStats, \
SelectedPosition
from yffpy.query import YahooFantasyFootballQuery
from yfpy.query import YahooFantasySportsQuery
40 changes: 20 additions & 20 deletions yffpy/data.py → yfpy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import logging
import os

from yffpy.models import YahooFantasyObject
from yffpy.utils import complex_json_handler, unpack_data
from yfpy.models import YahooFantasyObject
from yfpy.utils import complex_json_handler, unpack_data

logger = logging.getLogger(__name__)

Expand All @@ -32,24 +32,24 @@ def update_data_dir(self, new_save_dir):
self.data_dir = new_save_dir

@staticmethod
def get(yff_query, params=None):
def get(yf_query, params=None):
"""Run query to retrieve Yahoo fantasy football data.
:param yff_query: chosen yffpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yffpy query function
:return: result of the yffpy query
:param yf_query: chosen yfpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yfpy query function
:return: result of the yfpy query
"""
if params:
return yff_query(**params)
return yf_query(**params)
else:
return yff_query()
return yf_query()

def save(self, file_name, yff_query, params=None, new_data_dir=None):
def save(self, file_name, yf_query, params=None, new_data_dir=None):
"""Retrieve and save Yahoo fantasy football data locally.
:param file_name: name of file to which data will be saved
:param yff_query: chosen yffpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yffpy query function
:param yf_query: chosen yfpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yfpy query function
:param new_data_dir: (optional) full path to new desired directory to which data will be saved
:return:
"""
Expand All @@ -62,8 +62,8 @@ def save(self, file_name, yff_query, params=None, new_data_dir=None):
if not os.path.exists(self.data_dir):
os.makedirs(self.data_dir)

# run the actual yffpy query and retrieve the query results
data = self.get(yff_query, params)
# run the actual yfpy query and retrieve the query results
data = self.get(yf_query, params)

# save the retrieved data locally
saved_data_file_path = os.path.join(self.data_dir, file_name + ".json")
Expand All @@ -76,7 +76,7 @@ def load(self, file_name, data_type_class=None, new_data_dir=None):
"""Load Yahoo fantasy football data already stored locally (CANNOT BE RUN IF save METHOD HAS NEVER BEEN RUN).
:param file_name: name of file from which data will be loaded
:param data_type_class: (optional) yffpy models.py class for data casting
:param data_type_class: (optional) yfpy models.py class for data casting
:param new_data_dir: (optional) full path to new desired directory from which data will be loaded
:return:
"""
Expand All @@ -97,20 +97,20 @@ def load(self, file_name, data_type_class=None, new_data_dir=None):
saved_data_file_path))
return data

def retrieve(self, file_name, yff_query, params=None, data_type_class=None, new_data_dir=None):
def retrieve(self, file_name, yf_query, params=None, data_type_class=None, new_data_dir=None):
"""Fetch data from the web or load it locally (combination of the save and load methods).
:param file_name: name of file to/from which data will be saved/loaded
:param yff_query: chosen yffpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yffpy query function
:param data_type_class: (optional) yffpy models.py class for data casting
:param yf_query: chosen yfpy query method to run
:param params: (optional) dict of parameters to be passed to chosen yfpy query function
:param data_type_class: (optional) yfpy models.py class for data casting
:param new_data_dir: (optional) full path to new desired directory to/from which data will be saved/loaded
:return:
"""
if self.dev_offline:
return self.load(file_name, data_type_class, new_data_dir)
else:
if self.save_data:
return self.save(file_name, yff_query, params, new_data_dir)
return self.save(file_name, yf_query, params, new_data_dir)
else:
return self.get(yff_query, params)
return self.get(yf_query, params)
Loading

0 comments on commit 538d930

Please sign in to comment.