Format the code a bit, and work out filtering specific teams.
This commit is contained in:
parent
5f3ac87997
commit
6406947dcd
11
README.md
Normal file
11
README.md
Normal file
@ -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
|
||||
|
1
data.py
1
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"]
|
||||
|
3
game.py
3
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']}"
|
||||
|
||||
|
57
main.py
57
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()
|
||||
|
25
output.py
Normal file
25
output.py
Normal file
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user