RemcoE33

football ref

Mar 6th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Get table from pro-football-reference.com
  3. *
  4. * @param {string} url The url.
  5. * @param {boolean} headers default is true.
  6. * @return {array} table data.
  7. * @customfunction
  8. */
  9. function FOOTBALL_REF(url, headers = true) {
  10.   const html = UrlFetchApp.fetch(url).getContentText()
  11.   const table = /<tbody>(.*?)<\/tbody>/gmsi.exec(html)[1]
  12.   const rows = table.split("<tr")
  13.   rows.shift()
  14.  
  15.   const results = [];
  16.  
  17.   const latLng = /var destinations = new Array\((.*?)\);/gmsi.exec(html)[1].trim()
  18.     .split("\n")
  19.     .reduce((acc, curr) => {
  20.       const id = /"id" : "(.*?)"/gmsi.exec(curr)[1].trim()
  21.       const lat = Number(/new google\.maps\.LatLng\((.*?),/gmsi.exec(curr)[1])
  22.       const lng = Number(/new google\.maps\.LatLng\([\s\S]*?,(.*?)\)/gmsi.exec(curr)[1])
  23.       acc[id] = { lat, lng }
  24.       return acc;
  25.     }, {})
  26.  
  27.   console.log(latLng)
  28.   const startLat = Number(/var start = new google\.maps\.LatLng\((.*?),/gmsi.exec(html)[1])
  29.   const startLng = Number(/var start = new google\.maps\.LatLng\([\s\S]*?,(.*?)\)/gmsi.exec(html)[1])
  30.  
  31.   rows.forEach((row, i) => {
  32.     const object = {}
  33.  
  34.     row.split("<td").forEach(col => {
  35.       const key = /data-stat="(.*?)"/gmsi.exec(col)[1]
  36.       let value;
  37.  
  38.       if (key !== "distance") {
  39.         value = /">(.*?)<\/a>/gmsi.exec(col)[1]
  40.       } else {
  41.         const distanceId = /id="(.*?)"/.exec(col)[1].trim().replace("distance_", "");
  42.         const target = latLng[distanceId]
  43.         value = haversine(startLat, startLng, target.lat, target.lng);
  44.       }
  45.       object[key] = value
  46.     })
  47.  
  48.     if (headers && i === 0) {
  49.       results.push(Object.keys(object))
  50.     }
  51.     results.push(Object.values(object))
  52.   });
  53.  
  54.   return results;
  55. }
  56.  
  57.  
  58. function haversine(lat1, lon1, lat2, lon2) {
  59.   const rad = 6371;
  60.   const dLat = toRad(lat2 - lat1);
  61.   const dLon = toRad(lon2 - lon1);
  62.   const latRad1 = toRad(lat1);
  63.   const latRad2 = toRad(lat2);
  64.  
  65.   const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  66.     Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(latRad1) * Math.cos(latRad2);
  67.   const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  68.   const d = rad * c;
  69.   return Math.round(d / 1.60934);
  70. }
  71.  
  72. // Converts numeric degrees to radians
  73. function toRad(Value) {
  74.   return Value * Math.PI / 180;
  75. }
  76.  
  77.  
  78.  
Add Comment
Please, Sign In to add comment