Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for waivers with faab #56

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
"""Claim a single player from waivers 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):
"""Claim one player from waivers 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
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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_drop_with_faab.xml file is missing from the PR. Can you commit it? Same with add_drop_no_faab.xml below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, just added them. Thanks!

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