diff --git a/espn_api/basketball/constant.py b/espn_api/basketball/constant.py index 173699f7..d33672ee 100644 --- a/espn_api/basketball/constant.py +++ b/espn_api/basketball/constant.py @@ -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', diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index ea41fa2f..cd90c12e 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -6,10 +6,9 @@ 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 .transaction import Transaction -from .constant import POSITION_MAP, ACTIVITY_MAP +from .constant import POSITION_MAP, ACTIVITY_MAP, TRANSACTION_TYPES class League(BaseLeague): '''Creates a League instance for Public/Private ESPN league''' @@ -105,14 +104,20 @@ 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]: + 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":["FREEAGENT","WAIVER","WAIVER_ERROR"]}}} + filters = {"transactions":{"filterType":{"value":list(types)}}} headers = {'x-fantasy-filter': json.dumps(filters)} data = self.espn_request.league_get(params=params, headers=headers) diff --git a/espn_api/basketball/transaction.py b/espn_api/basketball/transaction.py index 7e6237e6..ae0e5cf7 100644 --- a/espn_api/basketball/transaction.py +++ b/espn_api/basketball/transaction.py @@ -1,13 +1,11 @@ -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.date = data.get('processDate') + self.bid_amount = data.get('bidAmount') self.items = [] for item in data['items']: self.items.append(TransactionItem(item, player_map)) @@ -15,3 +13,11 @@ def __init__(self, data, player_map, get_team_data): 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}' diff --git a/espn_api/basketball/transaction_item.py b/espn_api/basketball/transaction_item.py deleted file mode 100644 index fbaf42c7..00000000 --- a/espn_api/basketball/transaction_item.py +++ /dev/null @@ -1,7 +0,0 @@ -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}'