-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_model.js
345 lines (240 loc) · 12.5 KB
/
create_model.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
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
/* Import modules */
var fs = require("fs");
/*import fs from 'fs';*/
var sizeOf = require('image-size');
var lwip = require('lwip');
var rmdir = require('rmdir');
var colors = require('colors');
var YouTube = require('youtube-node');
function getYoutubeData(projtsJson) {
var youTube = new YouTube();
youTube.setKey('AIzaSyAPPDBVqSMFkX9UqTj1Nc_roz7oIKqowQc');
return new Promise(sendData => {
const arrayPromises = [];
Object.keys(projtsJson).forEach(projectKey => {
Object.keys(projtsJson[projectKey].videos).forEach(lang => {
projtsJson[projectKey].videos[lang].forEach((youtubeId, index) => {
arrayPromises.push(new Promise(resolve => {
youTube.getById(youtubeId, function(error, result) {
if (error) {
console.log(error);
}
else {
const thumbnails = result.items[0].snippet.thumbnails;
const data = {
src: thumbnails.standard.url,
srcset: [
`${thumbnails.standard.url} 1024w`,
`${thumbnails.standard.url} 800w`,
`${thumbnails.medium.url} 500w`,
`${thumbnails.default.url} 320w`,
],
width: thumbnails.standard.width,
height: thumbnails.standard.height,
content: youtubeId,
type: 'video',
};
resolve({
index:index,
lang:lang,
projectKey:projectKey,
data:data
});
}
});
}));
});
});
});
Promise.all(arrayPromises).then(data => {
data.forEach(dataProject => {
projtsJson[dataProject.projectKey]['videos'][dataProject.lang][dataProject.index] = dataProject.data;
});
sendData(projtsJson);
});
});
}
/* Generate responsive images */
function generateSourceResponsive(file, imagesFolder, dmsns, sourceImagesDir, imagesFolderWeb) {
return new Promise((rs, rj) => {
var dimensions = [2880, 1240, 620, 310, 160];
var srcsetPromises = [];
var dimTop;
var sourceImageDir = sourceImagesDir + file.split('.')[0] + '/';
if (!fs.existsSync(sourceImageDir)) fs.mkdirSync(sourceImageDir);
dimensions.some((d, key) => {
dimTop = key;
return dmsns.width >= d;
});
for (var i = dimTop; i <= 4; i++) {
srcsetPromises.push(new Promise((rs2, rj2) => {
var dim = dimensions[i];
var sourceImageDirFile = sourceImageDir + dim + '_' + file;
var imageFolderWebFile = imagesFolderWeb + dim + '_' + file;
lwip.open(imagesFolder + file, function (err, image) {
console.log('Generating -> ' + imagesFolder + file + ' to width = ' + this.dim);
image.batch()
.resize(this.dim, (this.dim / dmsns.width) * dmsns.height)
.writeFile(this.sourceImageDirFile, function (err) {
rs2(this.imageFolderWebFile + ' ' + this.dim + 'w');
}.bind({imageFolderWebFile: this.imageFolderWebFile, dim: this.dim}));
}.bind({sourceImageDirFile: sourceImageDirFile, imageFolderWebFile: imageFolderWebFile, dim: dim}));
}));
}
Promise.all(srcsetPromises).then(srcset => rs(srcset));
});
}
function generateSourceImages(projtsJson) {
return new Promise(sendData => {
function createImages(projtsJson) {
return new Promise(sendRawState => {
/* Declaration of vars */
var rawState = [];
/* Generate the projects output */
Object.keys(projtsJson).forEach(function (key) {
rawState.push(() => {
return new Promise(function (rslv, rjct) {
/* Declaration of vars */
const imagesFolder = 'projts/' + key + '/images/';
const imagesFolder_es = 'projts/' + key + '/images_es/';
const imagesFolderWeb = 'projts/' + key + '/imgweb/';
const imagesFolderWeb_es = 'projts/' + key + '/imgweb_es/';
const sourceImagesDir = 'projts/' + key + '/source_images/';
const sourceImagesDir_es = 'projts/' + key + '/source_images_es/';
function processFiles(folder1, folder2, folder3) {
fs.mkdirSync(folder1);
console.log(('Created/Updated directory: ' + folder1).cyan);
/* Get images info */
var files = fs.readdirSync(folder2);
var imagesPromises = [];
files.forEach(file => {
if (file.split('.')[1] == 'jpg' || file.split('.')[1] == 'png') {
imagesPromises.push(new Promise(function (rslv2, rjct2) {
var dimensions = sizeOf(folder2 + file);
generateSourceResponsive(file, folder2, dimensions, folder1, folder3).then(srcset => {
rslv2({
type: 'photo',
path: folder3 + file,
srcset: srcset,
width: dimensions.width,
height: dimensions.height
});
});
}));
}
});
return imagesPromises;
}
if (!fs.existsSync(imagesFolder_es)) {
fs.existsSync(sourceImagesDir)
? rmdir(sourceImagesDir, () => generateSourceImages(sourceImagesDir))
: generateSourceImages(sourceImagesDir);
function generateSourceImages(sourceImagesDir) {
Promise.all(processFiles(sourceImagesDir, imagesFolder, imagesFolderWeb)).then(values => {
projtsJson[key]["images"] = values;
/*projtsJson[key]["videos"] = processVideos(projtsJson[key]["videos"]);
processVideos(projtsJson[key]["videos"]).then(videos => {
projtsJson[key]["videos"] = videos;
});*/
/* include project */
var textCode = 'projects["' + key + '"]=' + JSON.stringify(projtsJson[key]) + ';';
rslv(textCode);
});
}
} else {
fs.existsSync(sourceImagesDir)
? rmdir(sourceImagesDir, () => {
fs.existsSync(sourceImagesDir_es)
? rmdir(sourceImagesDir_es, () => generateSourceImagesAll())
: generateSourceImagesAll();
})
: fs.existsSync(sourceImagesDir_es)
? rmdir(sourceImagesDir_es, () => generateSourceImagesAll())
: generateSourceImagesAll();
function generateSourceImagesAll() {
generateSourceImages_en(sourceImagesDir).then(generateSourceImages_es).then(rslv);
}
function generateSourceImages_es(projtsJson) {
return new Promise(gsiresolve => {
Promise.all(processFiles(sourceImagesDir_es, imagesFolder_es, imagesFolderWeb_es)).then(values => {
projtsJson[key]["images"]["es"] = values;
gsiresolve('projects["' + key + '"]=' + JSON.stringify(projtsJson[key]) + ';');
});
});
}
function generateSourceImages_en(sourceImagesDir) {
return new Promise(gsiresolve => {
Promise.all(processFiles(sourceImagesDir, imagesFolder, imagesFolderWeb)).then(values => {
projtsJson[key]["images"] = {"en": values};
gsiresolve(projtsJson);
});
});
}
}
})
});
});
sendRawState(rawState);
});
}
function generateState(rawState) {
return new Promise(sendData2 => {
var rawData;
(function promRecur(index) {
if (index == 0) {
rawData = "var state = state || {};";
rawData += "(function () { 'use strict';";
rawData += "var projects = {};";
}
rawState[index]().then(projectRawData => {
rawData += projectRawData;
console.log(('Generated raw code of project with key: ' + (index + 1)).green);
if (++index < rawState.length) {
promRecur(index);
} else {
rawData += "state = { projects: projects};";
rawData += "})();";
rawData += "export default state;";
/* Write the output */
sendData2(rawData);
}
});
})(0);
})
}
createImages(projtsJson).then(generateState).then(sendData);
});
}
function filterByValue(paramName, valueParam , rawObj, invert) {
var depObjt = {};
Object.keys(rawObj).forEach(value => {
if ((invert? rawObj[value][paramName] != valueParam: rawObj[value][paramName] == valueParam)) depObjt[value] = rawObj[value];
});
return depObjt;
}
function readConfigData(path) {
return new Promise(sendJson => sendJson(filterByValue("hide", true, JSON.parse(fs.readFileSync(path, "utf8")), true)));
}
function writeRawState(data) {
fs.writeFile("state/state.js", data, function (err) {
if (err) {
return console.log(err);
}
console.log("The code was written!");
});
}
function filterArgsCommands(projtsJson) {
function processArgs(projtsJson) {
let depObjt = {};
process.argv.slice(2).forEach(value => {
depObjt[value] = projtsJson[value];
});
return depObjt;
}
return new Promise(sendJson => sendJson(!process.argv[2]?projtsJson:processArgs(projtsJson)));
}
readConfigData("model_conf.json")
.then(filterArgsCommands)
.then(getYoutubeData)
.then(generateSourceImages)
.then(writeRawState);