Last active
December 29, 2020 22:57
-
-
Save rovolution/82fd1effb1f616f4e9248e3de0604fe3 to your computer and use it in GitHub Desktop.
Fantasy Football: Calculates highest scoring winner from each week + total scores for the season - REGULAR SEASON ONLY - (Yahoo Fantasy Sports)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Instructions: | |
- Log-in and navigate to your league's homepage. Make sure you are on Week 1 of the scores. | |
- Right-click and open Developer Tools | |
- Copy/paste the script contents into the Console and press enter to load the script into the page | |
- Execute the script within the Console by invoking the function: getScoreDataForSeason() | |
- Script will log in the Console when it is processing each week | |
- When the script is done, two tables will print to the Console: one with the top scorer for each week, another with the total scores for the entire season | |
*/ | |
async function getScoreDataForSeason() { | |
const highestWeeklyScoresData = []; | |
const usernameToTotalScoreMap = {}; | |
let week = 1; | |
const MAX_NUM_WEEKS = 16; | |
while(week <= MAX_NUM_WEEKS) { | |
console.log(`Extracting scores for week ${week}`); | |
const { highestWeeklyScorer, weeklyUserToScoreMap } = _getWeekScoreData(); | |
const { name, score } = highestWeeklyScorer; | |
highestWeeklyScoresData.push([week, name, score]); | |
for(const user in weeklyUserToScoreMap) { | |
const weeklyScore = weeklyUserToScoreMap[user]; | |
const totalUserScore = usernameToTotalScoreMap[user]; | |
if (totalUserScore === undefined) { | |
usernameToTotalScoreMap[user] = weeklyScore; | |
} | |
else { | |
usernameToTotalScoreMap[user] = totalUserScore + weeklyScore; | |
} | |
} | |
console.log(`Loading next week's scores`); | |
let nextPageBtn = _selectNextWeekPageBtn(); | |
if (nextPageBtn) { | |
nextPageBtn.click(); | |
await _wait(3500); // hacky: wait some amount of time to load next week's results | |
} | |
week++; | |
} | |
console.log("Extraction complete"); | |
console.log("Highest Weekly Scores"); | |
console.table(highestWeeklyScoresData); | |
console.log("Total Scores for Season"); | |
console.table(usernameToTotalScoreMap); | |
return { highestWeeklyScoresData, usernameToTotalScoreMap }; | |
/* Helpers */ | |
function _wait(ms) { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, ms); | |
}); | |
} | |
function _selectNextWeekPageBtn() { | |
return document.querySelector(".Js-next:not(.Btn-disabled)"); | |
} | |
function _getWeekScoreData() { | |
const scoreElements = document.querySelectorAll("#matchupweek .List-rich .Grid-u-6-13"); | |
let weekWinner = { | |
score: -1, | |
name: "Rolo" | |
}; | |
let weeklyUserToScoreMap = {}; | |
for (let i = 0; i < scoreElements.length; i++) { | |
const entryElement = scoreElements[i]; | |
const isByeWeekCell = (entryElement.innerText === "Bye"); | |
if (!isByeWeekCell) { | |
const score = parseFloat(entryElement.querySelector(".Grid-u-1-4").innerText); | |
const name = entryElement.querySelector(".F-link").innerText; | |
weeklyUserToScoreMap[name] = score; | |
if (score > weekWinner.score) { | |
weekWinner = { | |
score: score, | |
name: name | |
}; | |
} | |
} | |
} | |
return { | |
highestWeeklyScorer: weekWinner, | |
weeklyUserToScoreMap | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment