-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhubdump.js
executable file
·616 lines (527 loc) · 20.6 KB
/
dhubdump.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*jslint node: true */
/*jslint es5: true */
/*jslint nomen: true */
/*global setImmediate */
"use strict";
var wapi = require('./lib/winapi'),
rwcsv = require('./lib/rwcsv'),
jspage = require('./lib/jspage'),
fs = require('graceful-fs'),
path = require('path'),
argv = require('yargs'),
async = require('async'),
moment = require('moment'),
settings,
win,
outDir,
dumps,
work = [],
maxopen,
first_ts,
done = {report: [], count: { xml: 0, json: 0}},
reportcsv,
claimsmode = false,
timeinc,
include_intermediates_in_tourtypes = false,
current_ts = "current", //moment().format("YYYYMMDDTHHmmss"),
pageLimits = {'json': -1, 'xml': -1},
FORMATS = {xml: "asXML", json: "asJSON"},
PERIODS = {week: 7, day: 1},
PUBSTATES = {all: "ignorePublished", pub: "published" },
PRODUCTS = ['accommodation', 'permanent_offering', 'reca', 'temporary_offering', 'mice'],
CHANNELS = ['westtoer', 'fietsen_en_wandelen', 'kenniscentrum', 'dagtrips_voor_groepen',
'flanders_fields', 'flanders_fields_nl', 'flanders_fields_fr', 'flanders_fields_en', 'flanders_fields_de',
'leiestreek', 'leiestreek_nl', 'leiestreek_fr', 'leiestreek_en', 'leiestreek_de',
'de_kust', 'de_kust_nl', 'de_kust_en', 'de_kust_fr', 'de_kust_de',
'brugse_ommeland', 'brugse_ommeland_nl', 'brugse_ommeland_fr', 'brugse_ommeland_de', 'brugse_ommeland_en',
'westhoek', 'westhoek_nl', 'westhoek_fr',
'meetingkust', 'meetingkust_nl', 'meetingkust_fr',
'300_jaar_grens', '300_jaar_grens_nl', '300_jaar_grens_fr', '300_jaar_grens_en', '300_jaar_grens_de',
'autoroutes', 'itrip_coast', 'kustwandelroute', 'west-vlinderen', 'iedereen_flandrien'],
TOURTYPES = ["hotel", "bed_breakfast", "camping", "minicamping", "camping_grounds", "residence_park", "hiking_cabin",
"holiday_park", "youth_accommodation", "holiday_centre", "holiday_cottage", "holiday_accommodation", "belfry",
"historic_building", "monument", "religious_building", "forest", "domain", "park_garden", "battle_field_tour",
"belgian_cemetery", "commonwealth_cemetery", "german_cemetery", "french_cemetery", "american_cemetery",
"front_flight", "war_site", "cinema", "visitor_centre", "casino", "cultural_centre", "childrens_farm", "museum",
"shop_store", "wellness", "brewery", "farm_to_taste", "distillery", "vineyard", "bike_rental", "covered_wagon",
"riding_school", "adventure", "golf", "sport_accommodation", "swimming", "scooter_solex_rental", "dock",
"boat_rental", "marina", "canoe_kayak_rental", "beach_club", "theme_park", "amusement_park", "playground",
"tourist_office", "bistro", "cafe", "restaurant", "tearoom", "concert", "festival", "temporary_offering_biking",
"film", "commemorative_ceremony", "ice_rink", "shopping", "special_market", "sports", "procession", "exhibition",
"theater", "fireworks", "walking", "temporary_offering_water_recreation", "weekly_market", "undefined", "room"],
CITIES = ["Koksijde", "Oostende", "Poperinge", "Heuvelland", "Brugge"];
settings = argv
.usage('Maakt een dump op basis van de win2 API.\nUsage: $0')
.example('$0 -o ~/feeds/win/2.0/ -s secret [dumpspecs*]',
'Maakt de product-dump van de gekozen categoriën (dumpspecs) en plaatst die in de aangegeven output-folder.\n' +
'Wanneer geen dumps worden gedspecifieerd worden "products vocs samples" verondersteld.\n' +
'Numerieke dumpspecs worden aanzien als "id" van de uniek op te halen items.\n' +
'Let op: de dump van "token" is exclusief al de andere en retourneert gewoon snel een accesstoken dan 1h lang gebruikt kan worden.')
.describe('clientid', 'id of the client - requires matching secret')
.alias('i', 'clientid')
.describe('secret', 'het secret.')
.alias('s', 'secret')
.describe('server', 'Server to talk to (hostname or IP)')
.alias('S', 'server')
.describe('protocol', 'Protocol to use http or https')
.alias('P', 'protocol')
.describe('output', 'folder waar de dump wordt geplaatst.')
.alias('o', 'output')
.describe('report', 'naam van de report.csv')
.alias('r', 'report')
.default('r', 'dhubdump-report')
.boolean('verbose')
.describe('verbose', 'should there be a lot of logging output')
.alias('v', 'verbose')
.default('v', false)
.describe('progress', 'should there be a logging of the progress')
.alias('p', 'progress')
.default('p', false)
.describe('timebetween', 'wait this many millis between progressing to next task')
.alias('t', 'timebetween')
.default('t', 100) //10 tasks (20 requests) per second
.describe('maxopen', 'control not having more then this many open connections - wait starting new ones')
.alias('m', 'maxopen')
.default('m', 5) //5 simultaneous requests
.boolean('claimsmode')
.describe('claimsmode', 'performs id-lookups on the claims-api AND removes all filters on the claims dump')
.alias('C', 'claimsmode')
.default('C', false)
.describe('jsonpage', 'force paging those sets that are bigger then jsonpage value')
.alias('j', 'jsonpage')
.default('j', -1)
.demand(['secret', 'output'])
.argv;
win = wapi.client({secret: settings.secret, clientid: settings.clientid, verbose: settings.verbose, server: settings.server, protocol: settings.protocol});
outDir = settings.output;
timeinc = settings.timebetween;
maxopen = settings.maxopen;
claimsmode = !!(settings.claimsmode);
dumps = settings._ && settings._.length ? settings._ : ['vocs', 'products'];
reportcsv = settings.report + ".csv";
pageLimits.json = settings.jsonpage;
//pageLimits.xml = settings.xmlpage; // not yet foreseen
function contains(arr, item) {
return (arr.indexOf(item) >= 0);
}
function addTask(task) {
work.push(task);
}
function nameJoin() {
return [].reduce.call(arguments, function (name, part) { return (name.length ? name + "-" : "") + part; }, "");
}
function reportDone(ext, task, status, uri, ts_start, open_start, size, mime) {
var ts_end = moment(), open_end, duration, elapse;
done.count[ext] += 1;
work.open -= 1;
open_end = work.open;
duration = ts_end.diff(ts_start);
elapse = ts_start.diff(first_ts);
done.report.push([
elapse,
ts_start.toISOString(),
ts_end.toISOString(),
duration,
task.dir,
task.name + "." + ext,
task.query.resources.join('|'),
task.query.touristictypes.join('|'),
task.query.channels.join('|'),
task.query.lastmodRange ? task.query.lastmodRange.gte : "",
task.query.softDelState,
task.query.pubState,
task.query.selectMunicipal,
status, uri,
open_start, open_end, size, mime
]);
if (settings.progress) {
console.log("done %s #%d/%d \t|started @ %d ms\t|open connections then: %d > now: %d\t|size = %d\t|mime = %s",
ext, done.count[ext], work.length, elapse, open_start, open_end, size, mime);
}
if (done.count.xml === work.length && done.count.xml === done.count.json) {
rwcsv.write(
path.join(outDir, reportcsv),
done.report,
[
"elapse (ms)", "ts_start", "ts_end", "duration (ms)", "dir", "name", "types", " touristic_types", "channels",
"lastmod GTE", "softDelState", "pubState", "municipality",
"status", "uri",
"open on start", "open on close", "content-length", "content-type"
]
);
}
}
function remove(fname) {
if (fs.existsSync(fname)) {
fs.unlinkSync(fname);
}
}
function size(fname) {
if (!fs.existsSync(fname)) {
return -1;
} // else
return fs.statSync(fname).size;
}
function jsonSplit(size, outDirName, name, fPath) {
jspage.doSplit(size, path.join(outDirName, name), name + "-", fPath);
//console.log("todo - split json file into chunks of %d at %s for file %s ", size, pathname, fname);
}
function splitPages(ext, outDirName, name, fPath) {
if (ext === "xml") {
return; // not supported yet
}
if (ext === "json" && pageLimits.json > 0) {
jsonSplit(pageLimits.json, outDirName, name, fPath);
}
}
function perform(task) {
var q = task.query,
outDirName = path.join(outDir, task.dir),
pathname = path.join(outDirName, task.name);
Object.keys(FORMATS).forEach(function (ext) {
var status, fmtMethod = FORMATS[ext],
fname = [pathname, ext].join('.'),
ts, open, uri,
sink = fs.createWriteStream(fname), qbf;
sink.on('error', function (e) {
remove(fname);
status = "error :" + e;
console.error("error saving " + pathname + "." + ext + "-->" + e);
});
qbf = q.clone().bulk()[fmtMethod]();
uri = qbf.getURI(win, true);
ts = moment();
open = work.open;
work.open += 1;
if (first_ts === undefined) { first_ts = ts; }
win.stream(qbf, sink, function (err, res, stream) {
if (err) {
status += "\ncallback Error: " + err;
}
if (status === undefined) {
//wait for completion to be able to size the file.
//this because apparently there is no content-length header
res.on('end', function () {
reportDone(ext, task, "ok", uri, ts, open, size(fname), res.headers['content-type']);
stream.on('finish', function() {
splitPages(ext, outDirName, task.name, fname);
});
});
} else {
reportDone(ext, task, status, uri, ts, open, -1, "");
}
});
});
}
function makePeriodTasks(pTask) {
var to = moment();
return function (period) {
var days = PERIODS[period],
from = to.clone().subtract(days, 'days'),
task = {dir : pTask.dir,
name : nameJoin(pTask.name, period, from.format('YYYYMMDD'), current_ts),
query: pTask.query.clone().lastmodBetween(from, null)};
addTask(task);
};
}
function makePubTasks(pTask) {
return function (pubKey) {
var pubMethod = PUBSTATES[pubKey],
task = {dir : pTask.dir,
name : nameJoin(pTask.name, pubKey),
query: pTask.query.clone()[pubMethod]()};
addTask(task);
Object.keys(PERIODS).forEach(makePeriodTasks(task));
};
}
function assertDirExists(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
function makeTourTypeTasks(pTask) {
var dir = path.join(pTask.dir, "bytourtype");
assertDirExists(path.join(outDir, dir));
return function (tourtype) {
var ldir = path.join(pTask.dir, "bytourtype"), //tourtype),
task = {dir : ldir,
name : nameJoin(pTask.name, tourtype),
query: pTask.query.clone().forTouristicTypes(tourtype)};
addTask(task);
Object.keys(PERIODS).forEach(makePeriodTasks(task));
};
}
function makeChannelTasks(pTask) {
assertDirExists(path.join(outDir, pTask.dir, "bychannel"));
return function (channel) {
var dir = path.join(pTask.dir, "bychannel", channel),
task = {dir : dir,
name : nameJoin(pTask.name, channel, "all"),
query: pTask.query.clone().forTypes(PRODUCTS).forChannels(channel + ".*")};
assertDirExists(path.join(outDir, dir));
addTask(task);
Object.keys(PERIODS).forEach(makePeriodTasks(task));
};
}
function makeProductTasks(pTask) {
return function (prod) {
var task = {dir : pTask.dir,
name : nameJoin(pTask.name, prod),
query: pTask.query.clone().forTypes(prod)};
Object.keys(PUBSTATES).forEach(makePubTasks(task));
};
}
function makeMunicipalityTasks(pTask) {
assertDirExists(path.join(outDir, pTask.dir, "bycity"));
return function (city) {
var dir = path.join(pTask.dir, "bycity", city.toLowerCase()),
task = {dir : dir,
name : nameJoin(pTask.name, city.toLowerCase()),
query: pTask.query.clone().municipality(city)};
assertDirExists(path.join(outDir, dir));
PRODUCTS.forEach(makeProductTasks(task));
};
}
function makeClaimsDump(unfiltered) {
var task, q = wapi.query('claim');
if (!unfiltered) {
q = q.requireFields(["claims.claim.owner.email_address"]);
}
task = {
dir: ".",
name: "claims",
query: q
};
addTask(task);
}
function makeStatsDump() {
var task = {
dir: ".",
name: "stats",
query: wapi.query('statistics')
};
addTask(task);
}
function makeVocDump() {
var task = {
dir: "reference",
name: "taxonomies",
query: wapi.query('vocabulary')
};
assertDirExists(path.join(outDir, task.dir));
addTask(task);
}
function makeIdTask(pTask) {
return function (id) {
var task = {dir : pTask.dir,
name : nameJoin(pTask.name, id),
query: pTask.query.clone().id(id)};
addTask(task);
};
}
function makeSingleClaimDump(id) {
var task = {
dir: "claims",
name: "claim",
query: wapi.query('claim')
};
assertDirExists(path.join(outDir, task.dir));
makeIdTask(task)(id);
}
function makeSingleIdDump(id) {
var task = {
dir: "samples",
name: "item",
query: wapi.query('product').forTypes(PRODUCTS)
};
assertDirExists(path.join(outDir, task.dir));
makeIdTask(task)(id);
}
function makeSampleIdsDump() {
var SAMPLEIDS = [49928, 33346, 53249, 53302, 53303, 53304, 53306, 53307, 53308, 53309, 53310, 53311, 53312, 53313, 53314, 53315, 53381, 53382, 53383, 53384, 34593, 33214, 33214, 33581, 33248, 35429, 36422, 31858, 53249],
task = {
dir: "samples",
name: "sample",
query: wapi.query('product').forTypes(PRODUCTS)
};
assertDirExists(path.join(outDir, task.dir));
SAMPLEIDS.forEach(makeIdTask(task));
}
function makeProductsDump() {
var task = {
dir: ".",
name: "",
query: wapi.query('product')
};
//subtask for all channels together
task.name = "allchannels";
task.query = task.query.clone().forTypes(PRODUCTS);
TOURTYPES.forEach(makeTourTypeTasks(task));
task.name = "";
//subtasks per city
CITIES.forEach(makeMunicipalityTasks(task));
//subtasks per channel
CHANNELS.forEach(makeChannelTasks(task));
//subtasks per products
PRODUCTS.forEach(makeProductTasks(task));
}
function loadReferences(done) {
var query = wapi.query('vocabulary').bulk().asJSON(),
LANGMATCH = new RegExp("(.*)_nl");
function rootNodes(lvl) {
return lvl.map(function (node) { return node.code; });
}
function leafNodes(lvl, res, intermediates, depth) {
depth = depth || 0;
res = res || [];
intermediates = intermediates || false;
if (lvl !== undefined && lvl !== null && lvl.length !== 0) {
lvl.forEach(function (node) {
var isleaf = true;
if (node.hasOwnProperty("children") && node.children.length > 0) {
leafNodes(node.children, res, intermediates, depth + 1);
isleaf = false;
}
if (isleaf || (intermediates && depth > 0)) {
res.push(node.code);
} else if (settings.verbose) {
console.log("skipping node %s - %d", node.code, node.children.length);
}
});
}
return res;
}
function getChannels(cb) {
var q = query.clone().vocname('publicatiekanalen');
win.fetch(q, function (err, obj) {
/*jslint unparam:true*/
try {
var trees = win.parseVocabularyTrees(obj),
list = leafNodes(trees.publicatiekanalen); // only retain low level children
list = list.reduce(function (extended, item) {
var m = LANGMATCH.exec(item);
if (m !== null) {
extended.push(m[1]); //add .* variant for whenever a match is found for language-vraiants (match _nl --> add chunked off variant to
}
extended.push(item);
return extended;
}, []);
CHANNELS = list;
} catch (e) {
console.error("ERROR Retrieving 'channels' dynamically - fallback to hardcoded...");
}
cb(null, "");
});
}
function getTypes(cb) {
var q = query.clone().vocname('product_types');
win.fetch(q, function (err, obj) {
/*jslint unparam:true*/
try {
var trees = win.parseVocabularyTrees(obj),
list = leafNodes(trees.product_types, [], include_intermediates_in_tourtypes);
// last param == false ==> only retain lowest level children (that themselves have no children)
// last param == true ==> retain all but root levels that have children
if (list.length === 0) {
throw new Error('list op types cannot be empty - throwing error to force fallback');
}
TOURTYPES = list;
if (settings.verbose) {
console.log("list of leaf (include intermediates = %s) tour types == %j", include_intermediates_in_tourtypes, TOURTYPES);
}
} catch (e) {
console.error("ERROR Retrieving 'tourtypes (leafs)' dynamically - fallback to hardcoded...");
console.error(e);
}
cb(null, "");
});
}
function getProductClasses(cb) {
var q = query.clone().vocname('product_types');
win.fetch(q, function (err, obj) {
/*jslint unparam: true */
try {
var trees = win.parseVocabularyTrees(obj),
list = rootNodes(trees.product_types); // only retain top level children
PRODUCTS = list;
if (settings.verbose) {
console.log("list of root product types == %j", PRODUCTS);
}
} catch (e) {
console.error("ERROR Retrieving 'producttypes (roots)' dynamically - fallback to hardcoded...");
console.error(e);
}
cb(null, "");
});
}
async.parallel([getChannels, getTypes, getProductClasses], function (err, result) {
/*jslint unparam: true */
done();
});
return;
}
function assembleWork() {
dumps.forEach(function (d) {
// decide which dump-tasks to add...
if (d === 'vocs') {
makeVocDump();
} else if (d === 'samples') {
makeSampleIdsDump();
} else if (d === 'claims') {
makeClaimsDump(claimsmode);
} else if (d === 'products') {
makeProductsDump();
} else if (d === 'stats') {
makeStatsDump();
} else if (!isNaN(Number(d))) {
if (claimsmode) {
makeSingleClaimDump(d);
} else {
makeSingleIdDump(d);
}
}
});
}
function doWork(done) {
var cnt = 0;
work.open = 0;
function doNext() {
if (cnt < work.length) {
if (work.open < maxopen - 1) { // process next
perform(work[cnt]);
cnt += 1;
}
setTimeout(doNext, timeinc);
} else {
done();
}
}
doNext();
}
function doDump() {
// decide what to do
if (contains(dumps, 'token')) {
console.log('token');
win.start(function () {
console.log("token = %s", win.token);
win.stop();
});
return; // if we request a token, do nothing else!
} //else
if (!fs.existsSync(outDir)) {
throw "Cannot dump to " + outDir + " - path does not exist.";
}
if (!fs.statSync(outDir).isDirectory()) {
throw "Cannot dump to " + outDir + " - path is not a directory.";
}
if (settings.verbose) {
console.log("Started win client with ts mark %s", current_ts);
}
win.start(function () {
loadReferences(function () {
assembleWork();
doWork(function () {win.stop(); });
});
});
}
doDump();