Reputation: 47
Last year I was provided with IMPORTXML
so I can get live scoring updates to Google Sheets. I went to use it today to prep for 2018 and it's still pulling last year's data. I'm not familiar how I can get the information needed for this year, if it's changed or if it's just too soon. I was hoping someone with a little more knowledge could maybe guide me or point e in the right direction?
=importxml("http://www.nfl.com/liveupdate/scorestrip/ss.xml","//g/@eid" )
This was one of the fields I used, and it's still pulling 2017. Thanks!
Upvotes: 0
Views: 2401
Reputation: 37
Here's my script. It works well. Don't forget to set up the script triggers to auto run it each minute or however frequently you wish.
function GetNFLScore(){
var url = ('http://www.nfl.com/liveupdate/scores/scores.json');
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
var bye = "";
// Extracts the keys (these change every week)
var keys = [];
for(var k in json) keys.push(k);
// Declare array for results
var NFLResults =[];
// Make a Title Row
NFLResults.push(["Qtr","Time","Home","Pts","Away","Pts","Pos.","Down","To Go","Yard Line","Note","Network"]);
// Extracts the Games one per line
for(n=0; n<keys.length; n++) {
var Clock = json[keys[n]]["clock"]; // Kickoff time if pregame, Game Clock if game time
var Qtr = json[keys[n]]["qtr"]; // Gamestate (Pre, or Final), Game quater if game time
var Home = json[keys[n]]["home"]["abbr"]; // Home Team
var HsT = json[keys[n]]["home"]["score"]["T"]; // Home Total Score
var Away = json[keys[n]]["away"]["abbr"]; // Away Team
var AsT = json[keys[n]]["away"]["score"]["T"]; // Away Total Score
var TV = json[keys[n]]["media"]["tv"]; // TV Network (NBC, FOX, ESPN, ect..)
var YardLine = json[keys[n]]["yl"]; // Current scrimage yard line
var PosTeam = json[keys[n]]["posteam"]; // Team with ball possession
var RedZone = json[keys[n]]["redzone"]; // Redzone boolean
var Down = json[keys[n]]["down"]; // Current Down
var ToGo = json[keys[n]]["togo"]; // Current To Go yardage for 1st down
var Note = json[keys[n]]["note"]; // Scoring play (Touchdown, Field Goal, Extra Point, Safety)
// Correcting team abbr
if( Home == "JAC" ){ Home = "JAX";} // JAC > JAX
else if( Home == "WAS" ){ Home = "WSH";} // WAS > WSH
else if( Home == "LA" ){ Home = "LAR";} // LA > LAR
if( Away == "LA" ){ Away = "LAR";}
else if( Away == "WAS" ){ Away = "WSH";}
else if( Away == "JAC" ){ Away = "JAC";}
// Clear non-score related game data if game is final
if( Qtr == "Final" ){
Clock = "";
PosTeam = "";
RedZone = "";
Down = "";
ToGo = "";
Note = "";
TV = "";
}
else if( Qtr == "Final OT" ){
Clock = "";
PosTeam = "";
RedZone = "";
Down = "";
ToGo = "";
Note = "";
TV = "";
}
// Display Game data
NFLResults.push([Qtr,Clock,Home,HsT,Away,AsT,PosTeam,Down,ToGo,YardLine,Note,TV]);
}
// Clear the bottom of the scoreboard from lack of scores caused by bye weeks
var rows = NFLResults.length
for(n=0; n<(17-rows); n++) {
NFLResults.push([bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye]); // N'SYNC
}
// Create new tab 'NFL Scores' if it doesn't exist.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var itt = ss.getSheetByName('NFL Scores');
if (!itt) {
ss.insertSheet('NFL Scores');
}
// Push Scoreboard to spreadsheet
SpreadsheetApp.getActive().getSheetByName('NFL Scores').getRange(1,1,17,12).setValues(NFLResults);
}
Upvotes: 0
Reputation: 2291
Looks like that XML service is discontinued or not so easy to find.
However this site is a JSON equivalent I believe.
http://www.nfl.com/liveupdate/scores/scores.json
and this site gives live data also and you can choose by date.
http://www.nfl.com/liveupdate/game-center/2018090600/2018090600_gtd.json
After staring at the output from the first link, it looks like you can get the matches in play and scheduled from that, and then use the references to select a particular match for the full play-by-play details in the second link.
Below is a function you can use http://www.nfl.com/liveupdate/scores/scores.json to get the results into Google-sheets and present with selected fields.
To install and use it read the instructions here: https://developers.google.com/apps-script/guides/sheets/functions#using_a_custom_function
In your sheet pick a cell and enter = ReadNFLResults()
To run the script periodically (say every 2 minutes), follow these instructions https://www.quora.com/How-can-I-periodically-run-a-Google-Script-on-a-Spreadsheet
/** Imports NFL results to your spreadsheet Ex: ReadNFLResults()
* @customfunction
*/
function ReadNFLResults(){
try{
var res = UrlFetchApp.fetch("http://www.nfl.com/liveupdate/scores/scores.json");
var content = res.getContentText();
var json = JSON.parse(content);
// Extracts the keys (these change every week)
var keys = [];
for(var k in json) keys.push(k);
// Declare array for results
var NFLResults =[];
// Make a Title Row
NFLResults.push(["Date","Stadium","Time","Qtr","Home Team","Hs1","Hs2","Hs3","Hs4","HsT","Vis Team","As1","As2","As3","As4","AsT"]);
// Extracts the Games one per line
for(n=0; n<keys.length; n++){
var Date = ""+keys[n];
var Stadium = json[keys[n]]["stadium"];
var Clock = json[keys[n]]["clock"];
var TV = json[keys[n]]["media"]["tv"];
var Qtr = json[keys[n]]["qtr"];
var Home = json[keys[n]]["home"]["abbr"];
var Hs1 = json[keys[n]]["home"]["score"]["1"];
var Hs2 = json[keys[n]]["home"]["score"]["2"];
var Hs3 = json[keys[n]]["home"]["score"]["3"];
var Hs4 = json[keys[n]]["home"]["score"]["4"];
var HsT = json[keys[n]]["home"]["score"]["T"];
var Away = json[keys[n]]["away"]["abbr"];
var As1 = json[keys[n]]["away"]["score"]["1"];
var As2 = json[keys[n]]["away"]["score"]["2"];
var As3 = json[keys[n]]["away"]["score"]["3"];
var As4 = json[keys[n]]["away"]["score"]["4"];
var AsT = json[keys[n]]["away"]["score"]["T"];
NFLResults.push([Date,Stadium,Clock,Qtr,Home,Hs1,Hs2,Hs3,Hs4,HsT,Away,As1,As2,As3,As4,AsT]);
}
// Return array to Google-Sheets
return (NFLResults);
}
catch(err){
return "Error getting data";
}
}
Here is sample results as of Sept 10, 2018:
Upvotes: 2
Reputation: 51
Try this:
http://www.nfl.com/ajax/scorestrip?season=2018&seasonType=REG&week=1
Looks like you have to specify the querystring parameters for season and type and week but looks like the output might be the same.
Upvotes: 5