Skip to content

Instantly share code, notes, and snippets.

@dmattia
Created November 7, 2021 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmattia/fc631f09986e53e0ca947841791bf5cf to your computer and use it in GitHub Desktop.
Save dmattia/fc631f09986e53e0ca947841791bf5cf to your computer and use it in GitHub Desktop.
Qbs with the most 100% completion percentage games in a season
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();
@dmattia
Copy link
Author

dmattia commented Mar 27, 2022

import { mapSeries } from 'bluebird';
import got from 'got';
import _ from 'lodash';

/**

  • Finds the results of all drives in a given week
  • @param player
  • @param week - The week of the regular season a game was played
  • @returns a list of names
    */
    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));
}

/**

  • main function
    */
    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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment