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

ValueError: Could not find a game with that id. #52

Open
Hisairnessag3 opened this issue Jan 17, 2018 · 21 comments
Open

ValueError: Could not find a game with that id. #52

Hisairnessag3 opened this issue Jan 17, 2018 · 21 comments

Comments

@Hisairnessag3
Copy link

Running the same code as before:

def team_games(year,month):
    month = mlbgame.games(year, month)
    games = mlbgame.combine_games(month)
    for game in games:
        print game.game_id
    return games


games = team_games(2016, 5)


and its breaking on random game ids when iterating through.

This one in particular breaks:

stats = mlbgame.team_stats('2016_05_16_bosmlb_kcamlb_1')
era = stats.home_pitching.era
print(era)

Status is also FINAL

@trevor-viljoen
Copy link
Contributor

trevor-viljoen commented Jan 17, 2018

Hmm... That status should be "OTHER".

<sg_game>
  <game id="2016_05_16_bosmlb_kcamlb_1" league="AA" status="OTHER" start_time="7:05PM" home_code="">
    <delay_reason>Rain</delay_reason>
  </game>
  <team name="Royals" code="">
    <gameteam R="0" H="0" E="0"/>
  </team>
  <team name="Red Sox" code="">
    <gameteam R="0" H="0" E="0"/>
  </team>
</sg_game>

I think the problem is that it can't find boxscore.xml, not that it can't find the id. That function probably needs to check the scoreboard for the day and if the status is "OTHER", raise a different error about no stats being available for that game.

@Hisairnessag3
Copy link
Author

Whoops sorry, that one also breaks.

Here is the id I used: 2016_05_16_minmlb_detmlb_1

@trevor-viljoen
Copy link
Contributor

@Hisairnessag3
@panzarino
It looks like MLB is actively making changes away from the old gd2.mlb.com way of doing things. I'll submit another bug fix for URLs, but some thought needs to be put into raising errors for games that were scheduled, but never played. There is a slight change to how URLs are being crafted for the new gd-terr-origin.mlb.com domain.

@Hisairnessag3
Copy link
Author

Gotcha, I appreciate your help here. I'd be open to helping you out with that in the future

@trevor-viljoen
Copy link
Contributor

@panzarino Perhaps tag this for a v3 release.

It would probably also be wise to just make use of the MLB stats API to access the boxscore and any of the other endpoints we have custom code for, rather than dealing with these URLs that may or may not continue to change.

For example: https://statsapi.mlb.com/api/v1/schedule?sportId=1&date=05%2F16%2F2016 will give you the schedule for 05/16/2016 as a json payload.

Each game has a game_pk which you can use to get the boxscore. In your example, 2016_05_16_minmlb_detmlb_1, the game_pk is 447440.

The boxscore for that game is: https://statsapi.mlb.com/api/v1/game/447440/boxscore

boxscore endpoint documentation: https://statsapi.mlb.com/docs/#!/game/boxscore

@Hisairnessag3
Copy link
Author

Hmm still breaking here: 2016_04_02_milmlb_blxaax_1 15

@trevor-viljoen
Copy link
Contributor

That's some weird spring training game. The actual boxscore is here: http://gd-terr-origin.mlb.com/components/game/aax/year_2016/month_04/day_02/gid_2016_04_02_milmlb_blxaax_1/boxscore.xml and not under the expected game/mlb location. More logic would have to be added to read the scoreboard xml and get the game_data_directory attribute in the game tag.

@panzarino
Copy link
Owner

@trevor-viljoen @Hisairnessag3 I don't really have much time to maintain this library myself, but I would really be open to PRs if you guys are interested in joining the project.

@ajbowler
Copy link
Contributor

I ran into this yesterday while trying to cycle through a list of games for a random day in 2017.

It's probably worth logging a new issue for migrating over to the Stats API (if it's ready and supports all of mlbgame's current features) and bumping the major version as @trevor-viljoen suggested above.

For now we could probably just return None instead of throwing the error?

@trevor-viljoen
Copy link
Contributor

The boxscore location is information we currently have available (overview.game_data_directory). I haven't found a way to access it in the correct context, because it ends up causing circular imports.

@panzarino
Copy link
Owner

@ajbowler I feel like returning None wouldn't really change the functionality. In that case, you would still have to test if the response is None, where in this case you can just try/catch so I don't really see the advantage in changing to None other than avoiding the error.

@trevor-viljoen
Copy link
Contributor

trevor-viljoen commented Feb 1, 2018

@ajbowler So, there's a catch 22 here with retrieving the game data if the game isn't a common MLB team v MLB team game. You need the overview in order to get the game_data_directory attribute and you need the game_data_directory attribute to get the overview URL. I'm not sure how to proceed here. I'll have to do some more digging/thinking. Hopefully others have some ideas. The stats API would obsolete the need, but would require a rewrite. So that's likely a major release, perhaps v3.0.0 working off of a beta branch. That's up to @panzarino though.

@panzarino
Copy link
Owner

@trevor-viljoen I would be good with upgrading to the new stats API, but I feel like now that there is an easy API this service is useless. I built it because it was difficult to parse the xml and organize it, but a json API greatly reduces the need for the middle man.

@ajbowler
Copy link
Contributor

ajbowler commented Feb 2, 2018

It depends if the new API has all the existing data that this module needs. If it doesn't, then it ain't ready to migrate IMO but that's your call. If it does have everything, then I still find a huge value out of this module because it allows a nice abstraction away from hitting the API directly and allowing consumers to just stick to their program.

As it is, this library is nice because it parses the XML files into nice and easy objects without discarding the data. If it moves to the new JSON API, the library could definitely be slimmed down in its implementation, but I still see the benefit of having a ready-to-go Python wrapper.

@trevor-viljoen
Copy link
Contributor

@panzarino I agree with @ajbowler. This module still serves a purpose in the python community, wrapping the JSON API into python objects. It reduces unnecessary code and allows one to just import an easy to use python module which neatly handles all of the dirty work. I wouldn't want to write redundant code every time I wanted to go out and pull JSON from MLB. You've done really great work here.

Tonight, I started work on wrapping the JSON API. If you want to open a v3.0.0-beta branch, I'd be happy to open a PR once it is a bit more mature and has a few tests. To me, it makes more sense to keep it as a future release of mlbgame than to split it off as a separate project if you'd be open to having it.

Below is an example of some of the data returned by the playByPlay endpoint.

    {
      "startIndex": 53,
      "endIndex": 58,
      "top": [
        53,
        54,
        55
      ],
      "bottom": [
        56,
        57,
        58
      ],
      "hits": {
        "away": [
          {
            "team": {
              "id": 142,
              "name": "Minnesota Twins",
              "link": "/api/v1/teams/142",
              "springLeague": {
                "id": 115,
                "name": "Grapefruit League",
                "link": "/api/v1/league/115",
                "abbreviation": "GL"
              }
            },
            "inning": 6,
            "pitcher": {
              "id": 519455,
              "fullName": "Jordan Zimmermann",
              "link": "/api/v1/people/519455"
            },
            "batter": {
              "id": 435559,
              "fullName": "Kurt Suzuki",
              "link": "/api/v1/people/435559"
            },
            "coordinates": {
              "x": 111.68,
              "y": 148.02
            },
            "type": "O",
            "description": "Groundout"
          },
          {
            "team": {
              "id": 142,
              "name": "Minnesota Twins",
              "link": "/api/v1/teams/142",
              "springLeague": {
                "id": 115,
                "name": "Grapefruit League",
                "link": "/api/v1/league/115",
                "abbreviation": "GL"
              }
            },
            "inning": 6,
            "pitcher": {
              "id": 519455,
              "fullName": "Jordan Zimmermann",
              "link": "/api/v1/people/519455"
            },
...

pitchData is also included, which would be cool to provide so people can plot it with matplotlib or something like they do with MLB Game Day.

{
          "details": {
            "call": {
              "code": "B",
              "description": "Ball"
            },
            "description": "Ball",
            "ballColor": "rgba(39, 161, 39, 1.0)",
            "trailColor": "rgba( 0, 85, 254, 1.0)",
            "isInPlay": false,
            "isStrike": false,
            "isBall": true,
            "type": {
              "code": "CH",
              "description": "Changeup"
            },
            "hasReview": false
          },
          "count": {
            "balls": 3,
            "strikes": 1
          },
          "pitchData": {
            "startSpeed": 85.8,
            "endSpeed": 79.7,
            "nastyFactor": 46.0,
            "strikeZoneTop": 3.35,
            "strikeZoneBottom": 1.65,
            "coordinates": {
              "aY": 23.581,
              "aZ": -25.85,
              "pfxX": -6.73,
              "pfxZ": 3.87,
              "pX": -1.267,
              "pZ": 2.619,
              "vX0": 4.123,
              "vY0": -125.748,
              "vZ0": -2.214,
              "x": 165.3,
              "y": 168.07,
              "x0": -2.047,
              "y0": 50.0,
              "z0": 5.591,
              "aX": -10.858
            },
            "breaks": {
              "breakAngle": 21.1,
              "breakLength": 7.1,
              "breakY": 23.8
            }
          },
          "index": 5,
          "pfxId": "160516_194130",
          "playId": "379b9c0d-50e9-4908-8cde-109c7290b9e6",
          "pitchNumber": 4,
          "startTime": "2016-05-16T23:37:42.000Z",
          "endTime": "2016-05-16T23:37:59.000Z",
          "isPitch": true,
          "type": "pitch"
        },

@ajbowler
Copy link
Contributor

ajbowler commented Feb 2, 2018

@trevor-viljoen do you have a link to their new API? I've been unable to find anything other than a StatsAPI login that doesn't give an option to register.

@panzarino
Copy link
Owner

@trevor-viljoen Great! I'll open the branch soon.

@trevor-viljoen
Copy link
Contributor

@ajbowler
Copy link
Contributor

ajbowler commented Feb 3, 2018

@trevor-viljoen Thanks, that link works.

The docs are really bare bones on there and this API looks really WIP at the moment, but if they flesh this out more then having a JSON API would be awesome. I tried something simple like the standings endpoint but can't figure it out. What the hell is a "league ID"?

I've just added displaying MLB standings as a feature to my LED scoreboard. If that endpoint can be figured out, that might be one of the easier ones to start migrating over to.

@panzarino
Copy link
Owner

@ajbowler Check out the current league() function, I'm pretty sure that it includes an id somewhere for the league, and that is probably the same id.

@trevor-viljoen
Copy link
Contributor

@ajbowler division and league IDs can be found here: https://github.com/panzarino/mlbgame/blob/master/mlbgame/info.py#L192-L219

I don't think the standings API is hot yet though. Try schedule: https://statsapi.mlb.com/api/v1/schedule?sportId=1&season=2017&date=07%2F04%2F2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants