diff --git a/pyball/models/schedule/__init__.py b/pyball/models/schedule/__init__.py new file mode 100644 index 0000000..bece3e6 --- /dev/null +++ b/pyball/models/schedule/__init__.py @@ -0,0 +1,10 @@ +from .away import Away +from .content import Content +from .event import Event +from .game import Game +from .home import Home +from .league_record import LeagueRecord +from .scheduled_game import Schedule +from .status import Status +from .teams import Teams +from .team import Team diff --git a/pyball/models/schedule/away.py b/pyball/models/schedule/away.py new file mode 100644 index 0000000..81bb865 --- /dev/null +++ b/pyball/models/schedule/away.py @@ -0,0 +1,17 @@ +from dataclasses import dataclass, field +from typing import Union, Dict, Any + +from .league_record import LeagueRecord +from .team import Team + + +@dataclass +class Away: + splitSquad: bool = None + seriesNumber: int = None + leagueRecord: Union[LeagueRecord, Dict[str, Any]] = field(default_factory=dict) + team: Union[Team, Dict[str, Any]] = field(default_factory=dict) + + def __post_init__(self): + self.leagueRecord = LeagueRecord(**self.leagueRecord) + self.team = Team(**self.team) diff --git a/pyball/models/schedule/content.py b/pyball/models/schedule/content.py new file mode 100644 index 0000000..856caf1 --- /dev/null +++ b/pyball/models/schedule/content.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass + + +@dataclass +class Content: + link: str = None diff --git a/pyball/models/schedule/event.py b/pyball/models/schedule/event.py new file mode 100644 index 0000000..99d14df --- /dev/null +++ b/pyball/models/schedule/event.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass + + +@dataclass +class Event: + pass diff --git a/pyball/models/schedule/game.py b/pyball/models/schedule/game.py new file mode 100644 index 0000000..5b59c7c --- /dev/null +++ b/pyball/models/schedule/game.py @@ -0,0 +1,44 @@ +from dataclasses import dataclass, field +from typing import Union, Dict, Any + +from .status import Status +from .teams import Teams +from .content import Content +from pyball.models.venue import Venue + + +@dataclass +class Game: + gamePk: int = None + link: str = None + gameType: str = None + season: int = None + gameDate: str = None + gameNumber: int = None + publicFacing: bool = None + doubleHeader: str = None + gamedayType: str = None + tiebreaker: str = None + calendarEventID: str = None + seasonDisplay: str = None + dayNight: str = None + description: str = None + scheduledInnings: int = None + gamesInSeries: int = None + seriesGameNumber: int = None + seriesDescription: str = None + recordSource: str = None + ifNecessary: str = None + ifNecessaryDescription: str = None + rescheduledFrom: str = None + startTimeTBD: str = None + status: Union[Status, Dict[str, Any]] = field(default_factory=dict) + teams: Union[Teams, Dict[str, Any]] = field(default_factory=dict) + venue: Union[Venue, Dict[str, Any]] = field(default_factory=dict) + content: Union[Content, Dict[str, Any]] = field(default_factory=dict) + + def __post_init__(self): + self.status = Status(**self.status) + self.teams = Teams(**self.teams) + self.venue = Venue(**self.venue) + self.content = Content(**self.content) diff --git a/pyball/models/schedule/home.py b/pyball/models/schedule/home.py new file mode 100644 index 0000000..1b10e83 --- /dev/null +++ b/pyball/models/schedule/home.py @@ -0,0 +1,17 @@ +from dataclasses import dataclass, field +from typing import Union, Dict, Any + +from .league_record import LeagueRecord +from .team import Team + + +@dataclass +class Home: + splitSquad: bool = None + seriesNumber: int = None + leagueRecord: Union[LeagueRecord, Dict[str, Any]] = field(default_factory=dict) + team: Union[Team, Dict[str, Any]] = field(default_factory=dict) + + def __post_init__(self): + self.leagueRecord = LeagueRecord(**self.leagueRecord) + self.team = Team(**self.team) diff --git a/pyball/models/schedule/league_record.py b/pyball/models/schedule/league_record.py new file mode 100644 index 0000000..2a52745 --- /dev/null +++ b/pyball/models/schedule/league_record.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class LeagueRecord: + wins: int = None + losses: int = None + pct: float = None diff --git a/pyball/models/schedule/scheduled_game.py b/pyball/models/schedule/scheduled_game.py new file mode 100644 index 0000000..c18e700 --- /dev/null +++ b/pyball/models/schedule/scheduled_game.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass, field +from typing import Union, Dict, Any + +from .game import Game +from .event import Event + + +@dataclass +class Schedule: + date: str = None + totalItems: int = None + totalEvents: int = None + totalGames: int = None + totalGamesInProgress: int = None + games: Union[Game, Dict[str, Any]] = field(default_factory=dict) + events: Union[Event, Dict[str, Any]] = field(default_factory=dict) + + def __post_init__(self): + self.games = [Game(**g) for g in self.games] + self.events = Event() diff --git a/pyball/models/schedule/status.py b/pyball/models/schedule/status.py new file mode 100644 index 0000000..e7c46aa --- /dev/null +++ b/pyball/models/schedule/status.py @@ -0,0 +1,11 @@ +from dataclasses import dataclass + + +@dataclass +class Status: + abstractGameState: str = None + codedGameState: str = None + detailedState: str = None + statusCode: str = None + abstractGameCode: str = None + startTimeTBD: bool = None diff --git a/pyball/models/schedule/team.py b/pyball/models/schedule/team.py new file mode 100644 index 0000000..e761164 --- /dev/null +++ b/pyball/models/schedule/team.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class Team: + id: int = None + name: str = None + link: str = None diff --git a/pyball/models/schedule/teams.py b/pyball/models/schedule/teams.py new file mode 100644 index 0000000..369f401 --- /dev/null +++ b/pyball/models/schedule/teams.py @@ -0,0 +1,15 @@ +from dataclasses import dataclass, field +from typing import Union, Dict, Any + +from .home import Home +from .away import Away + + +@dataclass +class Teams: + away: Union[Away, Dict[str, Any]] = field(default_factory=dict) + home: Union[Home, Dict[str, Any]] = field(default_factory=dict) + + def __post_init__(self): + self.away = Away(**self.away) + self.home = Home(**self.home) diff --git a/pyball/pyball.py b/pyball/pyball.py index c05e5cb..6dcf267 100644 --- a/pyball/pyball.py +++ b/pyball/pyball.py @@ -1,5 +1,5 @@ import requests -from datetime import datetime +from datetime import datetime, date from typing import List, Dict, Any, Union from pyball.constants import BASE_URL @@ -35,6 +35,7 @@ from pyball.models import Sport from pyball.models.team import Coach from pyball.models.team import Player +from pyball.models.schedule import Schedule class PyBall: @@ -202,6 +203,17 @@ def get_sport_by_id(self, sport_id: id) -> Sport: results = self._get(url) return Sport(**results['sports'][0]) + def get_schedule(self, start_date: date = date.today(), end_date: date = date.today(), + team_id: int = None, game_type: str = None): + url = "{0}schedule?sportId=1&startDate={1}&endDate={2}".format(BASE_URL, + start_date, end_date) + if team_id: + url = url + "&teamId={0}".format(team_id) + if game_type: + url = url + "&gameType={0}".format(game_type) + results = self._get(url) + return [Schedule(**schedule) for schedule in results['dates']] + def get_seasons(self): # url = "{0}seasons/".format(BASE_URL) # results = self._get(url) diff --git a/test/test_schedule.py b/test/test_schedule.py new file mode 100644 index 0000000..f61745f --- /dev/null +++ b/test/test_schedule.py @@ -0,0 +1,27 @@ +import pytest +from pyball import PyBall +from datetime import date +from pyball.models.schedule import Schedule, Game, Team, Status, Content, Teams, Home, Away +from pyball.models.venue import Venue + + +@pytest.fixture(scope='module') +def schedules(): + pyball = PyBall() + return pyball.get_schedule(start_date=date(2019, 4, 1), end_date=date(2019, 4, 1), + team_id=114, game_type='R') + + +def test_get_schedule_returns_list_of_schedules(schedules): + assert isinstance(schedules, list) + assert isinstance(schedules[0], Schedule) + assert isinstance(schedules[0].games, list) + assert isinstance(schedules[0].games[0], Game) + assert isinstance(schedules[0].games[0].teams, Teams) + assert isinstance(schedules[0].games[0].teams.home, Home) + assert isinstance(schedules[0].games[0].teams.home.team, Team) + assert isinstance(schedules[0].games[0].teams.away, Away) + assert isinstance(schedules[0].games[0].teams.away.team, Team) + assert isinstance(schedules[0].games[0].status, Status) + assert isinstance(schedules[0].games[0].venue, Venue) + assert isinstance(schedules[0].games[0].content, Content)