-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.js
44 lines (34 loc) · 889 Bytes
/
getData.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
require('dotenv').config();
const fetch = require('isomorphic-fetch');
const fs = require('fs');
const [,, day] = process.argv;
const session = process.env.session;
const twoDigitNumber = num => {
if (num >= 10) {
return `${num}`;
}
return `0${num}`;
};
fetch(`https://adventofcode.com/2021/day/${day}/input`, {
"headers": {
"cookie": `session=${session}`
},
"method": "GET",
})
.then(data => data.text())
.then(data => data.split('\n').filter(el => el))
.then(data => {
const dataStr = `
const test = [
];
const data = ${JSON.stringify(data, null, 2)};
module.exports = { data, test };
`.trim();
fs.writeFile(`./data/day${twoDigitNumber(day)}.js`, dataStr, (err) => {
if (err) {
console.log({ err });
return;
}
console.log(`Fetched data for day ${day}, exported as 'data' from './data/day${twoDigitNumber(day)}.js'`);
});
});