Skip to content

Commit

Permalink
Merge pull request #598 from simonbilskyrollins/transactions
Browse files Browse the repository at this point in the history
Basketball League Transactions
  • Loading branch information
cwendt94 authored Nov 6, 2024
2 parents c41ff3d + 492bacc commit 365c3ca
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
16 changes: 16 additions & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@
'TRADED': 244,
}

TRANSACTION_TYPES = {
'DRAFT',
'TRADE_ACCEPT',
'WAIVER',
'TRADE_VETO',
'FUTURE_ROSTER',
'ROSTER',
'RETRO_ROSTER',
'TRADE_PROPOSAL',
'TRADE_UPHOLD',
'FREEAGENT',
'TRADE_DECLINE',
'WAIVER_ERROR',
'TRADE_ERROR'
}

NINE_CAT_STATS = {
'3PM',
'AST',
Expand Down
27 changes: 24 additions & 3 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import json
from typing import List, Tuple, Union
from typing import List, Set, Union

from ..base_league import BaseLeague
from .team import Team
from .player import Player
from .matchup import Matchup
from .box_score import get_box_scoring_type_class, BoxScore
from .constant import PRO_TEAM_MAP
from .activity import Activity
from .constant import POSITION_MAP, ACTIVITY_MAP
from .transaction import Transaction
from .constant import POSITION_MAP, ACTIVITY_MAP, TRANSACTION_TYPES

class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''
Expand Down Expand Up @@ -104,6 +104,27 @@ def recent_activity(self, size: int = 25, msg_type: str = None, offset: int = 0,

return activity

def transactions(self, scoring_period: int = None, types: Set[str] = {"FREEAGENT","WAIVER","WAIVER_ERROR"}) -> List[Transaction]:
'''Returns a list of recent transactions'''
if not scoring_period:
scoring_period = self.scoringPeriodId

if types > TRANSACTION_TYPES:
raise Exception('Invalid transaction type')

params = {
'view': 'mTransactions2',
'scoringPeriodId': scoring_period,
}

filters = {"transactions":{"filterType":{"value":list(types)}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

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

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

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
23 changes: 23 additions & 0 deletions espn_api/basketball/transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.get('processDate')
self.bid_amount = data.get('bidAmount')
self.items = []
for item in data['items']:
self.items.append(TransactionItem(item, player_map))

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

class TransactionItem(object):
def __init__(self, data, player_map):
self.type = data['type']
self.player = player_map[data['playerId']]

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

0 comments on commit 365c3ca

Please sign in to comment.