-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetInput.js
27 lines (21 loc) · 850 Bytes
/
getInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import fetch from "node-fetch";
import dayPadded from "./dayPadded.js";
export default async function getInput(year, day) {
const inputDir = "./inputs";
const inputFile = `${inputDir}/${year}-${dayPadded(day)}.txt`;
if (existsSync(inputFile)) {
return readFileSync(inputFile).toString("utf-8");
}
if (!existsSync(inputDir)) mkdirSync(inputDir);
if (!process.env.SESSION_TOKEN) {
console.error("Set a session token in .env to fetch your puzzle inputs.");
process.exit(1);
}
const opts = { headers: { cookie: `session=${process.env.SESSION_TOKEN}` } };
const url = `https://adventofcode.com/${year}/day/${day}/input`;
const res = await fetch(url, opts);
const input = await res.text();
writeFileSync(inputFile, input);
return input;
}