add a 1 minute ratelimit to aoc fetch function
This commit is contained in:
parent
c8b990f385
commit
acfe100c68
18
cli.ts
18
cli.ts
|
@ -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"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue