-
Notifications
You must be signed in to change notification settings - Fork 2
/
prep.js
86 lines (71 loc) · 2.06 KB
/
prep.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const request = require('request');
var fs = require('fs');
var dir = './';
const prompt = require('prompt-sync')();
const axios = require("axios");
const cheerio = require("cheerio");
var fileCount
fileCount = fs.readdirSync(dir, (err, files) => {
fileCount = files.length - 1;
return fileCount
});
var fileTotal = fileCount.length - 4
dir = dir + "day" + fileTotal
console.log(dir)
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
var splitMode = prompt("split by comma or line? ")
if (splitMode == "comma") {
var content = "const fs = require('fs');\n\nlet input = fs.readFileSync('./input.txt', {encoding: 'utf8'}).split(',')"
} else {
var content = "const fs = require('fs');\n\nlet input = fs.readFileSync('./input.txt', {encoding: 'utf8'}).split('\\n')"
}
for (let i = 1; i <= 2; i++) {
dirTemp = dir + "/part" + i + ".js"
fs.writeFile(dirTemp, content, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
//Download input
let file = fs.createWriteStream(dir + "/input.txt");
request({
url: 'https://adventofcode.com/2021/day/' + fileTotal + '/input',
method: "GET",
headers: {
"cookie": //cookie goes here
}
},
(error, response, body)=>{
console.log(response.headers)
}
).pipe(file)
//Download examples into txt files
var scrapeData = []
axios
.get("https://adventofcode.com/2021/day/" + fileTotal)
.then((response) => {
const html = response.data;
const $ = cheerio.load(html);
$('pre').each(function (i, e) {
if ($(this).prev().text().indexOf("example") != -1) {
scrapeData.push($(this).text())
}
});
dirBase = dir + "/example"
for (let i = 0; i < scrapeData.length; i++) {
dir = dirBase + i + ".txt"
fs.writeFile(dir, scrapeData[i], function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
})
.catch((error) => {
console.log(error);
});