Keep working on some more functionality.

This commit is contained in:
Dave Smith-Hayes 2025-03-08 22:13:59 -05:00
commit 5f3ac87997
8 changed files with 162 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Created by venv; see https://docs.python.org/3/library/venv.html
__pycache__
bin
include
lib
lib64
.env

25
data.py Normal file
View File

@ -0,0 +1,25 @@
import requests
import os
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"]
def get_team_game(team, games):
found_team = None
for game in games:
competition = game["competitions"][0]
found_team = filter(lambda c: c.team.abbrevation.lower() == team,
competition["competitors"])
if found_team:
break
return found_team

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

67
game.py Normal file
View File

@ -0,0 +1,67 @@
from dataclasses import dataclass
from dateutil import parser
import datetime
@dataclass
class Team:
name: str
abbr: str
runs: int = 0
hits: int = 0
errors: int = 0
def get_score(self) -> str:
return f"{self.runs}/{self.hits}/{self.errors}"
@dataclass
class GameStatus:
title: str
status: str
detail: str
short_detail: str
@dataclass
class Game:
date: datetime.datetime
status: GameStatus
inning: str
home_team: Team
away_team: Team
def parse_game_status(data) -> GameStatus:
if data["type"]["name"] == "STATUS_SCHEDULED":
title = f"{data['type']['detail']} - {data['type']['shortDetail']}"
else:
title = f"{data['type']['detail']}"
return GameStatus(title=title,
status=data["type"]["name"],
detail=data["type"]["detail"],
short_detail=data["type"]["shortDetail"])
def game_factory(data) -> Game:
teams = []
for team in data["competitors"]:
teams.append(Team(name=team["team"]["displayName"],
abbr=team["team"]["abbreviation"],
runs=int(team["score"]),
hits=team["hits"],
errors=team["errors"]))
home_team = teams[0]
away_team = teams[1]
status = parse_game_status(data["status"])
game = Game(date=parser.parse(data["date"]),
status=status,
inning=status.detail,
home_team=home_team,
away_team=away_team)
return game

43
main.py Normal file
View File

@ -0,0 +1,43 @@
import requests
import os
from dotenv import load_dotenv
from rich.console import Console
from rich.table import Table
from rich import box
from data import get_game_data
from game import game_factory
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")
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
games = get_game_data()
for event in games:
game = game_factory(event["competitions"][0])
table = create_table(game)
console.print(table)

5
pyvenv.cfg Normal file
View File

@ -0,0 +1,5 @@
home = /usr/bin
include-system-site-packages = false
version = 3.13.2
executable = /usr/bin/python3.13
command = /usr/bin/python -m venv /home/dave/src/baseball

12
requirements.txt Normal file
View File

@ -0,0 +1,12 @@
certifi==2025.1.31
charset-normalizer==3.4.1
idna==3.10
markdown-it-py==3.0.0
mdurl==0.1.2
Pygments==2.19.1
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
requests==2.32.3
rich==13.9.4
six==1.17.0
urllib3==2.3.0