Skip to content

Commit

Permalink
script takes date
Browse files Browse the repository at this point in the history
- also arrowprops updated
  • Loading branch information
bdilday committed Jun 29, 2024
1 parent b5ece30 commit 7a14277
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pybbda/graphics/graphical_standings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"avoid_text": True,
"avoid_points": False,
"avoid_self": False,
# "arrowprops": {"arrowstyle": "-", "color": "k", "lw": 0.25},
"arrowprops": {"arrowstyle": "-", "color": "k", "lw": 0.25},
}


Expand Down
62 changes: 49 additions & 13 deletions scripts/mlb_standings.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import argparse
import requests
from pybbda.graphics.graphical_standings import plot_graphical_standings
import pandas as pd
from functools import reduce
import pandas as pd
from pybbda.graphics.graphical_standings import plot_graphical_standings
from datetime import date, timedelta
from dateutil.parser import parse as datetime_parse
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

url = (
URL_FORMAT = (
"https://statsapi.mlb.com/api/v1/standings"
"?leagueId=103,104"
"&season=2022"
"&date=2022-04-13"
"&season={season}"
"&date={date_str}"
"&standingsTypes=regularSeason,springTraining,firstHalf,secondHalf"
"&hydrate=division,conference,sport,league,"
"team(nextSchedule(team,gameType=[R,F,D,L,W,C],inclusive=false),"
"previousSchedule(team,gameType=[R,F,D,L,W,C],inclusive=true))"
)

TODAY = date.today()
YESTERDAY = TODAY + timedelta(days=-1)


def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--standings-date",
"-d",
required=False,
type=lambda date_string: datetime_parse(date_string).date(),
default=YESTERDAY,
)
return parser.parse_args()


def get_url(standings_date: date) -> str:
return URL_FORMAT.format(season=standings_date.year, date_str=standings_date)


def process_team_record(team_record):
return {
Expand All @@ -33,12 +59,22 @@ def process_record(record):
]


payload = requests.get(url).json()
print(payload)
data = reduce(list.__add__, [process_record(record) for record in payload["records"]])
print(data)
def main():
args = _parse_args()
url = get_url(args.standings_date)
logger.info(f"fetching standings from {url}")
payload = requests.get(url).json()
logger.debug(payload)
data = reduce(
list.__add__, [process_record(record) for record in payload["records"]]
)
logger.debug(data)

standings = pd.DataFrame(data)
logger.info(standings)
p = plot_graphical_standings(standings)
print(p)


standings = pd.DataFrame(data)
print(standings)
p = plot_graphical_standings(standings)
print(p)
if __name__ == "__main__":
main()

0 comments on commit 7a14277

Please sign in to comment.