toddrob99 / MLB-StatsAPI

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

Player Stats Error #133

Closed kid-coding closed 9 months ago

kid-coding commented 9 months ago

Getting this error on player stats

{"timestamp":"2023-12-14T01:41:18.548+0000","status":404,"error":"Not Found","path":"/api/%7Bver%7D/stats/leaders"}

toddrob99 commented 9 months ago

What code are you running to get that error? The ver parameter is not being replaced correctly.

kid-coding commented 9 months ago

What is the ver parameter and would I have to replace that each time?

Sorry a novice

toddrob99 commented 9 months ago

Don't worry about the ver parameter for now. What code are you running or what are you doing when you get the error?

kid-coding commented 9 months ago

I'm running stats endpoint

toddrob99 commented 9 months ago

I cannot help you based on the amount of information provided. I assume you are running Python code since this is a Python api wrapper, so what code are you running to pull data from the stats endpoint?

kid-coding commented 9 months ago

import requests, json

url = "URL: https://statsapi.mlb.com/api/{ver}/stats" result = requests.get(url)

stat = result.json() print(json.dumps(stat, indent=4))

toddrob99 commented 9 months ago

Oh, you're not using the MLB-StatsAPI module. You just took the URL from the endpoint configuration and you're trying to retrieve it manually. That's fine if that's what you want to do, but this module is designed to handle the boring stuff for you.

If you do not want to use the module and you just want to use the API info that's documented here, then refer to the endpoint listing on the wiki to get the URLs and parameters for each endpoint. You'll need to replace any parameters that appear in the URL in curly brackets, such as {ver} with a value for that parameter. For {ver} you should use v1 in almost all cases. The game endpoint should be v1.1. For the parameters that are not listed in the URL, you should add them to the URL as query parameters, such as the teamId field. Query parameters are added to the end of the URL, separated with a ? for the first one and then & for each additional one. For example: ?teamId=143 or ?teamId=143&sportId=1.

If you want to use this API wrapper, then first you need to install the module:

pip install mlb-statsapi (or python -m pip install mlb-statsapi depending on your environment setup)

Then you can do something like this:

import statsapi

params = {
    "season": 2023,
    "stats": "season",
    "group": "hitting",
    "sortStat": "homeRuns",
    "order": "desc",
    "gameType": "R",
    "sportIds": 1,
}
stats = statsapi.get("stats", params)

print(json.dumps(stats, indent=4))

That is an example of pulling data directly from an API endpoint (the stats endpoint in that case). There are also some built-in functions to further abstract away the boring API details.

That will give you the 2023 regular season hitter stats sorted by home run total.

There are other examples in the wiki and on the subreddit. You can also post questions on the subreddit where there are more people who can help.

kid-coding commented 9 months ago

Oh that's super helpful man! Thank you!

How would I go about setting up the program to call those stats with an input request without having to replace the parameter each time manually in the code?

toddrob99 commented 9 months ago

There are many ways you could do that, but they're way outside the scope of this repository. Creating a web interface with flask is probably one of the easiest and most popular options.

kid-coding commented 9 months ago

Thank you