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()
|
load_dotenv()
|
||||||
ESPN_URL = str(os.getenv("ESPN_URL"))
|
ESPN_URL = str(os.getenv("ESPN_URL"))
|
||||||
|
|
||||||
|
|
||||||
def get_game_data():
|
def get_game_data():
|
||||||
res = requests.get(ESPN_URL).json()
|
res = requests.get(ESPN_URL).json()
|
||||||
return res["events"]
|
return res["events"]
|
||||||
|
3
game.py
3
game.py
@ -33,7 +33,8 @@ class Game:
|
|||||||
|
|
||||||
def parse_game_status(data) -> GameStatus:
|
def parse_game_status(data) -> GameStatus:
|
||||||
if data["type"]["name"] == "STATUS_SCHEDULED":
|
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:
|
else:
|
||||||
title = f"{data['type']['detail']}"
|
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 dotenv import load_dotenv
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.table import Table
|
import argparse
|
||||||
from rich import box
|
|
||||||
from data import get_game_data
|
from data import get_game_data
|
||||||
from game import game_factory
|
from game import game_factory
|
||||||
|
from output import output_game
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
def create_table(game):
|
def init_games():
|
||||||
table = Table(width=50, box=box.SQUARE)
|
games = []
|
||||||
table.add_column(game.status.title, width=32)
|
events = get_game_data()
|
||||||
table.add_column("R", width=3, justify="right")
|
for event in events:
|
||||||
table.add_column("H", width=3, justify="right")
|
games.append(game_factory(event["competitions"][0]))
|
||||||
table.add_column("E", width=3, justify="right")
|
|
||||||
|
|
||||||
home_team = game.home_team
|
return games
|
||||||
away_team = game.away_team
|
|
||||||
|
|
||||||
|
|
||||||
table.add_row(home_team.name,
|
# Parse options for teams
|
||||||
str(home_team.runs),
|
def main():
|
||||||
str(home_team.hits),
|
arg_parser = argparse.ArgumentParser(prog="Baseball Score CLI",
|
||||||
str(home_team.errors))
|
description="Get the current scores of the games.")
|
||||||
table.add_row(away_team.name,
|
arg_parser.add_argument('-t', '--team',
|
||||||
str(away_team.runs),
|
help="Give a specific team to get scores for.",
|
||||||
str(away_team.hits),
|
required=False)
|
||||||
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)
|
|
||||||
|
|
||||||
|
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