Skip to content

Commit

Permalink
Adding details function to Team class (#58)
Browse files Browse the repository at this point in the history
Adding a new function to the Team class to fetch the details of the
current team.

---------

Co-authored-by: DMcP89 <DMcP89@users.noreply.github.com>
  • Loading branch information
DMcP89 and DMcP89 authored Feb 8, 2025
1 parent 72be91e commit 0d8c178
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ docs/_build
.vscode/
.envrc
build/
.python-version
.python-version
oauth.json
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def readme():


setup(name='yahoo_fantasy_api',
version='2.10.1',
version='2.11.0',
description='Python bindings to access the Yahoo! Fantasy APIs',
long_description=readme(),
url='http://github.com/spilchen/yahoo_fantasy_api',
Expand Down
1 change: 1 addition & 0 deletions yahoo_fantasy_api/scripts/yfa_team
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if __name__ == '__main__':
lg = yfa.League(sc, args['<league_id>'])
teams = lg.teams()
tm = lg.to_team(lg.team_key())
print(tm.details())
print("Opponent: {}".format(teams[tm.matchup(args['<week>'])]['name']))
print("")
print("Roster")
Expand Down
15 changes: 15 additions & 0 deletions yahoo_fantasy_api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def __init__(self, sc, team_key):
def inject_yhandler(self, yhandler):
self.yhandler = yhandler

def details(self):
"""Return the details of the team
:return: Dictionary of the team details
>>> tm.details()
{'team_key': '388.l.27081.t.9', 'team_id': '9', 'name': 'Team Name',
'url': 'http://baseball.fantasysports.yahoo.com/archive/mlb/2013/27081/9',
'team_logos': [{'team_logo': {'size': 'large', 'url': 'http://l.yimg
"""
t = objectpath.Tree(self.yhandler.get_teams_by_keys_raw([self.team_key]))
json = t.execute('$..teams..team[0]')
details = {k: v for dic in [val for val in json if val != []] for k, v in dic.items()}
return details

def matchup(self, week):
"""Return the team of the matchup my team is playing in a given week
Expand Down
10 changes: 10 additions & 0 deletions yahoo_fantasy_api/tests/mock_yhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,13 @@ def put_roster(self, team_key, xml):
# This produces no output. Just save the xml for inspection by the
# test.
self.roster_xml = xml

def get_teams_by_keys_raw(self, team_keys):
"""Return the raw JSON when requesting details of a team.
:param team_keys: List of team keys to fetch the details For
:type team_keys: list[str]
:return: JSON document of the request.
"""
with open(self.dir_path + "/sample.team_details.json", "r") as f:
return json.load(f)
71 changes: 71 additions & 0 deletions yahoo_fantasy_api/tests/sample.team_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"fantasy_content": {
"xml:lang": "en-US",
"yahoo:uri": "/fantasy/v2/teams;team_keys=449.l.751781.t.9",
"teams": {
"0": {
"team": [
[
{ "team_key": "449.l.751781.t.9" },
{ "team_id": "9" },
{ "name": "Gibb it to me baby" },
{ "is_owned_by_current_login": 1 },
{ "url": "https://football.fantasysports.yahoo.com/f1/751781/9" },
{
"team_logos": [
{
"team_logo": {
"size": "large",
"url": "https://yahoofantasysports-res.cloudinary.com/image/upload/t_s192sq/fantasy-logos/5d910bdf34a17c6daf1a695b705c5b7807a6f701a421241218a9a32acdde0c31.jpg"
}
}
]
},
[],
{ "waiver_priority": 2 },
{ "faab_balance": "4" },
{ "number_of_moves": 40 },
{ "number_of_trades": 0 },
{
"roster_adds": {
"coverage_type": "week",
"coverage_value": 18,
"value": "0"
}
},
[],
{ "league_scoring_type": "head" },
[],
[],
{
"has_draft_grade": 1,
"draft_grade": "B+",
"draft_recap_url": "https://football.fantasysports.yahoo.com/f1/751781/9/draftrecap"
},
[],
[],
{
"managers": [
{
"manager": {
"manager_id": "9",
"nickname": "Dave",
"guid": "BHVG6BFSIBLNQNPEC2SKXNNRVY",
"is_current_login": "1",
"image_url": "https://s.yimg.com/ag/images/default_user_profile_pic_64sq.jpg",
"felo_score": "814",
"felo_tier": "platinum"
}
}
]
}
]
]
},
"count": 1
},
"time": "52.261114120483ms",
"copyright": "Certain Data by Sportradar, Stats Perform and Rotowire",
"refresh_rate": "60"
}
}
17 changes: 13 additions & 4 deletions yahoo_fantasy_api/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

DIR_PATH = os.path.dirname(os.path.realpath(__file__))


def test_matchup(mock_team):
opponent = mock_team.matchup(3)
assert(opponent == '388.l.27081.t.5')
Expand Down Expand Up @@ -89,8 +90,8 @@ def test__construct_transaction_xml(mock_team):
expected_xml = file.read().replace(' ', '\t')

action = "add/drop"
add_player_id=123
drop_player_id=456
add_player_id = 123
drop_player_id = 456
faab = 99

actual_xml = mock_team._construct_transaction_xml(
Expand All @@ -105,8 +106,8 @@ def test__construct_transaction_xml_with_faab(mock_team):
expected_xml = file.read().replace(' ', '\t')

action = "add/drop"
add_player_id=123
drop_player_id=456
add_player_id = 123
drop_player_id = 456

actual_xml = mock_team._construct_transaction_xml(
action, add_player_id, drop_player_id
Expand All @@ -129,3 +130,11 @@ def test_change_roster(mock_team):

with pytest.raises(Exception):
mock_team.change_positions("3", plyrs)


def test_details(mock_team):
details = mock_team.details()
assert details['team_key'] == '449.l.751781.t.9'
assert details['team_id'] == '9'
assert details['name'] == 'Gibb it to me baby'
assert details['is_owned_by_current_login'] == 1
9 changes: 9 additions & 0 deletions yahoo_fantasy_api/yhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def get_teams_raw(self):
"""
return self.get("users;use_login=1/games/teams")

def get_teams_by_keys_raw(self, team_keys):
"""Return the raw JSON when requesting details of a team.
:param team_keys: List of team keys to fetch the details For
:type team_keys: list[str]
:return: JSON document of the request.
"""
return self.get("teams;team_keys={}".format(",".join(team_keys)))

def get_standings_raw(self, league_id):
"""Return the raw JSON when requesting standings for a league.
Expand Down

0 comments on commit 0d8c178

Please sign in to comment.