-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
398 lines (365 loc) · 11.1 KB
/
mod.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import {
PDFDocument,
rgb,
StandardFonts,
} from "https://cdn.skypack.dev/pdf-lib@^1.11.1?dts";
import { readCSV } from "https://deno.land/x/csv/mod.ts";
import { expandGlob } from "https://deno.land/[email protected]/fs/mod.ts";
import * as path from "https://deno.land/std/path/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
interface DocumentInformation {
pdfPath?: string;
donorPdf?: string;
tempo?: string;
cue?: string;
patch?: string;
endNote?: string;
}
interface CsvData {
type: string;
folderName: string;
priorAct: string;
nickName: string;
cue: string;
globalEndNote: string;
tempo: string;
startingPerformer: string;
performerFileNames: Array<string>;
performerPatch: string;
performerEndNote: string;
}
async function extractCsvDatas(
performer: string,
csvPath: string,
): Promise<CsvData[]> {
const f = await Deno.open(csvPath);
let rowIndex = 0;
let performerIndex = undefined;
const csvDatas: Array<CsvData> = [];
for await (const row of readCSV(f, { columnSeparator: "\t" })) {
if (rowIndex == 0) {
let columnIndex = 0;
for await (const cell of row) {
if (cell == performer) {
performerIndex = columnIndex;
}
columnIndex += 1;
}
} else if (rowIndex == 1) {
//pass
} else if (rowIndex > 1) {
if (performerIndex == undefined) {
throw new Error(
`Did not find performer ${performer} in file ${csvPath}`,
);
}
let type = undefined;
let folderName = undefined;
let priorAct = undefined;
let nickName = undefined;
let cue = undefined;
let globalEndNote = undefined;
let tempo = undefined;
let startingPerformer = undefined;
let performerFileNames = undefined;
let performerPatch = undefined;
let performerEndNote = undefined;
let columnIndex = 0;
for await (const cell of row) {
switch (columnIndex) {
case 0:
type = cell;
break;
case 1:
folderName = cell;
break;
case 2:
priorAct = cell;
break;
case 3:
nickName = cell;
break;
case 4:
cue = cell;
break;
case 5:
globalEndNote = cell;
break;
case 6:
tempo = cell;
break;
case 7:
startingPerformer = cell;
break;
case performerIndex:
performerFileNames = cell.split("|").filter(Boolean);
break;
case performerIndex + 1:
performerPatch = cell;
break;
case performerIndex + 2:
performerEndNote = cell;
break;
}
columnIndex += 1;
}
if (
type == undefined ||
folderName == undefined ||
priorAct == undefined ||
nickName == undefined ||
cue == undefined ||
globalEndNote == undefined ||
tempo == undefined ||
startingPerformer == undefined ||
performerFileNames == undefined ||
performerFileNames.length == 0 ||
performerPatch == undefined ||
performerEndNote == undefined
) {
console.log(folderName);
console.log(performerFileNames);
throw new Error(
`Failed to parse CSV file ${csvPath}. Is every field present for performer "${performer}"?`,
);
}
csvDatas.push({
type: type,
folderName: folderName,
priorAct: priorAct,
nickName: nickName,
cue: cue,
globalEndNote: globalEndNote,
tempo: tempo,
startingPerformer: startingPerformer,
performerFileNames: performerFileNames,
performerPatch: performerPatch,
performerEndNote: performerEndNote,
});
}
rowIndex += 1;
}
return csvDatas;
}
interface extractDocumentOptions {
performer: string;
csvDatas: Array<CsvData>;
folderPath: string;
}
async function extractDocumentInformations(
options: extractDocumentOptions,
): Promise<DocumentInformation[]> {
const folderPath = options.folderPath;
const csvDatas = options.csvDatas;
const performer = options.performer;
const documentInformations: Array<DocumentInformation> = [];
for (const csvData of csvDatas) {
// Performer does not play on piece
if (
csvData.performerFileNames.length == 1 &&
csvData.performerFileNames[0] == "-"
) {
// Only retrieve the first page of the PDF file with shortest name
const allFilePaths = [];
for await (
const entry of expandGlob(
`${folderPath}/${csvData.type}/${csvData.folderName}/**/*.pdf`,
)
) {
allFilePaths.push(entry.path);
}
if (allFilePaths.length == 0) {
throw new Error(
`Did not find any PDF files for song ${csvData.type}/${csvData.folderName}`,
);
}
const chosenPdf = allFilePaths.reduce((a, b) =>
a.length <= b.length ? a : b
);
documentInformations.push({
donorPdf: chosenPdf,
cue: `${csvData.startingPerformer} "${csvData.cue}"`.trim(),
tempo: csvData.tempo,
patch: csvData.performerPatch,
endNote: csvData.performerEndNote || csvData.globalEndNote,
});
} else {
// Find PDF file for performer
const allFilePaths = [];
for await (
const entry of expandGlob(
`${folderPath}/${csvData.type}/${csvData.folderName}/**/*.pdf`,
)
) {
allFilePaths.push(entry.path);
}
if (allFilePaths.length == 0) {
throw new Error(
`Did not find any PDF files for song ${csvData.type}/${csvData.folderName}`,
);
}
const possiblePdfPaths = [];
for (const pdfPath of allFilePaths) {
let isMatch = false;
let isNotMatch = false;
for (const possibleSubstring of csvData.performerFileNames) {
if (
possibleSubstring.startsWith("^") &&
path
.basename(pdfPath)
.toLowerCase()
.includes(possibleSubstring.substring(1).toLowerCase())
) {
isNotMatch = true;
break;
}
if (
path
.basename(pdfPath)
.toLowerCase()
.includes(possibleSubstring.toLowerCase())
) {
isMatch = true;
}
}
if (isMatch && !isNotMatch) {
possiblePdfPaths.push(pdfPath);
}
}
if (possiblePdfPaths.length == 0) {
const baseNames = allFilePaths.map((filePath) =>
path.basename(filePath).toLowerCase()
);
console.log(`All PDFs for song is: ${baseNames}`);
throw new Error(
`Did not find any PDFs for performer ${performer} for song ${csvData.type}/${csvData.folderName} with file names ${csvData.performerFileNames}`,
);
}
let chosenPdf = undefined;
if (possiblePdfPaths.length > 1) {
console.log(
`Found ${possiblePdfPaths.length} PDFs for performer ${performer} for song ${csvData.type}/${csvData.folderName}`,
);
console.log(`Matching PDFs: ${possiblePdfPaths}`);
// Choose the longest PDF path
chosenPdf = possiblePdfPaths.reduce((a, b) =>
a.length > b.length ? a : b
);
} else {
chosenPdf = possiblePdfPaths[0];
}
console.log(`Chosen PDF: ${chosenPdf}`);
documentInformations.push({
pdfPath: chosenPdf,
cue: `${csvData.startingPerformer} "${csvData.cue}"`.trim(),
tempo: csvData.tempo,
patch: csvData.performerPatch,
endNote: csvData.performerEndNote || csvData.globalEndNote,
});
}
}
return documentInformations;
}
async function combineDocuments(
performer: string,
documentInformations: Array<DocumentInformation>,
) {
// Create a new PDFDocument that will be the combined PDF
const dstDoc = await PDFDocument.create();
const fontSize = 13;
const helvetica = await dstDoc.embedFont(StandardFonts.Helvetica);
const timesRomanItalic = await dstDoc.embedFont(
StandardFonts.TimesRomanItalic,
);
const timesRoman = await dstDoc.embedFont(StandardFonts.TimesRoman);
const tempoColor = rgb(0.0, 0.0, 0.0);
const cueColor = rgb(0.8, 0.2, 0.2);
const patchColor = rgb(0.2, 0.2, 0.5);
const endNoteColor = rgb(0.2, 0.5, 0.2);
for (const docInfo of documentInformations) {
// Read PDF from memory and load it as a PDFDocument type
let donorPdf = undefined;
let totalPages = undefined;
if (docInfo.pdfPath) {
donorPdf = await PDFDocument.load(Deno.readFileSync(docInfo.pdfPath));
totalPages = donorPdf.getPageCount();
} else if (docInfo.donorPdf) {
donorPdf = await PDFDocument.load(Deno.readFileSync(docInfo.donorPdf));
totalPages = 1;
// totalPages = donorPdf.getPageCount()
} else {
throw Error(
"Did not find PDF path. This is probably an error with this script.",
);
}
for (let i = 0; i < totalPages; i++) {
const [page] = await dstDoc.copyPages(donorPdf, [i]);
if (i == 0) {
if (docInfo.tempo) {
const { width, height } = page.getSize();
page.drawText(`tempo: ${docInfo.tempo}`, {
x: 0.1 * width,
y: 0.96 * height,
size: fontSize,
font: timesRoman,
color: tempoColor,
});
}
if (docInfo.cue) {
const { width, height } = page.getSize();
page.drawText(docInfo.cue, {
x: 0.1 * width,
y: 0.98 * height,
size: fontSize + 1,
font: timesRomanItalic,
color: cueColor,
});
}
if (docInfo.patch) {
const { width, height } = page.getSize();
page.drawText(docInfo.patch, {
x: 0.7 * width,
y: 0.98 * height,
size: fontSize,
font: helvetica,
color: patchColor,
});
}
}
if (i == totalPages - 1) {
if (docInfo.endNote) {
const { width, height } = page.getSize();
page.drawText(docInfo.endNote, {
x: 0.7 * width,
y: (1 - 0.98) * height,
size: fontSize,
font: helvetica,
color: endNoteColor,
});
}
}
dstDoc.addPage(page);
}
}
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await dstDoc.save();
Deno.writeFileSync(`${performer}.pdf`, pdfBytes);
}
async function main() {
const args = parse(Deno.args);
// const performer = "Henrik"
const csvPath = args.csv;
const songsDirectory = args.folder;
const performers = [args.performer];
for (const performer of performers) {
console.log(`Parsing performer ${performer}`);
const csvDatas = await extractCsvDatas(performer, csvPath);
const documentInformations = await extractDocumentInformations({
performer: performer,
folderPath: songsDirectory,
csvDatas: csvDatas,
});
combineDocuments(performer, documentInformations);
}
}
main();