toddrob99 / MLB-StatsAPI

Python wrapper for MLB Stats API
GNU General Public License v3.0
533 stars 100 forks source link

What endpoint to get a gamePk given info such as both teams, season, or date #83

Closed maxnbf closed 2 years ago

maxnbf commented 2 years ago

Hello,

I'm trying to make a feature where a user can search for a game by supplying at least one (MLB) team name, and or a season/date.

for example user can supply:

One/Both (MLB) team names and year

OR

One/Both (MLB) team names and exact date

And would in return give a list of all games that satisfy the conditions.

I understand there may not be an endpoint that does exactly this but what would be the most effective way to recreate these results?

toddrob99 commented 2 years ago

First, I would suggest storing a list/dict of team names and ids, and creating a function to look up the team id from the team name that the user enters. Then you can use the team id(s) for the API call. You can also use the statsapi.lookup_team() method, but it may be faster to do it locally.

For the date/year, I would suggest creating a function that takes the user input and decides what date(s) to use. If the input is a year, use start_date = 1/1/XXXX and end_date = 12/31/XXXX. If the input is a date, make sure it's formatted either YYYY-MM-DD or MM/DD/YYYY.

Then use the statsapi.schedule() method to look up the games, using the date or (start_date and end_date), team_id, and opponent_team_id that you looked up from user input.