Advertisement
RemcoE33

baseballsavant.mlb.com

Mar 8th, 2023
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Get Park Factors Leaderboard data from baseballsavant.mlb.com
  3. *
  4. * @param {2022} year The year filter.
  5. * @param {true} headers Return the headers.
  6. * @return {array} Park Factors Leaderboard data.
  7. * @customfunction
  8. */
  9. function BBSAVANT(year = 2022, headers = true) {
  10.   const url = `https://baseballsavant.mlb.com/leaderboard/statcast-park-factors?type=year&year=${year}&batSide=&stat=index_wOBA&condition=All`
  11.   const request = UrlFetchApp.fetch(url)
  12.   const html = request.getContentText()
  13.   let data
  14.   console.log(html)
  15.  
  16.   try {
  17.     const json = /var data = (.*?);/gmsi.exec(html)
  18.     console.log(json)
  19.     data = JSON.parse(json[1])
  20.   } catch (e) {
  21.     throw new Error("Error getting data")
  22.   }
  23.  
  24.   const results = [];
  25.  
  26.   data.forEach((row, i) => {
  27.     if (i === 0 && headers) {
  28.       results.push(Object.keys(row))
  29.     }
  30.  
  31.     results.push(Object.values(row))
  32.   })
  33.  
  34.   return results
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement