Skip to content

Commit

Permalink
Basketball League Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbilskyrollins committed Oct 26, 2024
1 parent be5e92b commit 0228a86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .box_score import get_box_scoring_type_class, BoxScore
from .constant import PRO_TEAM_MAP
from .activity import Activity
from .transaction import Transaction
from .constant import POSITION_MAP, ACTIVITY_MAP

class League(BaseLeague):
Expand Down Expand Up @@ -104,6 +105,21 @@ def recent_activity(self, size: int = 25, msg_type: str = None, offset: int = 0,

return activity

def transactions(self, scoring_period: int = None) -> List[Transaction]:
'''Returns a list of recent transactions'''
params = {

Check warning on line 110 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L110

Added line #L110 was not covered by tests
'view': 'mTransactions2',
'scoringPeriodId': scoring_period,
}

filters = {"transactions":{"filterType":{"value":["FREEAGENT","WAIVER","WAIVER_ERROR"]}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

Check warning on line 116 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L115-L116

Added lines #L115 - L116 were not covered by tests

data = self.espn_request.league_get(params=params, headers=headers)
transactions = data['transactions']

Check warning on line 119 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L118-L119

Added lines #L118 - L119 were not covered by tests

return [Transaction(transaction, self.player_map, self.get_team_data) for transaction in transactions]

Check warning on line 121 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L121

Added line #L121 was not covered by tests

def free_agents(self, week: int=None, size: int=50, position: str=None, position_id: int=None) -> List[Player]:
'''Returns a List of Free Agents for a Given Week\n
Should only be used with most recent season'''
Expand Down
17 changes: 17 additions & 0 deletions espn_api/basketball/transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .transaction_item import TransactionItem

class Transaction(object):
def __init__(self, data, player_map, get_team_data):
self.team = get_team_data(data['teamId'])
self.type = data['type']
self.status = data['status']
self.scoring_period = data['scoringPeriodId']
self.date = data['processDate']
self.bid_amount = data['bidAmount']
self.items = []
for item in data['items']:
self.items.append(TransactionItem(item, player_map))

Check warning on line 13 in espn_api/basketball/transaction.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L5-L13

Added lines #L5 - L13 were not covered by tests

def __repr__(self):
items = ', '.join([str(item) for item in self.items])
return f'Transaction({self.team.team_name} {self.type} {items})'

Check warning on line 17 in espn_api/basketball/transaction.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L16-L17

Added lines #L16 - L17 were not covered by tests
7 changes: 7 additions & 0 deletions espn_api/basketball/transaction_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TransactionItem(object):
def __init__(self, data, player_map):
self.type = data['type']
self.player = player_map[data['playerId']]

Check warning on line 4 in espn_api/basketball/transaction_item.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction_item.py#L3-L4

Added lines #L3 - L4 were not covered by tests

def __repr__(self):
return f'{self.type} {self.player}'

Check warning on line 7 in espn_api/basketball/transaction_item.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction_item.py#L7

Added line #L7 was not covered by tests

0 comments on commit 0228a86

Please sign in to comment.