Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basketball League Transactions #598

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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

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#L109-L110

Added lines #L109 - L110 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L112-L113

Added lines #L112 - L113 were not covered by tests

params = {

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L115

Added line #L115 was not covered by tests
'view': 'mTransactions2',
'scoringPeriodId': scoring_period,
cwendt94 marked this conversation as resolved.
Show resolved Hide resolved
}

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

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#L120-L121

Added lines #L120 - L121 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L123-L124

Added lines #L123 - L124 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L126

Added line #L126 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
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))

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L3-L11

Added lines #L3 - L11 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 15 in espn_api/basketball/transaction.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L14-L15

Added lines #L14 - L15 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L19-L20

Added lines #L19 - L20 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/transaction.py#L23

Added line #L23 was not covered by tests
Loading