Skip to content

Commit

Permalink
refactor for pypi dist
Browse files Browse the repository at this point in the history
  • Loading branch information
uberfastman committed Sep 6, 2019
1 parent 1b1da7e commit dd65f8e
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 111 deletions.
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
auth/private.json
auth/token.json
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# JetBrains IDE project files
.idea/*

# Distribution / packaging
.Python
build/
dist/
*.egg-info/
34 changes: 0 additions & 34 deletions __init__.py

This file was deleted.

10 changes: 10 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
certifi==2019.6.16
chardet==3.0.4
idna==2.8
pyaml==19.4.1
PyYAML==5.1.2
rauth==0.7.3
requests==2.22.0
stringcase==1.2.0
urllib3==1.25.3
yahoo-oauth==0.1.9
Empty file added examples/examples.py
Empty file.
2 changes: 1 addition & 1 deletion resources/EXAMPLE-private.json → examples/private.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"consumer_key": "YAHOO_DEVELOPER_APP_CONSUMER_KEY_STRING",
"consumer_secret": "YAHOO_DEVELOPER_APP_CONSUMER_SECRET_STRING"
}
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yahoo-oauth==0.1.9
stringcase==1.2.0
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import setuptools

with open("README.md", "r") as docs:
long_description = docs.read()

with open("requirements.txt") as reqs:
required = reqs.read().splitlines()

setuptools.setup(
name="yffpy",
version="1.0.0",
author="Wren J. R.",
author_email="wrenjr@yahoo.com",
description="Python API wrapper for the Yahoo Fantasy Football public API.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/uberfastman/yffpy",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Development Status :: 4 - Beta",
"Topic :: Software Development :: Libraries :: Python Modules",
"Environment :: Console",
"Intended Audience :: Developers"
],
python_requires=">=3.5",
install_requires=required
)
3 changes: 3 additions & 0 deletions yffpy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from yffpy.data import Data
from yffpy.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
91 changes: 91 additions & 0 deletions yffpy/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json
import logging
import os

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

logger = logging.getLogger(__name__)


class Data(object):

def __init__(self, save_dir):
self.save_dir = save_dir

@staticmethod
def get(yff_query, params=None):
# run data query
if params:
query_output = yff_query(**params, run=True)
else:
query_output = yff_query(run=True)
data = query_output.get("data")
url = query_output.get("url")
logger.info(
"DATA FETCHED WITH QUERY URL: {}".format(url) + (" AND PARAMS: {}".format(params) if params else ""))
return data

def save(self, file_name, yff_query, params=None):

if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)

data = self.get(yff_query, params)

saved_data_file_path = os.path.join(self.save_dir, file_name + ".json")
with open(saved_data_file_path, "w", encoding="utf-8") as data_file:
json.dump(data, data_file, ensure_ascii=False, indent=2, default=complex_json_handler)
logger.info("DATA SAVED LOCALLY TO: {}".format(saved_data_file_path))
return data

def load(self, file_name, data_type_class=None):

saved_data_file_path = os.path.join(self.save_dir, file_name + ".json")
if os.path.exists(saved_data_file_path):
with open(saved_data_file_path, "r", encoding="utf-8") as data_file:
unpacked = unpack_data(json.load(data_file), YahooFantasyObject)
data = data_type_class(unpacked) if data_type_class else unpacked
logger.info("DATA LOADED LOCALLY FROM: {}".format(saved_data_file_path))
else:
raise FileNotFoundError(
"FILE {} DOES NOT EXIST. CANNOT LOAD DATA LOCALLY WITHOUT HAVING PREVIOUSLY SAVED DATA!".format(
saved_data_file_path))
return data

# def persist_and_retrieve_data(self, yff_query, data_dir, data_file_name, data_type_class=None, params=None,
# persist_data=False, refresh_data=True):
# data_persistence_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), data_dir)
#
# if not os.path.exists(data_persistence_dir):
# os.makedirs(data_persistence_dir)
#
# if refresh_data:
# # run data query
# if params:
# query_output = yff_query(**params, run=True)
# else:
# query_output = yff_query(run=True)
# data = query_output.get("data")
# url = query_output.get("url")
# logger.info(
# "DATA FETCHED WITH QUERY URL: {}".format(url) + (" AND PARAMS: {}".format(params) if params else ""))
# else:
# persisted_data_file_path = os.path.join(data_persistence_dir, data_file_name + ".json")
# if os.path.exists(persisted_data_file_path):
# with open(persisted_data_file_path, "r", encoding="utf-8") as data_file:
# unpacked = unpack_data(json.load(data_file), YahooFantasyObject)
# data = data_type_class(unpacked) if data_type_class else unpacked
# logger.info("DATA RETRIEVED LOCALLY: {}".format(persisted_data_file_path))
# else:
# raise FileNotFoundError(
# "FILE {} DOES NOT EXIST. CANNOT RUN LOCALLY WITHOUT HAVING PREVIOUSLY PERSISTED DATA!".format(
# persisted_data_file_path))
#
# if persist_data and refresh_data:
# persisted_data_file_path = os.path.join(data_persistence_dir, data_file_name + ".json")
# with open(persisted_data_file_path, "w", encoding="utf-8") as persisted_data_file:
# json.dump(data, persisted_data_file, ensure_ascii=False, indent=2, default=complex_json_handler)
# logger.info("DATA PERSISTED LOCALLY: {}".format(persisted_data_file_path))
#
# return data
14 changes: 2 additions & 12 deletions dao.py → yffpy/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import json
import logging

import inflect
import stringcase

logger = logging.getLogger(__name__)
place = inflect.engine()

from yffpy.utils import complex_json_handler

def complex_json_handler(obj):
if hasattr(obj, "serialized"):
return obj.serialized()
else:
try:
return str(obj, "utf-8")
except TypeError:
raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)))
logger = logging.getLogger(__name__)


class YahooFantasyObject(object):
Expand Down
54 changes: 34 additions & 20 deletions query.py → yffpy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from yahoo_oauth import OAuth2

from yffpy.dao import *
from yffpy.util import unpack_data, reformat_json_list
from yffpy.models import *
from yffpy.utils import reformat_json_list, unpack_data

logger = logging.getLogger(__name__)
logging.getLogger("yahoo_oauth").setLevel(level=logging.INFO)
Expand All @@ -18,10 +18,6 @@ def __init__(self, auth_dir, league_id, game_id=None, offline=False):
self.league_key = None
self.league_name = None

# yahoo_oauth_token_cache_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "auth")
# if not os.path.exists(yahoo_oauth_token_cache_dir):
# os.makedirs(yahoo_oauth_token_cache_dir)

if not offline:
with open(os.path.join(auth_dir, "private.json")) as yahoo_app_credentials:
auth_info = json.load(yahoo_app_credentials)
Expand Down Expand Up @@ -49,18 +45,22 @@ def query(self, url, data_key_list, data_type_class=None, run=True):
response = self.oauth.session.get(url, params={"format": "json"})
logger.debug("RESPONSE (RAW JSON): {}".format(response.json()))
raw_response_data = response.json().get("fantasy_content")
logger.debug("RESPONSE (Yahoo fantasy football data extracted from: \"fantasy_content\"): {}".format(raw_response_data))
logger.debug("RESPONSE (Yahoo fantasy football data extracted from: \"fantasy_content\"): {}".format(
raw_response_data))

for i in range(len(data_key_list)):
if type(raw_response_data) == list:
raw_response_data = reformat_json_list(raw_response_data)[data_key_list[i]]
else:
raw_response_data = raw_response_data.get(data_key_list[i])
logger.debug("RESPONSE (Yahoo fantasy football data extracted from: {}): {}".format(data_key_list, raw_response_data))
logger.debug("RESPONSE (Yahoo fantasy football data extracted from: {}): {}".format(data_key_list,
raw_response_data))

unpacked = unpack_data(raw_response_data, YahooFantasyObject)
clean_response_data = data_type_class(unpacked) if data_type_class else unpacked
logger.debug("UNPACKED AND PARSED JSON (Yahoo fantasy football data wth parent type: {}): {}".format(data_type_class, unpacked))
logger.debug(
"UNPACKED AND PARSED JSON (Yahoo fantasy football data wth parent type: {}): {}".format(data_type_class,
unpacked))

return {
"data": clean_response_data,
Expand All @@ -78,39 +78,53 @@ def get_current_nfl_fantasy_game(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/game/nfl", ["game"], Game, run=run)

def get_nfl_fantasy_game(self, game_id, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/game/" + str(game_id), ["game"], Game, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/game/" + str(game_id), ["game"], Game,
run=run)

def get_league_key(self):
if self.game_id:
return self.get_nfl_fantasy_game(self.game_id).get("data").game_key + ".l." + self.league_id
else:
logger.warning("No Yahoo Fantasy game id provided, defaulting to current NFL fantasy football season game id.")
logger.warning(
"No Yahoo Fantasy game id provided, defaulting to current NFL fantasy football season game id.")
return self.get_current_nfl_fantasy_game().get("data").game_key + ".l." + self.league_id

def get_user_game_history(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;codes=nfl/", ["users", "0", "user"], User, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;codes=nfl/",
["users", "0", "user"], User, run=run)

def get_user_league_history(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;codes=nfl/leagues/", ["users", "0", "user"], User, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;codes=nfl/leagues/",
["users", "0", "user"], User, run=run)

def get_overview(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/", ["league"], League, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/", ["league"],
League, run=run)

def get_standings(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/standings", ["league", "standings"], Standings, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/standings",
["league", "standings"], Standings, run=run)

def get_settings(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/settings", ["league", "settings"], Settings, run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/settings",
["league", "settings"], Settings, run=run)

def get_teams(self, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/teams", ["league", "teams"], run=run)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/teams",
["league", "teams"], run=run)

def get_matchups(self, chosen_week, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/scoreboard;week=" + str(chosen_week), ["league", "scoreboard", "0", "matchups"], run=run)
return self.query(
"https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/scoreboard;week=" + str(
chosen_week), ["league", "scoreboard", "0", "matchups"], run=run)

def get_team_roster(self, team_id, chosen_week, run=True):
team_key = self.league_key + ".t." + str(team_id)
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/team/" + str(team_key) + "/roster;week=" + str(chosen_week) + "/players/stats", ["team", "roster", "0", "players"], run=run)
return self.query(
"https://fantasysports.yahooapis.com/fantasy/v2/team/" + str(team_key) + "/roster;week=" + str(
chosen_week) + "/players/stats", ["team", "roster", "0", "players"], run=run)

def get_player_stats(self, player_key, chosen_week, run=True):
return self.query("https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/players;player_keys=" + player_key + "/stats;type=week;week=" + str(chosen_week), ["league", "players", "0", "player"], Player, run=run)
return self.query(
"https://fantasysports.yahooapis.com/fantasy/v2/league/" + self.league_key + "/players;player_keys=" + player_key + "/stats;type=week;week=" + str(
chosen_week), ["league", "players", "0", "player"], Player, run=run)
Loading

0 comments on commit dd65f8e

Please sign in to comment.