From 0228a868ec37ee7114e1b26888a16d267a3d64ff Mon Sep 17 00:00:00 2001 From: Simon Bilsky-Rollins Date: Sat, 26 Oct 2024 15:37:14 -0700 Subject: [PATCH 1/3] Basketball League Transactions --- espn_api/basketball/league.py | 16 ++++++++++++++++ espn_api/basketball/transaction.py | 17 +++++++++++++++++ espn_api/basketball/transaction_item.py | 7 +++++++ 3 files changed, 40 insertions(+) create mode 100644 espn_api/basketball/transaction.py create mode 100644 espn_api/basketball/transaction_item.py diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index cf35d9d6..ea41fa2f 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -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): @@ -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 = { + 'view': 'mTransactions2', + 'scoringPeriodId': scoring_period, + } + + filters = {"transactions":{"filterType":{"value":["FREEAGENT","WAIVER","WAIVER_ERROR"]}}} + 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..7e6237e6 --- /dev/null +++ b/espn_api/basketball/transaction.py @@ -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)) + + def __repr__(self): + items = ', '.join([str(item) for item in self.items]) + return f'Transaction({self.team.team_name} {self.type} {items})' diff --git a/espn_api/basketball/transaction_item.py b/espn_api/basketball/transaction_item.py new file mode 100644 index 00000000..fbaf42c7 --- /dev/null +++ b/espn_api/basketball/transaction_item.py @@ -0,0 +1,7 @@ +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}' From 5096ae4a24b6f5db76118b6001e5d51e29580956 Mon Sep 17 00:00:00 2001 From: Simon Bilsky-Rollins Date: Sat, 2 Nov 2024 23:18:03 -0700 Subject: [PATCH 2/3] feedback --- espn_api/basketball/constant.py | 16 ++++++++++++++++ espn_api/basketball/league.py | 13 +++++++++---- espn_api/basketball/transaction.py | 14 ++++++++++---- espn_api/basketball/transaction_item.py | 7 ------- 4 files changed, 35 insertions(+), 15 deletions(-) delete mode 100644 espn_api/basketball/transaction_item.py 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}' From 492baccf5ce184b0024fd44c4ec6b05b7df99d29 Mon Sep 17 00:00:00 2001 From: Simon Bilsky-Rollins Date: Sun, 3 Nov 2024 08:48:01 -0800 Subject: [PATCH 3/3] fix typing error --- espn_api/basketball/league.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index cd90c12e..30a81e48 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -1,5 +1,5 @@ import json -from typing import List, Tuple, Union +from typing import List, Set, Union from ..base_league import BaseLeague from .team import Team @@ -104,7 +104,7 @@ 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]: + 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