-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloadAllInputFiles.js
65 lines (53 loc) · 1.53 KB
/
preloadAllInputFiles.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fs from 'fs';
import path from 'path';
let years = process.argv.slice(2).map((it) => {
if (+it) {
return +it
} else {
throw new Error(`Invalid year: "${it}"`)
}
})
if (years.length === 0) throw new Error('No years specified')
for (const year of years) {
for (let day = 1; day <= 25; day++) {
let loaded = await loadDay(year, day);
if (loaded) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
}
function loadDay(year, day) {
let folder = new URL(`./inputs/${year}`, import.meta.url).pathname;
let fileName = `day${String(day).padStart(2, '0')}.txt`;
if (fs.existsSync(path.join(folder, fileName))) {
console.log(`Day ${year}/${day} already loaded`);
return null;
}
let url = `https://adventofcode.com/${year}/day/${day}/input`;
let token = process.env.SESSION_TOKEN;
if (!token) {
console.error('SESSION_TOKEN env variable is not set');
process.exit(1);
}
console.log(`Processing ${year}/${day}...`);
console.log('Loading...', url);
return fetch(url, {
headers: {
cookie: `session=${token}`
}
})
.then((res) => {
if (res.status === 404) {
throw new Error(`Day ${year}/${day} not found. Too soon for ${day} december ${year}?`)
}
return res.text();
})
.then((text) => {
console.log('Loaded');
text = text.trim();
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(path.join(folder, fileName), text);
console.log(`Saved ${year}/${day}`);
return true;
})
}