I am trying to import live NASCAR Race Results to Google Sheet. I am open to an App Script or even trying to figure out the IMPORTXML, with XPath. From my previous experience, IMPORTHTML will not work, but if someone knows of another way, let me know.
https://www.nascar.com/results/racecenter/2022/nascar-cup-series/...LATEST_RACE_NAME.../stn/live
For the IMPORTXML, I have the XPath of the element [@id="pgc-361475-1-0"]/div/div/div[7]/div[1] ... The problem is that the element changes by a table header that is selected (i.e., Qualifying, Practice 1, Practice 2, Entry, etc.) The default will return "Practice 1", but that is it, I can't get anything else to send an of the other live tables until after the race and it switches from Results to Recap.
I have also tried a Regex App Script see code below. Again it will only send back the Practice 1 table. I don't know how to reference or "Select" the other elements so that the right table will come back. When the Race Starts, there is a Header Tab of "Race" I want to pull that tables position with Driver Name, Car No, and Position.
function myFunction() {
clearRecords();
var url = "https://www.nascar.com/results/racecenter/2022/nascar-cup-series/firekeepers-casino-400/stn/live";
var str = UrlFetchApp.fetch(url).getContentText();
const mainRegex = /<span class=\"race-center-driver-car-number\">([\s\S]*?)<\/span>/gi;
var results = str.match(mainRegex);
const driverPass1 = /(?<=<span class=\"race-center-driver-car-number\">).*?(?=<\/span>)/gi;
for(var i = 0; i < 40; i )
{
Logger.log('content: ' results[i]);
var driver1String = results[i].match(driverPass1);
Logger.log('content: ' driver1String[0]);
addRecord(i 1, driver1String[0]);
}
}
function clearRecords()
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var tableSheet = ss.getSheetByName("TABLE");
tableSheet.getRange("A1:D40").clear();
}
function addRecord(count, driver, logo) {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var tableSheet = ss.getSheetByName("TABLE");
var currentRow = tableSheet.getLastRow();
var nextRow = currentRow 1;
tableSheet.getRange(nextRow,1).setValue(count);
tableSheet.getRange(nextRow,2).setValue(driver);
}
CodePudding user response:
You can get the raw data from the api urls. All the urls are in the source file. This will set you up together with this video
See the data in your browser. Just put one of the .json
urls in. With this addon things will be formatted neatly.
function getLiveFeed(url) {
//This is in the sourcecode of the website
// var raceCenterGlobals = {
// 'raceURL': 'https://cf.nascar.com/cacher/2022/1/5170/weekend-feed.json',
// 'pointsURL': 'https://cf.nascar.com/live/feeds/series_1/5170/live_points.json',
// 'liveRaceURL': 'https://cf.nascar.com/live/feeds/series_1/5170/live_feed.json',
// 'stagePointsURL': 'https://cf.nascar.com/cacher/2022/1/5170/live-stage-points.json',
// 'snappyTVURL': 'https://cf.nascar.com/cacher/2022/1/5170/snappytv.json',
// 'keyMomentsURL': 'https://cf.nascar.com/cacher/2022/1/5170/lap-notes.json',
// 'lapTimesURL': 'https://cf.nascar.com/cacher/2022/1/5170/lap-times.json',
// 'driverRecapURL': 'https://cf.nascar.com/cacher/2022/1/5170/driver-recap.json',
// 'lapAveragesURL': 'https://cf.nascar.com/cacher/2022/1/5170/lap-averages.json',
// 'lapFastURL': 'https://cf.nascar.com/loopstats/prod/2022/1/5170.json',
// 'pitStopDataURL': 'https://cf.nascar.com/cacher/live/series_1/5170/live-pit-data.json',
// 'liveFlagDataURL': 'https://cf.nascar.com/live/feeds/live-flag-data.json',
// 'scheduleFeedURL': 'https://cf.nascar.com/cacher/2022/race_list_basic.json',
// 'weekendScheduleFeedURL': 'https://cf.nascar.com/cacher/2022/1/schedule-feed.json',
// 'season': '2022',
// 'raceId': '5170',
// 'series': '1',
// 'selTabName': 'live',
// 'selSubTabName': ''
// }
const raceCenterGlobalsRequest = UrlFetchApp.fetch(url)
const html = raceCenterGlobalsRequest.getContentText()
const raceCenterString = /var raceCenterGlobals = {(.*?)};/gms.exec(html)[1]
const raceCenterObject = raceCenterString.split(',').reduce((acc, curr) => {
const [key, value] = curr.split("' :").map(e => e.replace(/'/g,"").trim())
acc[key] = value
return acc
}, {})
const liveFeedRequest = UrlFetchApp.fetch(raceCenterObject.liveRaceURL)
const liveFeedData = JSON.parse(liveFeedRequest.getContentText())
return liveFeedData
}
function testTheApi() {
console.log(getLiveFeed("https://www.nascar.com/results/racecenter/2022/nascar-cup-series/firekeepers-casino-400/stn/live"))
}
CodePudding user response:
You can get data with
function getJSON(type='liveRaceURL') {
const url = "https://www.nascar.com/results/racecenter/2022/nascar-cup-series/firekeepers-casino-400/stn/live"
const source = UrlFetchApp.fetch(url).getContentText()
const urls = JSON.parse(/raceCenterGlobals = ({.*?});/gms.exec(source)[1].replace(/'/g,'"'))
console.log(UrlFetchApp.fetch(urls[type]).getContentText())
}
then parse the result
type could be one of these values:
raceURL , pointsURL , liveRaceURL , stagePointsURL , snappyTVURL , keyMomentsURL , lapTimesURL , driverRecapURL , lapAveragesURL , lapFastURL , pitStopDataURL , liveFlagDataURL , scheduleFeedURL , weekendScheduleFeedURL