69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
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":
|
|
game_date = data['type']['shortDetail'].split('-')[1].strip()
|
|
title = f"{data['type']['detail']} - {game_date}"
|
|
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
|
|
|