Skip to content
Draft
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
Empty file.
28 changes: 28 additions & 0 deletions basketball_reference_web_scraper/contracts/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import dataclasses
import enum
from typing import Optional, Dict


@dataclasses.dataclass(frozen=True)
class PlayerData:
name: str
identifier: str


class SalaryOption(enum.Enum):
PLAYER = "player"
TEAM = "team"


@dataclasses.dataclass(frozen=True)
class SeasonSalaryData:
value: str
season_identifier: str
option: Optional[SalaryOption]


@dataclasses.dataclass(frozen=True)
class RowData:
player: Optional[PlayerData]
team_id: Optional[str]
salary_by_season: Dict[str, Optional[SeasonSalaryData]]
67 changes: 67 additions & 0 deletions basketball_reference_web_scraper/contracts/readers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import enum
from typing import Optional, Callable, Dict, Any

from lxml.etree import Element

from .data import PlayerData


def read_player_cell_data(cell: Element) -> PlayerData:
return PlayerData(name=cell.text_content(), identifier=cell.get("data-append-csv"))


def read_team_identifier_cell_data(cell: Element) -> str:
return cell.text_content()


class Column(enum.Enum):
PLAYER = "player"
TEAM = "team_id"
FIRST_SEASON_SALARY = "y1"
SECOND_SEASON_SALARY = "y2"
THIRD_SEASON_SALARY = "y3"
FOURTH_SEASON_SALARY = "y4"
FIFTH_SEASON_SALARY = "y5"
SIXTH_SEASON_SALARY = "y6"


class CellIdentifier:
def __init__(self, column: Column):
self.column = column

def identify_cell(self, row: Element) -> Optional[Element]:
matching_cells = row.xpath(f'./td[@data-stat="{self.column.value}"]')
if 1 == len(matching_cells):
return matching_cells[0]


class RowDataReader:

def __init__(self,
cell_identifiers_by_column: Dict[Column, CellIdentifier],
cell_value_readers_by_column: Dict[Column, Callable[[Element], Any]]):
self.cell_identifiers_by_column = cell_identifiers_by_column
self.cell_value_readers_by_column = cell_value_readers_by_column

def read(self, row: Element) -> Dict[Column, Optional[Any]]:
return dict(map(lambda column_and_cell: [
column_and_cell[0],
None if column_and_cell[1] is None \
else self.cell_value_readers_by_column.get(column_and_cell[0])(column_and_cell[1])
], map(
lambda e: [e[0], e[1].identify_cell(row=row)],
self.cell_identifiers_by_column.items())))

@staticmethod
def instance():
# TODO: don't recreate the object on each invocation
return RowDataReader(
cell_identifiers_by_column={
Column.PLAYER: CellIdentifier(column=Column.PLAYER),
Column.TEAM: CellIdentifier(column=Column.TEAM),
},
cell_value_readers_by_column={
Column.PLAYER: read_player_cell_data,
Column.TEAM: read_team_identifier_cell_data
}
)
30 changes: 30 additions & 0 deletions basketball_reference_web_scraper/html.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import re
from typing import Dict, Set, Tuple

from lxml import html
from lxml.html import HtmlComment

from basketball_reference_web_scraper.contracts.readers import RowDataReader, Column


class BasicBoxScoreRow:
def __init__(self, html):
Expand Down Expand Up @@ -1231,10 +1234,37 @@ def losses(self):
return None


class PlayerContractsTableReader:
def __init__(self, row_data_reader: RowDataReader):
self.row_data_reader = row_data_reader

@property
def column_names_by_identifier(self) -> Dict[str, str]:
return dict(
filter(lambda value: 1 == len(value[1]),
map(lambda expected_header_identifier: (
expected_header_identifier,
self.html.xpath(f'.//th[@scope="col"]/[@data-stat="{expected_header_identifier}]')),
Column)))

def rows(self, table):
for row_html in table.xpath('./tbody/tr[@data-row and not(@class)]'):
yield self.row_data_reader.read(row=row_html)


class PlayerContractsRow:
def __init__(self, html):
self.html = html

def retrieve_cell(self, statistic_identifier: str, attributes: Set[str]) -> Tuple[str, Dict[str, str]]:
matching_cells = self.html.xpath(f'.//td[@data-stat="{statistic_identifier}')

if 1 == len(matching_cells):
cell = matching_cells[0]
return cell.text_content(), dict(map(lambda attribute: [attribute, cell.get(attribute)], attributes))

raise ValueError(f"expected exactly one matching cell for statistic identifier {statistic_identifier}")

@property
def player_name(self):
matching_cells = self.html.xpath('.//td[@data-stat="player"]')
Expand Down
6 changes: 3 additions & 3 deletions basketball_reference_web_scraper/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def parse_outcome(self, formatted_outcome):
def parse_margin_of_victory(self, formatted_outcome):
return int(
self.search_formatted_outcome(formatted_outcome=formatted_outcome)
.group(self.margin_of_victory_regex_group_name)
.group(self.margin_of_victory_regex_group_name)
)


Expand Down Expand Up @@ -177,13 +177,13 @@ def parse_scores(self, formatted_scores):
def parse_away_team_score(self, formatted_scores):
return int(
self.parse_scores(formatted_scores=formatted_scores)
.group(self.away_team_score_group_name)
.group(self.away_team_score_group_name)
)

def parse_home_team_score(self, formatted_scores):
return int(
self.parse_scores(formatted_scores=formatted_scores)
.group(self.home_team_score_group_name)
.group(self.home_team_score_group_name)
)


Expand Down
Loading