-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetable.js
165 lines (137 loc) · 3.83 KB
/
timetable.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
156
157
158
159
160
161
162
163
164
165
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const startTime = new Date(2022, 11, 26, 16);
async function main() {
const data = JSON.parse(fs.readFileSync(path.join(__dirname, "tmp", "running-order.json")));
data.pop();
data.shift();
const groups = {};
data.forEach(item => {
let name = item.name;
const time = new Date(startTime);
time.setSeconds(time.getSeconds() + item.startTime);
if (name.indexOf(":\\") > -1) {
const parsed = path.parse(item.name);
name = parsed.dir.split("\\").pop();
}
console.log(`${name} - ${time}`);
let groupIdentifier = "";
if (name == 'Break' || name == 'Outro') {
groupIdentifier = name;
return;
} else if (name.indexOf("Ensemble") > -1 || name.substring(0, 1) == 'O') {
groupIdentifier = "Ensemble";
} else {
const parts = name.split("-")[1].trim().split(" ");
for (let i = 0; i < parts.length; i++) {
if (parts[i] == 'Over' || (!isNaN(parts[i]) && !isNaN(parseFloat(parts[i])))) {
break;
}
groupIdentifier += " " + parts[i];
}
}
groupIdentifier = groupIdentifier.trim();
const payload = {
name,
time
};
if (groups.hasOwnProperty(groupIdentifier)) {
groups[groupIdentifier].push(payload);
} else {
groups[groupIdentifier] = [payload]
}
});
const opts = {
width: 1080,
height: 1080
};
const imgBase = path.join(__dirname, "tmp", "gfx");
if (!fs.existsSync(imgBase)) {
fs.mkdirSync(imgBase, { recursive: true });
}
const textTemplate = `
<svg width="${opts.width}" height="${opts.height}">
<style>
.title {
fill: #282360;
font-size: 65px;
font-weight: bold;
font-family: 'Asket';
text-transform: uppercase;
}
.category{
fill: #282360;
font-family: 'Open Sans';
}
</style>
<text x="50%" y="20%" text-anchor="middle" class="title">{{title}}</text>
{{categories}}
</svg>
`;
const names = Object.keys(groups);
console.log(names.length);
for (let index = 0; index < names.length; index++) {
const k = names[index].replaceAll("&", "&");
const groupData = groups[k];
const dest = path.join(imgBase, `${index + 1} - ${k}.png`);
let categories = "";
const spacing = 10;
let top = 35;
groupData.forEach(category => {
const start = category.time.toTimeString().substring(0, 5);
const fontSize = 40;//Math.min(40, opts.width / category.name.length) + 2;
let name = category.name.replaceAll("&", "&").replaceAll(k, "");
if(name.indexOf(" - ") > -1){
name = name.split(" - ")[1].trim();
}
categories += `<text x="35%" y="${top}%" class="category" font-size="${fontSize}px"><tspan style="font-weight: bold">${start}:</tspan> ${name}</text>\n`;
top += spacing;
});
const text = textTemplate.replaceAll("{{title}}", k.toUpperCase()).replaceAll("{{categories}}", categories.trim()).trim();
const [byba, tymba, sponsor, buffer] = await Promise.all([
sharp('byba.png').flatten({ background: '#FFF' }).resize({ height: opts.height * 0.4 }).ensureAlpha(0.4).toBuffer(),
sharp('TYMBA.png').flatten({ background: '#FFF' }).resize({ height: opts.height * 0.4 }).ensureAlpha(0.4).toBuffer(),
sharp('marching arts.png').flatten({ background: '#FFF' }).resize({ height: opts.height * 0.1 }).toBuffer(),
sharp(Buffer.from(text)).toBuffer()
]);
await sharp({
create: {
width: opts.width,
height: opts.height,
channels: 4,
background: {
r: 255,
g: 255,
b: 255,
alpha: 1
}
}
}).composite([
{
input: byba,
top: opts.height * 0.3,
left: -250
},
{
input: tymba,
top: opts.height * 0.3,
left: opts.width - 200
},
{
input: sponsor,
left: opts.width * 0.35,
top: opts.height * 0.85
},
{
input: buffer,
left: 0,
top: 0
}
]).toFile(dest);
}
}
main().catch(e => {
console.log("Error");
console.log(e);
});