summaryrefslogtreecommitdiff
path: root/build/lib/nba/leaders.py
blob: aa6f3b416c1d35976f76f2f0ddbe33791c3dd391 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Fetches and formats NBA statistical leaders.
"""

import json

from tabulate import tabulate
from nba_api.stats.endpoints.leagueleaders import LeagueLeaders

BOLD = "\033[1m"
END = "\033[0m"

# Ordered list of (api_abbreviation, display_label)
CATEGORIES = [
    ("PTS", "Points"),
    ("REB", "Rebounds"),
    ("AST", "Assists"),
    ("STL", "Steals"),
    ("BLK", "Blocks"),
    ("EFF", "Efficiency"),
    ("FG_PCT", "FG%"),
    ("FT_PCT", "FT%"),
    ("FG3_PCT", "3P%"),
]

# Extra columns to show alongside RANK, PLAYER, TEAM, GP for each category
_EXTRA_COLS = {
    "PTS":    ["PTS", "FGM", "FGA", "FG_PCT", "FTM", "FTA", "FT_PCT"],
    "REB":    ["REB", "OREB", "DREB", "GP"],
    "AST":    ["AST", "TOV", "AST_TOV", "GP"],
    "STL":    ["STL", "TOV", "GP"],
    "BLK":    ["BLK", "PF", "GP"],
    "EFF":    ["EFF", "PTS", "REB", "AST", "GP"],
    "FG_PCT": ["FG_PCT", "FGM", "FGA", "PTS"],
    "FT_PCT": ["FT_PCT", "FTM", "FTA", "PTS"],
    "FG3_PCT":["FG3_PCT", "FG3M", "FG3A", "PTS"],
}

_DISPLAY_NAMES = {
    "FG_PCT": "FG%", "FT_PCT": "FT%", "FG3_PCT": "3P%",
    "FG3M": "3PM", "FG3A": "3PA",
    "AST_TOV": "AST/TO",
}


def fetch_leaders(category: str = "PTS") -> dict:
    endpoint = LeagueLeaders(
        stat_category_abbreviation=category,
        season_type_all_star="Regular Season",
    )
    return json.loads(endpoint.get_json())


def get_leaders_table(data: dict, category: str = "PTS") -> str:
    result = data["resultSet"]
    headers = result["headers"]
    rows = result["rowSet"]

    base = ["RANK", "PLAYER", "TEAM", "GP"]
    extra = [c for c in _EXTRA_COLS.get(category, [category]) if c not in base]
    wanted = base + extra

    idx = {h: i for i, h in enumerate(headers)}
    table_data = [
        [row[idx[col]] for col in wanted if col in idx]
        for row in rows[:25]
    ]
    display_headers = [_DISPLAY_NAMES.get(c, c) for c in wanted if c in idx]

    cat_label = dict(CATEGORIES).get(category, category)
    title = f"{BOLD}League Leaders — {cat_label}{END}"
    return title + "\n" + tabulate(table_data, headers=display_headers, tablefmt="grid")