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 cf35d9d6..30a81e48 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -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''' @@ -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''' diff --git a/espn_api/basketball/transaction.py b/espn_api/basketball/transaction.py new file mode 100644 index 00000000..ae0e5cf7 --- /dev/null +++ b/espn_api/basketball/transaction.py @@ -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}'