-
Notifications
You must be signed in to change notification settings - Fork 16
/
merge.js
74 lines (66 loc) · 1.86 KB
/
merge.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
/**
* Given multiple input files (JSON/KML), merge them into one large JSON file (the Takeout format).
*
* An example to use this script:
*
* % find DIR -name "*.json" -o -name ""*.kml" -exec echo -n '"{}" ' \; | \
* xargs nodejs merge.js -o OUT_FILE.json
*
*/
const fs = require("fs");
const yargs = require("yargs");
const xml_parser = require('fast-xml-parser');
const file_parser = require('../parsers.js');
const STDOUT = process.stdout;
const STDERR = process.stderr;
const argv = yargs
.demandCommand(1)
.option('output', {
alias: 'o',
description: 'Specify the output filename',
type: 'string',
})
.option('pretty', {
description: 'pretty output',
type: 'boolean',
})
.usage('Usage: $0 input_file ... [options]')
.help().alias('help', 'h')
.argv;
function main() {
// Read inputs from each file listed in arguments.
let all_points = [];
argv._.forEach((input_filename) => {
const input_text = fs.readFileSync(input_filename, 'utf-8');
const ps = file_parser.parseFile(input_filename, input_text);
console.log(`Loaded ${ps.length} points from ${input_filename}`);
all_points = all_points.concat(ps);
});
// Convert it to Takeout format
let out_obj = [];
for (let p of all_points) {
out_obj.push({
placeVisit: {
location: {
latitudeE7: p.lat * 1e7,
longitudeE7: p.lng * 1e7,
name: p.name,
},
duration: {
startTimestampMs: p.begin * 1000,
endTimestampMs: p.end * 1000,
}
}
});
}
// Output to file (or STDOUT)
let out_file = argv.output ? argv.output : "/dev/stdout";
let space = argv.pretty ? 2 : 0;
let out_text = JSON.stringify({
timelineObjects: out_obj,
}, null, space);
fs.writeFile(out_file, out_text, function (err) {
if (err) throw err;
});
}
main();