baseballr icon indicating copy to clipboard operation
baseballr copied to clipboard

Fangraphs Data No longer available

Open Ic4ru5Wing opened this issue 1 year ago • 14 comments

As of today (13 May 2025), Fangraphs functions now producing a Request failed [403] error with the following as an example message:

2025-05-13 20:53:27.00607: Invalid arguments or no player pitching leaders data available! Error in fg_pitcher_leaders(qual = 0, startseason = year, endseason = year, : object 'leaders' not found

This is now an issue with all Fangraphs datasets

Having contacted David Appleton from the website, they have apparently implemented new checks recently, but said this should not affect access or site usage.

However, he has not provided any further details or help

I hope this is of some use in resolving the matter

Regards A S Wilson

Ic4ru5Wing avatar May 13 '25 20:05 Ic4ru5Wing

I think it's a version issue of some form. I had baseballr on two devices, one runnin R version 4.2, the other version 4.4. I updated the 4.2 version to 4.5, and the issue resolved.

If that doesn't work, I had the following function replicate what pitcher_leaders did. I think I pulled it from another issue thread:

fg_pitcher_leaders1 <- function(age = "", pos = "all", stats = "pit", lg = "all", qual = "0", startseason = "2023", endseason = "2023", startdate = "", enddate = "", month = "0", hand = "", team = "0", pageitems = "10000", pagenum = "1", ind = "0", rost = "0", players = "", type = "8", postseason = "", sortdir = "default", sortstat = "WAR"){

params <- list( age = age, pos = pos, stats = stats, lg = lg, qual = qual, season = startseason, season1 = endseason, startdate = startdate, enddate = enddate, month = month, hand = hand, team = team, pageitems = pageitems, pagenum = pagenum, ind = ind, rost = rost, players = players, type = type, postseason = postseason, sortdir = sortdir, sortstat = sortstat )

url <- "https://www.fangraphs.com/api/leaders/major-league/data"

fg_endpoint <- httr::modify_url(url, query = params)

tryCatch( expr = {

  resp <- fg_endpoint %>% httr::GET()
  fg_df <- resp$data %>% jsonlite::toJSON() %>% jsonlite::fromJSON(flatten = TRUE)
  fg_df <- resp %>%
    httr::content(as = "text") %>%
    jsonlite::fromJSON(flatten = TRUE) %>%
    as.data.frame()  
  
  c <- colnames(fg_df)
  c <- sub("^data\\.", "", c)
  c <- gsub("%", "_pct", c, fixed = TRUE)
  c <- gsub("/", "_", c, fixed = TRUE)
  c <- ifelse(substr(c, nchar(c) - 1 + 1, nchar(c)) == ".", gsub("\\.", "_pct", c), c)
  c <- gsub(" ", "_", c, fixed = TRUE)
  colnames(fg_df) <- c
  leaders <- fg_df %>% 
    dplyr::rename_with(~ gsub("pi", "pi_", .x), starts_with("pi")) %>% 
    dplyr::rename_with(~ gsub("pfx", "pfx_", .x), starts_with("pfx")) %>%
    dplyr::rename(
      "Start.IP" = "Start.IP",
      "Relief.IP" = "Relief.IP",
      "WPA_minus" = ".WPA",
      "WPA_plus" = "WPA", 
      "AgeRng" = "AgeR",
      "team_name" = "TeamName",
      "team_name_abb" = "TeamNameAbb") %>%
    dplyr::select(-dplyr::any_of(c(
      "Name", 
      "Team"
    ))) %>%
    dplyr::select(
      "Season",
      "team_name",
      "Throws", 
      "xMLBAMID", 
      "PlayerNameRoute",
      "PlayerName",
      "playerid",
      "Age",
      "AgeRng",
      tidyr::everything())
  
},
error = function(e) {
  message(glue::glue("{Sys.time()}: Invalid arguments or no player pitching leaders data available!"))
},
finally = {
}

) return(leaders) }

tmr621 avatar May 19 '25 17:05 tmr621

Thank you for the help, very much appreciated. I will go and have a look to see if I can get it to work now

Ic4ru5Wing avatar May 19 '25 17:05 Ic4ru5Wing

Unfortunately, after updating to the latest version of R and running the amended function above, the issue persists. It appears to be a fundamental problem with how the data is now structured on the Fangraphs website

Ic4ru5Wing avatar May 19 '25 19:05 Ic4ru5Wing

Which R version are you using? If it's working for other people, I doubt it's an issue on FG's side.

tmr621 avatar May 19 '25 20:05 tmr621

I suspect you are right as I am largely self-taught an clearly not an expert. I am using the latest R version 4.50 for the Mac. Not sure if that makes a difference but will need to try and figure out what the issue is

Thanks again for the help

Ic4ru5Wing avatar May 19 '25 21:05 Ic4ru5Wing

Have you restarted your device? Maybe it's a caching issue.

tmr621 avatar May 19 '25 22:05 tmr621

I think the issue is with the dummy running the code! 😂

Ic4ru5Wing avatar May 19 '25 22:05 Ic4ru5Wing

I definitely can't seem to get it to work for me unfortunately. I think the url may be incorrect as when you try to go to "https://www.fangraphs.com/api/leaders/major-league/data" the page does not exist and the function returns object 'leaders' not found. The 'api' section of the web address may be the issue because the leaders stats is at the address: (https://www.fangraphs.com/leaders/major-league) but, again, I'm no expert

Ic4ru5Wing avatar May 20 '25 17:05 Ic4ru5Wing

@Ic4ru5Wing do you know what an API is? You might want to read up on them. What the function does is query the database using fangraph's API. The URL in the package is correct.

For example, if you add parameters the your query, the API url will display the information you want an example here.

Can you update all your packages, then run the R.version command and put the output here?

tmr621 avatar May 20 '25 18:05 tmr621

Sorry, I probably look like a fool but thanks for trying to help. It's much appreciated

Yeah I do know what an API is but I'm a novice when it comes to using the likes of R to access them. What I'm really just trying to do is teach my son to code using baseball data and obviously not doing a great job of it! For whatever reason it only seems to be the FanGraphs functions that have stopped working and it's clearly an issue at my end.

I have updated everything as far as I can see and it still persists.

Here is an example we use when trying to scrape team batting stats:

team_batting <- fg_team_batter(startseason = 2025, endseason = 2025, qual = 0,)

and the output:

Request failed [403]. Retrying in 1 seconds... Request failed [403]. Retrying in 1.7 seconds... 2025-05-21 08:05:58.827791: Invalid arguments or no team batting data available! Error in fg_team_batter(startseason = 2025, endseason = 2025, qual = 0, : object 'leaders' not found

We even get the same issue if we try to run the examples within the R help files

Ic4ru5Wing avatar May 21 '25 07:05 Ic4ru5Wing

If you open the link in my comment, do you see data or are you getting an error page? Do you have a firewall/VPN/security settings that may be impacting your access to fangraphs.com? Getting a 403 error seems odd. If you're having errors across all fangraphs functions, it may be your access to the site that's messed up.

Have you tried removing then reinstalling the baseballr package?

tmr621 avatar May 21 '25 21:05 tmr621

If you mean where you have posted 'an example here' above, regarding the API then yeah I see the page with pretty print but it's completely unformatted. I don't have a VPN but I switched off my firewall to test it and it was still the same. I really can't understand what's going on at all. I also uninstalled and reinstalled baseballr and it made no difference. It seems to be since they made changes to the site. I also now regularly get the check to prove I'm human when I visit the site which didn't happen before. I'm starting to wonder if it's because I'm actually in the UK but when I contacted David Appleton I explained that to him and he didn't say that was an issue. Although he didn't really say much at all otherwise. Anyway, thanks again for the help but I'm now reaching the stage of giving up with it as I don't really know what else to do

Ic4ru5Wing avatar May 21 '25 22:05 Ic4ru5Wing

Can you check again what version of R you're using? You can do that with the R.version command. It helps with troubleshooting.

Alternatively, if you're trying to teach your son to code, check out some of the other packages related to sports. A good starting point is sportsdataverse

tmr621 avatar May 21 '25 23:05 tmr621

Thanks, I will check that out. This is the output from the R.version command:

R.Version() $platform [1] "aarch64-apple-darwin20"

$arch [1] "aarch64"

$os [1] "darwin20"

$system [1] "aarch64, darwin20"

$status [1] ""

$major [1] "4"

$minor [1] "5.0"

$year [1] "2025"

$month [1] "04"

$day [1] "11"

$svn rev [1] "88135"

$language [1] "R"

$version.string [1] "R version 4.5.0 (2025-04-11)"

$nickname [1] "How About a Twenty-Six"

Ic4ru5Wing avatar May 22 '25 06:05 Ic4ru5Wing

Fixed in baseballr 2.0.0 (PRs #403 + #405).

As of 2026-06-03 FanGraphs serves a Cloudflare JS challenge (HTTP 403, cf-mitigated: challenge) to every unrecognized client — including the plain library User-Agent that previously worked, and no header/TLS tweak passes it (it needs a JS runtime). The challenge exempts the okhttp client the FanGraphs mobile app uses, so the FanGraphs requests now send User-Agent: okhttp/4.12.0 and get HTTP 200 with normal data again.

A dedicated internal fg_api_call() helper (okhttp UA) carries the leaders/team functions (fg_batter_leaders(), fg_pitcher_leaders(), fg_fielder_leaders(), fg_team_batter(), fg_team_pitcher(), fg_team_fielder()); the game-log functions (fg_batter_game_logs(), fg_pitcher_game_logs(), fg_milb_batter_game_logs(), fg_milb_pitcher_game_logs()) and the .aspx scrapers send the same UA. All FanGraphs functions return data again.

Please reinstall from master:

remotes::install_github("BillPetti/baseballr")

Closing as resolved — please reopen (or file a fresh issue) if you still see a 403 after reinstalling.

saiemgilani avatar Jun 08 '26 10:06 saiemgilani