Created
November 7, 2021 18:58
-
-
Save dmattia/fc631f09986e53e0ca947841791bf5cf to your computer and use it in GitHub Desktop.
Qbs with the most 100% completion percentage games in a season
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
import got from 'got'; | |
import { mapSeries } from 'bluebird'; | |
/** | |
* Finds all players who had 100% completion percentages during a given week of a given year. | |
* | |
* @param year - The year the games were played | |
* @param week - The week of the regular season a game was played | |
* @returns a list of names | |
*/ | |
async function findQbsWithPerfectAccuracyForWeek( | |
year: number, | |
week: number, | |
): Promise<string[]> { | |
const { body: games } = await got.get( | |
`https://api.collegefootballdata.com/games/players`, | |
{ | |
headers: { | |
Authorization: `Bearer ${process.env.COLLEGE_FOOTBALL_DATA_API_KEY}`, | |
}, | |
searchParams: { | |
year, | |
week, | |
category: 'passing', | |
}, | |
responseType: 'json', | |
}, | |
); | |
return (games as any).flatMap(({ teams }) => | |
teams.flatMap(({ categories }) => | |
categories.flatMap(({ types }) => | |
types | |
.filter(({ name }) => name === 'C/ATT') | |
.flatMap(({ athletes }) => athletes) | |
.filter(({ id, stat }) => { | |
// "Team" stats should not count, and are identified by an ID less than 0 | |
if (parseInt(id, 10) < 0) { | |
return false; | |
} | |
const [completions, attempts] = stat | |
.split('/') | |
.map((rawNum) => parseInt(rawNum, 10)); | |
return completions === attempts; | |
}) | |
.map(({ name }) => name), | |
), | |
), | |
); | |
} | |
async function main(): Promise<void> { | |
// Look through the years 2004 to 2021 | |
const years = [...new Array(18).keys()].map((year) => year + 2004); | |
// Look through weeks 1 to 14 | |
const weeks = [...new Array(14).keys()].map((week) => week + 1); | |
// Go through each year one by one to not be rate-limited | |
await mapSeries(years, async (year) => { | |
// Find all perfect games completion wise | |
const perfectWeeks = await mapSeries(weeks, async (week) => { | |
const qbs = await findQbsWithPerfectAccuracyForWeek(year, week); | |
// Sleep for half a second in order to not bring down the API | |
await new Promise((r) => setTimeout(r, 500)); | |
return qbs; | |
}); | |
// Count how many times each player occurred for this year | |
const qbCounts = perfectWeeks | |
.flat() | |
.reduce( | |
(map, qb) => map.set(qb, (map.get(qb) ?? 0) + 1), | |
new Map<string, number>(), | |
); | |
// Print out any player with 5+ such games | |
[...qbCounts.entries()] | |
.filter(([_, perfectWeekCount]) => perfectWeekCount >= 5) | |
.forEach(([qb, perfectWeeks]) => { | |
console.info(`${qb} had ${perfectWeeks} perfect weeks in ${year}`); | |
}); | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import { mapSeries } from 'bluebird';
import got from 'got';
import _ from 'lodash';
/**
*/
async function findDriveResultsForPlayer(
player: string,
week: number,
): Promise<string[]> {
const drivesData = await got.get(
https://api.collegefootballdata.com/drives
,{
headers: {
Authorization:
Bearer ${process.env.COLLEGE_FOOTBALL_DATA_API_KEY}
,},
searchParams: {
year: 2021,
week,
team: 'Notre Dame',
},
responseType: 'json',
},
);
const driveResults = new Map<string, string>(
drivesData.body
.filter(({ offense }) => offense === 'Notre Dame')
.map(({ id, drive_result }) => [id, drive_result]),
);
const playsData = await got.get(
https://api.collegefootballdata.com/play/stats
,{
headers: {
Authorization:
Bearer ${process.env.COLLEGE_FOOTBALL_DATA_API_KEY}
,},
searchParams: {
year: 2021,
week,
team: 'Notre Dame',
},
responseType: 'json',
},
);
const playerDrives = new Set(
playsData.body
.filter(({ athleteName }) => athleteName === player)
.map(({ driveId }) => driveId),
);
return [...playerDrives].map((id) => driveResults.get(id));
}
/**
*/
async function main(): Promise {
const weeks = [...new Array(14).keys()].map((week) => week + 1);
const results = (
await mapSeries(weeks, (week) =>
// findDriveResultsForPlayer('Tyler Buchner', week),
// findDriveResultsForPlayer('Jack Coan', week),
findDriveResultsForPlayer('Drew Pyne', week),
)
).flat();
const occurrences = _.countBy(results);
console.log(occurrences);
}
main();