• Oct 15, 2023 •LeifMessinger
0 likes • 7 views
//Disclaimer: I do not condone mass web scraping of ESPN websites, and this is just theoretical code that hasn't been used. //Scrape player stats off of https://www.espn.com/nfl/team/roster/_/name/phi/philadelphia-eagles //Example player JS object /* { "shortName": "S. Opeta", "name": "Sua Opeta", "href": "http://www.espn.com/nfl/player/_/id/3121009/sua-opeta", "uid": "s:20~l:28~a:3121009", "guid": "dec19157-e984-f981-3724-498281328c97", "id": "3121009", "height": "6' 4\"", "weight": "305 lbs", "age": 27, "position": "G", "jersey": "78", "birthDate": "08/15/96", "headshot": "https://a.espncdn.com/i/headshots/nfl/players/full/3121009.png", "lastName": "Sua Opeta", "experience": 4, "college": "Weber State" } */ //The page needs to be focused, so you have to put this code in a bookmarklet and click the page before clicking it. Allegedly. navigator.clipboard.writeText(JSON.stringify( window["__espnfitt__"].page.content.roster.groups.flatMap((group)=>{ return group.athletes.map((athlete)=>{ //We can assume all football players are above 100 lbs and below 1000 lbs. athlete.weight = parseInt(athlete.weight.substring(0,3)); if(athlete.experience == "R") athlete.experience = 0; else athlete.experience = parseInt(athlete.experience); //We can assume players are at least 1 foot, or under 10 feet tall. athlete.inches = 12 * parseInt(athlete.height.substring(0, 1)); //Add feet in inches athlete.inches += parseInt(athlete.height.substring(2).replaceAll("\"", "").trim()); //Add remaining inches const monthDayYear = athlete.birthDate.split("/"); athlete.birthMonth = parseInt(monthDayYear[0]); athlete.birthDay = parseInt(monthDayYear[1]); athlete.birthYear = parseInt(monthDayYear[2]); //The only really useful stuff we get from this is Height, weight, left handedness, age, position, and birthday return athlete; }); }) , null, "\t")).then(null, ()=>{alert("That failed.")}); //Changes all the team links on this page https://www.espn.com/nfl/stats/team to the team's roster for easier scraping $$("table > tbody > tr > td > div > div > a").forEach((elm)=>{ elm.setAttribute("href", elm.getAttribute("href").replace("team/", "team/roster/")); });
• Nov 16, 2023 •LeifMessinger
function filePath(file){ let folders = file.getParents(); const parents = []; function makePathString(folderArray){ let path = ""; folderArray.forEach((folder)=>{ path += "/" + folder.getName(); }); return path; } while (folders.hasNext()) { const folder = folders.next(); //This should hopefully remove that folder from the iterator parents.unshift(folder); } return makePathString(parents); } function myFunction() { const myEmail = Session.getEffectiveUser().getEmail(); Logger.log("My email is " + myEmail); // Log the name of every file in the user's Drive. var fileIterator = DriveApp.getFiles(); const files = []; //List of [File, size] entries const filesMaxSize = 10; while (fileIterator.hasNext()) { var file = fileIterator.next(); const owner = file.getOwner(); //Only files I own if((!(owner)) || (!(owner.getEmail)) || owner.getEmail() != myEmail){ continue; } const entry = [file, file.getSize()]; function slideUp(arr, index){ //Let's keep sliding it up so we don't have to sort it at the end. for(let i = index; i > 0; --i){ //Stops at 1 const nextFile = arr[i-1]; if(nextFile[1] > entry[1]){ break; }else{ //Swap with the next file to slide the file up const temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } } } if(files.length < filesMaxSize){ files.push(entry); slideUp(files, files.length - 1); }else{ if(entry[1] > files[files.length - 1][1]){ //If it's bigger than the smallest file in the list. files[files.length - 1] = entry; //Replace the smallest file slideUp(files, files.length - 1); //Slide it up } } } for(let i = 0; i < filesMaxSize; ++i){ const file = files[i][0]; const size = files[i][1]; Logger.log(size + "\t" + filePath(file) + "/" + file.getName() + "\t" + file.getOwner().getName()); } }
• Nov 19, 2022 •CodeCatch
0 likes • 1 view
const levenshteinDistance = (s, t) => { if (!s.length) return t.length; if (!t.length) return s.length; const arr = []; for (let i = 0; i <= t.length; i++) { arr[i] = [i]; for (let j = 1; j <= s.length; j++) { arr[i][j] = i === 0 ? j : Math.min( arr[i - 1][j] + 1, arr[i][j - 1] + 1, arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1) ); } } return arr[t.length][s.length]; }; levenshteinDistance('duck', 'dark'); // 2
0 likes • 3 views
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k])); euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
• Feb 5, 2021 •LeifMessinger
0 likes • 2 views
class Timer{ start(){ this.startTime = Date.now(); } stop(){ const dt = Date.now() - this.startTime; console.log(dt); return dt; } getExecTime(fun, asynchronous){ return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun); } getSyncExecTime(fun){ this.start(); fun(); return this.stop(); } async getAsyncExecTime(fun){ this.start(); await fun(); return this.stop(); } static average(fun, numTimes, asynchronous = false){ const times = []; const promises = []; let timer = new Timer(); if(asynchronous){ for(let i = 0; i < numTimes; i++){ timer.getAsyncExecTime(fun); } }else{ for(let i = 0; i < numTimes; i++){ timer.getSyncExecTime(fun); } } return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length); } static async timeAsync(fun, numTimes){ if(numTimes <= 1) return new Timer().getAsyncExecTime(fun); const promises = []; let timer = new Timer(true); for(let i = 0; i < numTimes; i++){ promises.push(fun().catch((e)=>{})); } try{ await Promise.all(promises).catch((e)=>{}); }catch(e){ //do absolutely nothing } return timer.stop(); } constructor(start = false){ if(start) this.start(); } }
0 likes • 0 views
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed); // Examples toFixed(25.198726354, 1); // 25.1 toFixed(25.198726354, 2); // 25.19 toFixed(25.198726354, 3); // 25.198 toFixed(25.198726354, 4); // 25.1987 toFixed(25.198726354, 5); // 25.19872 toFixed(25.198726354, 6); // 25.198726