From 4060127a9761ce01323c68c31b325b4cc5dd42e1 Mon Sep 17 00:00:00 2001 From: Stephen Akerson-Nagy Date: Mon, 20 Jan 2025 03:54:07 -0800 Subject: [PATCH] Add support for waivers with faab (#56) --- setup.py | 2 +- yahoo_fantasy_api/team.py | 37 +++++++++++++++++- yahoo_fantasy_api/tests/add_drop_no_faab.xml | 22 +++++++++++ .../tests/add_drop_with_faab.xml | 23 +++++++++++ yahoo_fantasy_api/tests/test_team.py | 39 +++++++++++++++++-- 5 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 yahoo_fantasy_api/tests/add_drop_no_faab.xml create mode 100644 yahoo_fantasy_api/tests/add_drop_with_faab.xml diff --git a/setup.py b/setup.py index f4864ab..5b591ce 100755 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/yahoo_fantasy_api/team.py b/yahoo_fantasy_api/team.py index 6aaa6fe..ff61ca7 100644 --- a/yahoo_fantasy_api/team.py +++ b/yahoo_fantasy_api/team.py @@ -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 @@ -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 @@ -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], diff --git a/yahoo_fantasy_api/tests/add_drop_no_faab.xml b/yahoo_fantasy_api/tests/add_drop_no_faab.xml new file mode 100644 index 0000000..fba5166 --- /dev/null +++ b/yahoo_fantasy_api/tests/add_drop_no_faab.xml @@ -0,0 +1,22 @@ + + + + add/drop + + + 268.p.123 + + add + 268.l.46645 + + + + 268.p.456 + + drop + 268.l.46645 + + + + + diff --git a/yahoo_fantasy_api/tests/add_drop_with_faab.xml b/yahoo_fantasy_api/tests/add_drop_with_faab.xml new file mode 100644 index 0000000..3471e7c --- /dev/null +++ b/yahoo_fantasy_api/tests/add_drop_with_faab.xml @@ -0,0 +1,23 @@ + + + + add/drop + 99 + + + 268.p.123 + + add + 268.l.46645 + + + + 268.p.456 + + drop + 268.l.46645 + + + + + diff --git a/yahoo_fantasy_api/tests/test_team.py b/yahoo_fantasy_api/tests/test_team.py index f9ea88a..daedd02 100644 --- a/yahoo_fantasy_api/tests/test_team.py +++ b/yahoo_fantasy_api/tests/test_team.py @@ -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') @@ -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' @@ -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' @@ -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'}]