Created
December 12, 2022 22:19
-
-
Save jonahsnider/7a5ccaf15c836660e41a22f9eb3a8da8 to your computer and use it in GitHub Desktop.
Find FRC teams attending events that take place before a given event
This file contains 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
// @ts-check | |
// curl --request GET \ | |
// --url 'https://frc-api.firstinspires.org/v3.0/2023/teams?eventCode=casf' \ | |
// --header 'Authorization: Basic <token>}' | |
import rawSf from "./sf/raw.json" assert { type: "json" }; | |
import * as fs from "fs/promises"; | |
const teams = rawSf.teams.map((team) => team.teamNumber); | |
const BASE_URL = "https://frc-api.firstinspires.org"; | |
const AUTH_TOKEN = "<token>"; | |
const YEAR = 2023; | |
const EVENT_FOR_FILTERING = { week: 3, name: "CASF", startDate: new Date("2023-03-16T00:00:00") }; | |
async function getCache(team) { | |
try { | |
const cache = await fs.readFile(`./cache/${team}.json`, "utf-8"); | |
return JSON.parse(cache); | |
} catch { | |
return undefined; | |
} | |
} | |
async function getEventsForTeam(team) { | |
let body = await getCache(team); | |
if (!body) { | |
const url = new URL(`v3.0/${YEAR}/events`, BASE_URL); | |
url.searchParams.append("teamNumber", team); | |
const response = await fetch(url, { | |
method: "GET", | |
headers: { | |
Authorization: `Basic ${AUTH_TOKEN}`, | |
}, | |
}); | |
if (!response.ok) { | |
throw new Error(`Request failed - ${response.status} ${response.statusText} - ${await response.text()}`); | |
} | |
body = await response.json(); | |
await fs.mkdir("./cache", { recursive: true }); | |
await fs.writeFile(`./cache/${team}.json`, JSON.stringify(body, null, 2)); | |
} | |
return body.Events.map((event) => { | |
const result = { week: event.weekNumber, name: event.code, startDate: new Date(event.dateStart) }; | |
// The events API appears to be wrong some of the time | |
switch (event.code) { | |
case "CASF": | |
result.week = 3; | |
break; | |
case "CAFR": | |
result.week = 2; | |
break; | |
case "CAPH": | |
result.week = 1; | |
break; | |
} | |
return result; | |
}); | |
} | |
const fullResult = []; | |
for (const team of teams) { | |
const events = await getEventsForTeam(team); | |
for (const event of events) { | |
fullResult.push({ team, week: event.week, "event name": event.name, "start date": event.startDate }); | |
} | |
} | |
const result = fullResult.filter((entry) => entry["start date"] < EVENT_FOR_FILTERING.startDate); | |
console.table(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment