Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to get schedule #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyball/models/schedule/__init__.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions pyball/models/schedule/away.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions pyball/models/schedule/content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass


@dataclass
class Content:
link: str = None
6 changes: 6 additions & 0 deletions pyball/models/schedule/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass


@dataclass
class Event:
pass
44 changes: 44 additions & 0 deletions pyball/models/schedule/game.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions pyball/models/schedule/home.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions pyball/models/schedule/league_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class LeagueRecord:
wins: int = None
losses: int = None
pct: float = None
20 changes: 20 additions & 0 deletions pyball/models/schedule/scheduled_game.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions pyball/models/schedule/status.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions pyball/models/schedule/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Team:
id: int = None
name: str = None
link: str = None
15 changes: 15 additions & 0 deletions pyball/models/schedule/teams.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 13 additions & 1 deletion pyball/pyball.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions test/test_schedule.py
Original file line number Diff line number Diff line change
@@ -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)