forked from opening-hours/opening_hours.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_for_new_taginfo_data.js
executable file
·83 lines (69 loc) · 2.53 KB
/
check_for_new_taginfo_data.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
#!/usr/bin/env node
/* Constant variables {{{ */
var taginfo_api_base_url = 'https://taginfo.openstreetmap.org/api/4/';
var exit_code_new = 0;
var exit_code_not_new = 1;
/* Parameter handling {{{ */
var optimist = require('optimist')
.usage('Usage: $0')
.describe('h', 'Display the usage')
.describe('E', 'Specifiy the exit code in case there is no new data. The default value is ' + exit_code_not_new + '.')
.alias('h', 'help')
.alias('E', 'exit-code-not-new');
var argv = optimist.argv;
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
if (typeof argv.E === 'number') {
exit_code_not_new = argv.E;
} else if (typeof argv.E !== 'undefined') {
optimist.showHelp();
process.exit(0);
}
/* }}} */
/* }}} */
/* Required modules {{{ */
var https = require('https');
var fs = require('fs');
/* }}} */
/* Helper functions {{{ */
function get_dump_creation_time_from_file(file) {
try {
var data = JSON.parse(fs.readFileSync(file, 'utf8'));
for (var i = 0; i < data.length; i++) {
if (data[i].name === 'Database') {
return new Date(data[i].data_until);
}
}
} catch (err) {
return;
}
}
/* }}} */
var local_dump_creation_time = get_dump_creation_time_from_file('taginfo_sources.json');
/* Download source file and compare {{{ */
var taginfo_api_url_source = taginfo_api_base_url + 'site/sources';
console.log('Loading file ' + taginfo_api_url_source + ' to check if new data is availale.');
var file = fs.createWriteStream('taginfo_sources.json');
var request = https.get(taginfo_api_url_source, function(response) {
response.pipe(file);
response.on('error', function(err) {
throw("Got error: " + err.message);
});
response.on('end', function() {
var upstream_dump_creation_time = get_dump_creation_time_from_file('taginfo_sources.json');
if (typeof local_dump_creation_time === 'object')
console.log("Local taginfo data was generated on: " + local_dump_creation_time);
if (typeof local_dump_creation_time === 'object'
&& local_dump_creation_time.getTime() === upstream_dump_creation_time.getTime()) {
console.log("Not newer then local data.");
process.exit(exit_code_not_new);
} else {
console.log("New data available …");
console.log("Taginfo data was generated on: " + upstream_dump_creation_time.toISOString());
process.exit(exit_code_new);
}
});
});
/* }}} */