krz/nba-scores

A CLI/TUI app for NBA scores.

clone: git clone https://gitbay.org/krz/nba-scores.git

main: nba/scores.py · raw

  1"""
  2Tabulates a scoreboard for today's games.
  3"""
  4
  5from tabulate import tabulate
  6
  7# ANSI escape codes for text formatting
  8BOLD = "\033[1m"
  9END = "\033[0m"
 10RED = "\033[91m"
 11GREEN = "\033[32m"
 12
 13
 14# Function to get team record from standings
 15def get_team_record(team_name, standings) -> str:
 16    """
 17    Retrieves a team's win-loss record from the standings data.
 18
 19    Args:
 20            team_name (str): Name of the team.
 21            standings (dict): Team standings data.
 22
 23    Returns:
 24            record (str): Team's win-loss record in 'W-L' format. Defaults to 'N/A'.
 25    """
 26    for result_set in standings["resultSets"]:
 27        if result_set["name"] == "Standings":
 28            for team in result_set["rowSet"]:
 29                if team[4] == team_name:
 30                    return f"{team[12]}-{team[13]}"
 31    return "N/A"
 32
 33
 34def get_scoreboard_table(games, standings) -> str:
 35    """
 36    Builds and returns the current day's games as a formatted table string.
 37
 38    Args:
 39            games (dict): JSON parsed games data.
 40            standings (dict): Team standings data.
 41
 42    Returns:
 43            str: Formatted table string with ANSI color codes.
 44    """
 45    scoreboard_data = games["scoreboard"]
 46    game_list = scoreboard_data["games"]
 47
 48    if not game_list:
 49        return "No games scheduled today."
 50
 51    # Prepare the table data
 52    table_data = []
 53    for game in game_list:
 54        home_team = game["homeTeam"]["teamName"]
 55        away_team = game["awayTeam"]["teamName"]
 56        game_status = game["gameStatusText"]
 57        home_score = game["homeTeam"]["score"]
 58        away_score = game["awayTeam"]["score"]
 59
 60        home_record = get_team_record(home_team, standings)
 61        away_record = get_team_record(away_team, standings)
 62
 63        # Determine the winning team
 64        if home_score > away_score:
 65            home_team_bold = f"{BOLD}{GREEN}{home_team} ({home_record}){END}{END}"
 66            away_team_bold = f"{away_team} ({away_record}){END}"
 67            home_score_bold = f"{BOLD}{GREEN}{home_score}{END}{END}"
 68            away_score_bold = f"{away_score}{END}"
 69        elif away_score > home_score:
 70            home_team_bold = f"{home_team} ({home_record}){END}"
 71            away_team_bold = f"{BOLD}{GREEN}{away_team} ({away_record}){END}{END}"
 72            home_score_bold = f"{home_score}{END}"
 73            away_score_bold = f"{BOLD}{GREEN}{away_score}{END}{END}"
 74        else:
 75            home_team_bold = f"{home_team} ({home_record})"
 76            away_team_bold = f"{away_team} ({away_record})"
 77            home_score_bold = f"{home_score}"
 78            away_score_bold = f"{away_score}"
 79
 80        # Determine games still in progress
 81        if game_status != "Final":
 82            game_status = f"{RED}{game_status}{END}"
 83
 84        table_data.append(
 85            [
 86                f"{home_team_bold}\n{away_team_bold}",
 87                f"{home_score_bold}\n{away_score_bold}",
 88                f"{BOLD}{game_status}{END}",
 89            ]
 90        )
 91
 92    # Define the table headers
 93    headers = ["Team", "Score", "Game Status"]
 94
 95    return tabulate(table_data, headers=headers, tablefmt="grid")
 96
 97
 98def build_scoreboard(games, standings) -> None:
 99    """
100    Prints the current day's games in a table format.
101
102    Args:
103            games (dict): JSON parsed games data.
104            standings (dict): Team standings data.
105    """
106    print(get_scoreboard_table(games, standings))