loisaidasam / the-masters-api

The Masters API!
MIT License
10 stars 2 forks source link

Null values #2

Closed TeeJay942 closed 5 years ago

TeeJay942 commented 5 years ago

On day 2 (Friday morning) of the Masters, API is showing null values for "player" and "to_par". Using this to keep track of a Masters Draft Scoring for a group of guys. Please help and thanks!

earnjam commented 5 years ago

Looks like ESPN added a movement column in front of the player name and it's throwing off the scraper.

earnjam commented 5 years ago

image

TeeJay942 commented 5 years ago

Ahh that makes sense. Are you able to fix it? Yesterday was my first time doing anything with an API, so not sure I would be able to help.

earnjam commented 5 years ago

Just opened a PR (#3) that fixes it. Will need @loisaidasam to update on his end if you're using his hosted API.

BTW, this thing is fun. I'm using it to auto-update stats in a Google Sheet for a little fantasy Masters challenge with some coworkers.

TeeJay942 commented 5 years ago

Awesome; I see the pull request. Thanks! Hope it gets accepted soon. I'm updating Google Sheets as well. Everyone is excited about the sheet.

TeeJay942 commented 5 years ago

Are you not using his hosted API?

earnjam commented 5 years ago

Yeah, I am using his hosted version too.

TeeJay942 commented 5 years ago

I struggled at first trying to set up the auto-updating trigger on a function from inside the cells, but realized the only way to get the refresh is to have the script editor update the cells. So the data automatically refreshes once a minute.

earnjam commented 5 years ago

Yep, same setup here. Much easier to put that kind of stuff in a script. This is what I did:

function setLeaderboard() {
  var leaderboard = JSON.parse(UrlFetchApp.fetch("http://samsandberg.com/themasters/"));
  var players = leaderboard.players.map( function(obj){
    return Object.keys(obj).map(function(key) {
      return obj[key];
    })
  });
  var leaderboardSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Leaderboard');
  leaderboardSheet.getRange( 2, 1, leaderboard.players.length, 12).setValues(players);
}

There is probably a cleaner way to turn the player objects into arrays for inserting, but hey...it works 😀

TeeJay942 commented 5 years ago

Looks good. Here is what I did:

function JSONInitNames() {

  var res = UrlFetchApp.fetch("http://samsandberg.com/themasters/");
  var content = res.getContentText();

  var json = JSON.parse(content);
  var players = json["players"];

  for(var i=0;i<players.length;i++){

    var thisplayer = players[i]
    var playername = thisplayer["player"]
    var playerscore = thisplayer["to_par"]
    var thruhole = thisplayer["thru"]
    var round1 = thisplayer["r1"]
    var round2 = thisplayer["r2"]
    var round3 = thisplayer["r3"]
    var round4 = thisplayer["r4"]

  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  targetSheet.getRange(i+15, 8).setValue(playername)
  targetSheet.getRange(i+15, 9).setValue(playerscore)
  targetSheet.getRange(i+15, 10).setValue(thruhole)
  targetSheet.getRange(i+15, 11).setValue(round1)
  targetSheet.getRange(i+15, 12).setValue(round2)
  targetSheet.getRange(i+15, 13).setValue(round3)
  targetSheet.getRange(i+15, 14).setValue(round4)

  }

}
loisaidasam commented 5 years ago

Thanks for the heads up. 👍

A few other column updates needed to be made, which I handled.

All set and deployed to http://samsandberg.com/themasters/