Skip to content

Commit

Permalink
Add support for waivers with faab (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
indigobuffalo authored Jan 20, 2025
1 parent 6b26e6b commit 4060127
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
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.9.1',
version='2.10.0',
description='Python bindings to access the Yahoo! Fantasy APIs',
long_description=readme(),
url='http://github.com/spilchen/yahoo_fantasy_api',
Expand Down
37 changes: 36 additions & 1 deletion yahoo_fantasy_api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ def add_player(self, player_id):
xml = self._construct_transaction_xml("add", player_id)
self.yhandler.post_transactions(self.league_id, xml)

def claim_player(self, player_id, faab=None):
"""Submit a waiver claim for a single player by their player ID
:param player_id: Yahoo! player ID of the player to add
:type player_id: int
:param faab: Number of faab dollars to bid on the claim
:type faab: int
>>> tm.add_player(6767, faab=7)
"""
xml = self._construct_transaction_xml("add", player_id, faab=faab)
self.yhandler.post_transactions(self.league_id, xml)

def drop_player(self, player_id):
"""Drop a single player by their player ID
Expand All @@ -174,6 +187,23 @@ def add_and_drop_players(self, add_player_id, drop_player_id):
drop_player_id)
self.yhandler.post_transactions(self.league_id, xml)

def claim_and_drop_players(self, add_player_id, drop_player_id, faab=None):
"""Submit a waiver claim for one player and drop another in the same transaction
:param add_player_id: Yahoo! player ID of the player to add
:type add_player_id: int
:param drop_player_id: Yahoo! player ID of the player to drop
:type drop_player_id: int
:param faab: Number of faab dollars to bid on the claim
:type faab: int
>>> tm.claim_and_drop_players(6770, 6767, faab=22)
"""
xml = self._construct_transaction_xml(
"add/drop", add_player_id, drop_player_id, faab=faab
)
self.yhandler.post_transactions(self.league_id, xml)

def proposed_trades(self):
"""
Retrieve information for any proposed trades that include your team
Expand Down Expand Up @@ -408,13 +438,18 @@ def _construct_change_roster_xml(self, time_frame, modified_lineup):

return doc.toprettyxml()

def _construct_transaction_xml(self, action, *player_ids):
def _construct_transaction_xml(self, action, *player_ids, faab=None):
doc = Document()
transaction = doc.appendChild(doc.createElement('fantasy_content')) \
.appendChild(doc.createElement('transaction'))

transaction.appendChild(doc.createElement('type')) \
.appendChild(doc.createTextNode(action))

if faab is not None:
transaction.appendChild(doc.createElement("faab_bid")) \
.appendChild(doc.createTextNode(str(faab)))

if action == 'add/drop':
players = transaction.appendChild(doc.createElement('players'))
self._construct_transaction_player_xml(doc, players, player_ids[0],
Expand Down
22 changes: 22 additions & 0 deletions yahoo_fantasy_api/tests/add_drop_no_faab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" ?>
<fantasy_content>
<transaction>
<type>add/drop</type>
<players>
<player>
<player_key>268.p.123</player_key>
<transaction_data>
<type>add</type>
<destination_team_key>268.l.46645</destination_team_key>
</transaction_data>
</player>
<player>
<player_key>268.p.456</player_key>
<transaction_data>
<type>drop</type>
<source_team_key>268.l.46645</source_team_key>
</transaction_data>
</player>
</players>
</transaction>
</fantasy_content>
23 changes: 23 additions & 0 deletions yahoo_fantasy_api/tests/add_drop_with_faab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>
<fantasy_content>
<transaction>
<type>add/drop</type>
<faab_bid>99</faab_bid>
<players>
<player>
<player_key>268.p.123</player_key>
<transaction_data>
<type>add</type>
<destination_team_key>268.l.46645</destination_team_key>
</transaction_data>
</player>
<player>
<player_key>268.p.456</player_key>
<transaction_data>
<type>drop</type>
<source_team_key>268.l.46645</source_team_key>
</transaction_data>
</player>
</players>
</transaction>
</fantasy_content>
39 changes: 35 additions & 4 deletions yahoo_fantasy_api/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pytest


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 @@ -58,8 +60,7 @@ def test_proposed_trades(mock_team):


def test__construct_trade_xml(mock_team):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(f'{dir_path}/accept_trade.xml', 'r') as file:
with open(f'{DIR_PATH}/accept_trade.xml', 'r') as file:
expected_xml = file.read().replace(' ', '\t')

transaction_key = '396.l.49770.pt.1'
Expand All @@ -69,8 +70,7 @@ def test__construct_trade_xml(mock_team):


def test__construct_trade_proposal_xml(mock_team):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(f'{dir_path}/trade_proposal.xml', 'r') as file:
with open(f'{DIR_PATH}/trade_proposal.xml', 'r') as file:
expected_xml = file.read().replace(' ', '\t')

tradee_team_key = '248.l.55438.t.4'
Expand All @@ -84,6 +84,37 @@ def test__construct_trade_proposal_xml(mock_team):
assert actual_xml == expected_xml


def test__construct_transaction_xml(mock_team):
with open(f'{DIR_PATH}/add_drop_with_faab.xml', 'r') as file:
expected_xml = file.read().replace(' ', '\t')

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

actual_xml = mock_team._construct_transaction_xml(
action, add_player_id, drop_player_id, faab=faab
)

assert actual_xml == expected_xml


def test__construct_transaction_xml_with_faab(mock_team):
with open(f'{DIR_PATH}/add_drop_no_faab.xml', 'r') as file:
expected_xml = file.read().replace(' ', '\t')

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

actual_xml = mock_team._construct_transaction_xml(
action, add_player_id, drop_player_id
)

assert actual_xml == expected_xml


def test_change_roster(mock_team):
plyrs = [{'player_id': 5981, 'selected_position': 'BN'},
{'player_id': 4558, 'selected_position': 'BN'}]
Expand Down

0 comments on commit 4060127

Please sign in to comment.