toddrob99 / MLB-StatsAPI

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

Use reverse index to find actual last game #57

Closed DatGuy1 closed 3 years ago

DatGuy1 commented 3 years ago

Not sure if it's a recent change but when calling the endpoint with the previous parameters, the output contains five games which descends with time. See in example - 2021-06-13, NYY @ PHI 0-7 is the first index. Last index is 2021-06-18, the upcoming game OAK @ NYY (at time of writing). Second to last index is 2021-06-17, NYY @ TOR 8-4 which is the actual last game.

toddrob99 commented 3 years ago

@DatGuy1 are you sure -2 is always the right index? Did you test this under varying circumstances, including on game day before the game starts, during the game, after the game ends, and on an off day?

DatGuy1 commented 3 years ago

I've tested it with a number of different teams today. Before the game, during the game, and after the game. Not had a chance for off day yet though.

DatGuy1 commented 3 years ago

Seems on off days the current behaviour is correct, while all other times second to last index is correct. Maybe the best way to do it would be to iterate through the gameDates of each item?

toddrob99 commented 3 years ago

I think the dates will have to be checked to determine which one to use. There is additional logic to take into consideration though... What about days with doubleheaders? Both games for the current day could be returned, but if it's between games, the first game in the list might be in the future. Also, when a game is in progress, is that game considered the last game, or should it be the last completed game? If you want to propose a set of logic to fix the last_game (and next_game?) method(s), I'm all for it.

DatGuy1 commented 3 years ago

First, correction for my previous comment, the last game is always either [-1] on off days or [-2] on all other times, not [0]. Second, I believe both last_game and next_game should exclude any currently ongoing games. As for sorting by dates, gameDate attributes for games are in ISO8601 with UTC, so a comparison like this should work https://pastebin.com/weYQkZRa. Keep in mind the code is untested, but it seems fine to me from a technical point of view.

toddrob99 commented 3 years ago

I finally had a chance to look into this in more detail, and you were right about using reverse index to find the last game. The missing piece of the puzzle was filtering out games that are not Final. I committed and fix that I'll publish in v1.3 once I get next_game working too. Thank you for submitting this and helping with the logic, even though your PR ultimately didn't get merged.