add a 1 minute ratelimit to aoc fetch function

This commit is contained in:
everyone 2023-10-15 19:48:11 -07:00
parent c8b990f385
commit acfe100c68
Signed by: everyone
SSH Key Fingerprint: SHA256:FKcGHdUnp2OocVUUAEJV25QetYQXwbmKPSsblofJOrM
1 changed files with 16 additions and 2 deletions

18
cli.ts
View File

@ -10,10 +10,14 @@ await load({
export: true,
})
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const error = (msg: string) => {
console.log(msg)
Deno.exit(1)
}
const consoleUpdate = async (msg: string) =>
await Deno.stdout.write(new TextEncoder().encode("\r" + msg))
const EventDate = ({ value }: ArgumentValue) => {
const results = value.split("/").map((date, _, arr) => {
@ -31,7 +35,16 @@ const aoc = async (
init:
| { type: "input"; date: Event }
| { type: "answer"; date: Event; answer: unknown; level: 1 | 2 },
) => {
): Promise<string> => {
const timestamp = localStorage.getItem("timestamp")
if (timestamp && Number(timestamp) > new Date().getTime()) {
await consoleUpdate(
`Woah, slow down. waiting for ${(Number(timestamp) - new Date().getTime()) / 1000} seconds`,
)
await sleep(1000)
return aoc(init)
} else localStorage.setItem("timestamp", String(new Date().getTime() + 60000))
const headers = new Headers({
Cookie: Deno.env.get("cookie")!,
"User-Agent": JSON.stringify(cliInfo),
@ -46,6 +59,7 @@ const aoc = async (
answer: String(init.answer),
})
}
const res = await self.fetch(
`https://adventofcode.com/${init.date[1]}/day/${init.date[2]}/${init.type}`,
{ headers, body, method: init.type === "answer" ? "POST" : "GET" },
@ -53,7 +67,7 @@ const aoc = async (
if (res.status != 200) error(res.statusText)
const text = await res.text()
if ("level" in init) {
if (init.type === "answer") {
const { DOMParser } = await import(
"https://deno.land/x/deno_dom@v0.1.41-alpha-artifacts/deno-dom-wasm.ts"
)