// Code.gs - Google Apps Script
// Deploy as Web App (Anyone, execute as "Me").
// This script fetches the ESPNcricinfo points table page and returns JSON of the table.
function doGet(e) {
var url = 'https://www.espncricinfo.com/series/nepal-premier-league-2025-26-1510976/points-table-standings';
try {
var res = UrlFetchApp.fetch(url, {muteHttpExceptions:true, followRedirects:true, validateHttpsCertificates:true});
var code = res.getResponseCode();
var html = res.getContentText();
if (code !== 200) {
return ContentService.createTextOutput(JSON.stringify({error: 'fetch_failed', code: code, bodySnippet: html.substring(0,200)})).setMimeType(ContentService.MimeType.JSON);
}
// Extract the first
block — generic approach
var tableMatch = html.match(/
/i);
if (!tableMatch) {
// Try to find a div or script-based JSON that contains "points" or "pointsTable"
return ContentService.createTextOutput(JSON.stringify({error:'table_not_found', hint:'No element found - page may be client rendered.'})).setMimeType(ContentService.MimeType.JSON);
}
var tableHtml = tableMatch[0];
// Remove newline chars for easier parsing
tableHtml = tableHtml.replace(/\r?\n/g,' ');
// Parse rows
var rows = [];
var trRegex = /]*>([\s\S]*?)<\/tr>/ig;
var tdRegex = /<(?:td|th)[^>]*>([\s\S]*?)<\/(?:td|th)>/ig;
var tr;
while ((tr = trRegex.exec(tableHtml)) !== null) {
var rowText = tr[1];
var cols = [];
var td;
while ((td = tdRegex.exec(rowText)) !== null) {
// Strip HTML tags inside cell, trim space
var cell = td[1].replace(/<[^>]+>/g,'').replace(/ /g,' ').trim();
cols.push(cell);
}
// ignore empty rows
if (cols.length) rows.push(cols);
}
var result = {
fetchedFrom: url,
fetchedAt: new Date().toISOString(),
statusCode: code,
rows: rows
};
return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
} catch (err) {
return ContentService.createTextOutput(JSON.stringify({error:'exception', message: err.toString()})).setMimeType(ContentService.MimeType.JSON);
}
}