-
Notifications
You must be signed in to change notification settings - Fork 5
/
prepare_day.js
155 lines (118 loc) · 4.52 KB
/
prepare_day.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
// ========================= //
// = Copyright (c) NullDev = //
// ========================= //
const fs = require("node:fs");
const fetch = require("node-fetch").default;
const cheerio = require("cheerio");
const {exec} = require("child_process");
let year;
let day;
let session;
// get session from args in case we run via GH action
if (!!process.argv[3]) session = process.argv[3];
else {
try {
// eslint-disable-next-line prefer-destructuring
session = require("./config.json").session;
}
catch (e){
console.log("No config.json found! Copy-paste config.template.json to config.json and fill in your session cookie!");
process.exit(1);
}
if (!session){
console.log("No session cookie found! Fill in your session cookie in config.json!");
process.exit(1);
}
}
const date = process.argv[2];
if (!date || date === "today"){
const now = new Date();
const y = now.getFullYear();
const d = now.getDate();
year = String(y);
day = String(d);
}
else {
[year, day] = date.split("-");
}
if ((!year || !day) || (isNaN(Number(year)) || isNaN(Number(day)))){
console.error("Invalid year-day specified!");
process.exit(1);
}
console.log(`Preparing ${year}-${day}...`);
const headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
cookie: `session=${session};`,
};
(async() => {
const markup = await fetch(`https://adventofcode.com/${year}/day/${day}`, { headers }).then(res => res.text());
const $ = cheerio.load(markup);
$("a > span").each((_, el) => {
$(el).replaceWith($(el).text());
});
$("span").remove();
$("pre em, pre code").each((_, el) => {
$(el).replaceWith($(el).text());
});
$("a").each((_, el) => {
$(el).removeAttr("target");
$(el).removeAttr("class");
$(el).removeAttr("style");
$(el).removeAttr("id");
});
const res = [...$("body > main > article.day-desc")].map(el => {
const article = $(el).html()?.trim() ?? "";
let sanitized = article.replace(/(<\/li>)|(<ul>)|(<\/ul>)/g, "")
.replace(/<li>/g, "- ")
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/&/g, "&")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/(<h2>)|(<h2 id="part2">)/g, "## ")
.replace(/<\/h2>/g, "\n")
.replace(/<code><em>/g, "**`")
.replace(/<\/em><\/code>/g, "`**")
.replace(/(<code>)|(<\/code>)/g, "`")
.replace(/<pre>\n/g, "```\n")
.replace(/<pre>/g, "```\n")
.replace(/\n<\/pre>/g, "\n```")
.replace(/<\/pre>/g, "\n```")
.replace(/(<em>)|(<em class=".*">)|(<\/em>)/g, "**")
.replace(/(<p>)|(<\/p>)/g, "\n")
.replace(/\n{3,}/g, "\n\n");
sanitized.match(/<a href=".*?">.*?<\/a>/g)?.forEach(link => {
const [, href, text] = link.match(/<a href="(.+?)">(.+?)<\/a>/) ?? [];
const regex = new RegExp(String(link.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")).trim(), "gi");
sanitized = sanitized.replace(regex, `[${text}](${href?.replace(/&/g, "&")})`);
});
return sanitized;
});
const result = `Link: <https://adventofcode.com/${year}/day/${day}> <br>
Author: Eric Wastl ([@ericwastl](https://twitter.com/ericwastl)) (${year})
---
` + res[0] + (!!res[1] ? ("\n---\n\n" + res[1]) : "");
const dir = `./${year}/Day_${day.padStart(2, "0")}`;
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(`${dir}/README.md`, result, { flag: "w" });
const input = await fetch(`https://adventofcode.com/${year}/day/${day}/input`, { headers }).then(r => r.text());
fs.writeFileSync(`${dir}/input.txt`, input, { flag: "w" });
const CODE = `"use strict";
const fs = require("node:fs");
const path = require("node:path");
const { performance } = require("node:perf_hooks");
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\\n");
const pStart = performance.now();
//
// YOUR CODE HERE
//
const result = "...";
const pEnd = performance.now();
console.log("<DESCRIPTION>: " + result);
console.log(pEnd - pStart);
`;
if (!fs.existsSync(`${dir}/part_1.js`)) fs.writeFileSync(`${dir}/part_1.js`, CODE);
if (!fs.existsSync(`${dir}/part_2.js`)) fs.writeFileSync(`${dir}/part_2.js`, CODE);
exec(`git add ${dir}/*`);
})();