This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_template.js
171 lines (146 loc) · 4.54 KB
/
create_template.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
166
167
168
169
170
171
const fs = require('fs');
const sql = require('sql.js');
const moment = require('moment');
const db = (function () {
const buffer = fs.readFileSync('db.sqlite');
return new sql.Database(buffer);
})();
let likelyCast = {};
const dbProductions = db.exec('SELECT * FROM PRODUCTION');
const getProduction = (date) => {
const showDate = moment(date, 'DD.MM.YYYY');
let _ = (column) => {
return dbProductions[0]['columns'].indexOf(column);
};
for (let i = 0; i < dbProductions[0]['values'].length; i++) {
const production = dbProductions[0]['values'][i];
if (production[_('LOCATION')] === 'Wien') {
continue;
}
const startDate = moment(production[_('START')], 'YYYY-MM-DD HH:mm:ss.SSS');
const endDate = moment(production[_('END')], 'YYYY-MM-DD HH:mm:ss.SSS');
if (!showDate.isBefore(startDate) && !showDate.isAfter(endDate)) {
return {
'id': +production[_('ID')],
'location': production[_('LOCATION')],
};
}
}
console.log('Could not get production ID for date = ' + date.format('DD.MM.YYYY'));
throw {};
};
const dbPersons = db.exec('SELECT * FROM PERSON');
const toName = (personId) => {
let _ = (column) => {
return dbPersons[0]['columns'].indexOf(column);
};
for (let i = 0; i < dbPersons[0]['values'].length; i++) {
if (dbPersons[0]['values'][i][_('ID')] === personId) {
return dbPersons[0]['values'][i][_('NAME')];
}
}
console.log('Cannot convert personId ' + personId + ' to name');
throw {};
};
const calculateLikelyCast = (productionId) => {
const statement = db.prepare(`
SELECT
"CAST".PERSON_ID AS PERSON_ID
FROM "CAST"
WHERE
"CAST".SHOW_ID IN (
SELECT ID FROM SHOW WHERE PRODUCTION_ID = :productionId
)
AND "CAST".ROLE = :role
GROUP BY "CAST".ROLE, "CAST".PERSON_ID
ORDER BY "CAST".ROLE, COUNT( * ) DESC
`);
let cast = {
'Graf von Krolock': [],
'Sarah': [],
'Alfred': [],
'Professor Abronsius': [],
'Chagal': [],
'Magda': [],
'Rebecca': [],
'Herbert': [],
'Koukol': [],
'Tanzsolisten': [],
'Gesangssolisten': [],
'Tanzensemble': [],
'Gesangsensemble': [],
'Dirigent': [],
};
// TODO Also get the other cast members? :/
try {
[
'Graf von Krolock',
'Alfred',
'Sarah',
'Professor Abronsius',
'Chagal',
'Magda',
'Rebecca',
'Koukol',
'Herbert',
'Dirigent'
].forEach((role) => {
statement.bind({
':productionId': productionId,
':role': role,
});
statement.step();
const result = statement.getAsObject();
const personId = +result['PERSON_ID'];
if (isNaN(personId)) {
return;
}
cast[role] = [toName(personId)];
});
} finally {
statement.free();
}
return cast;
};
(() => {
let _ = (column) => {
return dbProductions[0]['columns'].indexOf(column);
};
for (let i = 0; i < dbProductions[0]['values'].length; i++) {
const production = dbProductions[0]['values'][i];
const productionId = production[_('ID')];
likelyCast[productionId] = calculateLikelyCast(productionId);
}
})();
const getTemplate = (date) => {
const day = date.format('DD.MM.YYYY');
const time = date.format('HH:mm');
const type = ((+date.format('HH')) < 18) ? 'Matinée' : 'Soirée';
const production = getProduction(date);
return {
'date': day,
'time': time,
'type': type,
'location': production['location'],
'cast': likelyCast[production['id']],
};
};
fs.readdir('./images', (err, items) => {
if (err) {
throw err;
}
const sortedFiles = items.sort((a, b) => {
const dateA = moment(a.split(/\./)[0], 'DD_MM_YYYY_HHmm');
const dateB = moment(b.split(/\./)[0], 'DD_MM_YYYY_HHmm');
return dateA.isBefore(dateB) ? -1 : +1;
});
const result = sortedFiles.map((file) => {
try {
return getTemplate(moment(file.split(/\./)[0], 'DD_MM_YYYY_HHmm'));
} catch (e) {
console.log('Failed for ' + file);
}
});
// TODO write to file
console.log(JSON.stringify(result, null, 4));
});