From 6406947dcd98d5c524cccae2b3e1edb78f2ef9f4 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sat, 15 Mar 2025 20:55:24 -0400 Subject: [PATCH] Format the code a bit, and work out filtering specific teams. --- README.md | 11 +++++++++++ data.py | 1 + game.py | 3 ++- main.py | 57 ++++++++++++++++++++++++++----------------------------- output.py | 25 ++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 README.md create mode 100644 output.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..8263d62 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Baseball CLI Tool + +This application will simply grab the on-going games from the ESPN JSON API. + +## TODO + +* Parse by Team +* Get team stats from the API +* Cache the API calls + * Store in SQLite database + diff --git a/data.py b/data.py index f3bea86..e2423c7 100644 --- a/data.py +++ b/data.py @@ -5,6 +5,7 @@ from dotenv import load_dotenv load_dotenv() ESPN_URL = str(os.getenv("ESPN_URL")) + def get_game_data(): res = requests.get(ESPN_URL).json() return res["events"] diff --git a/game.py b/game.py index cf0e738..faec7b5 100644 --- a/game.py +++ b/game.py @@ -33,7 +33,8 @@ class Game: def parse_game_status(data) -> GameStatus: if data["type"]["name"] == "STATUS_SCHEDULED": - title = f"{data['type']['detail']} - {data['type']['shortDetail']}" + game_date = data['type']['shortDetail'].split('-')[1].strip() + title = f"{data['type']['detail']} - {game_date}" else: title = f"{data['type']['detail']}" diff --git a/main.py b/main.py index a292bb5..2586f9d 100644 --- a/main.py +++ b/main.py @@ -1,43 +1,40 @@ -import requests -import os from dotenv import load_dotenv from rich.console import Console -from rich.table import Table -from rich import box +import argparse from data import get_game_data from game import game_factory +from output import output_game load_dotenv() - console = Console() -def create_table(game): - table = Table(width=50, box=box.SQUARE) - table.add_column(game.status.title, width=32) - table.add_column("R", width=3, justify="right") - table.add_column("H", width=3, justify="right") - table.add_column("E", width=3, justify="right") +def init_games(): + games = [] + events = get_game_data() + for event in events: + games.append(game_factory(event["competitions"][0])) - home_team = game.home_team - away_team = game.away_team + return games - table.add_row(home_team.name, - str(home_team.runs), - str(home_team.hits), - str(home_team.errors)) - table.add_row(away_team.name, - str(away_team.runs), - str(away_team.hits), - str(away_team.errors)) - - return table - -games = get_game_data() - -for event in games: - game = game_factory(event["competitions"][0]) - table = create_table(game) - console.print(table) +# Parse options for teams +def main(): + arg_parser = argparse.ArgumentParser(prog="Baseball Score CLI", + description="Get the current scores of the games.") + arg_parser.add_argument('-t', '--team', + help="Give a specific team to get scores for.", + required=False) + args = arg_parser.parse_args() + + if args.team: + # find the team + print(args.team) + else: + for event in init_games(): + table = output_game(event) + console.print(table) + +if __name__ == "__main__": + main() diff --git a/output.py b/output.py new file mode 100644 index 0000000..877b835 --- /dev/null +++ b/output.py @@ -0,0 +1,25 @@ +from rich import box +from rich.table import Table + +def output_game(game): + table = Table(width=50, box=box.SQUARE) + table.add_column(game.status.title, width=32) + table.add_column("R", width=3, justify="right") + table.add_column("H", width=3, justify="right") + table.add_column("E", width=3, justify="right") + + home_team = game.home_team + away_team = game.away_team + + + table.add_row(home_team.name, + str(home_team.runs), + str(home_team.hits), + str(home_team.errors)) + table.add_row(away_team.name, + str(away_team.runs), + str(away_team.hits), + str(away_team.errors)) + + return table +