-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·133 lines (114 loc) · 3.71 KB
/
index.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
#!/usr/bin/env node
'use strict';
const jsonStatRepl = require('jsonstat-repl');
const fetch = require('node-fetch');
const fs = require('fs');
const argv = require('yargs')
.usage('$0 <table-id> [args]')
.option('limit', {
alias: 'l',
describe: 'Only fetch the first N values of each variable (to reduce size)',
type: 'number'
})
.option('exclude-elimination', {
alias: 'ee',
describe: 'Ignore variables that can be eliminated (to reduce size)',
type: 'boolean'
})
.option('metadata', {
alias: 'm',
describe: 'Only print metadata from the given table',
type: 'boolean'
})
.option('query', {
alias: 'q',
describe: 'Path to file that contains the JSON query you want to execute.',
type: 'string'
})
.option('debug', {
alias: 'd',
describe: 'Enable debug logging',
type: 'boolean'
})
.string('_').demand(1)
.help('help')
.argv;
const errors = {
tooBig: 'The response may be too big. Use [--limit n] to only fetch the n first values of each dimension, or [--exclude-elimination] to exclude all variables that can be eliminated.'
}
if (argv.debug) {
process.env.DEBUG = [process.env.DEBUG, 'ssb-repl'].join(',');
}
const debug = require('debug')('ssb-repl');
debug(argv);
const tableId = argv._[0];
const HEADERS = {
'User-Agent': 'ssb-repl',
Accept: 'application/json'
}
const apiUrl = 'http://data.ssb.no/api/v0/no/table/' + tableId;
const jsonify = (method, res) => {
if (res.ok) {
return res.json();
} else {
return res.text().then(body => {
debug(body);
const err = new Error('unable to ' + method + ' ' + res.url + ' for table ' + tableId + ': ' + res.status + ' ' + res.statusText);
err.httpCode = res.status;
err.httpBody = res.body;
return Promise.reject(err);
});
}
}
// fetch metadata for the table
debug(apiUrl)
fetch(apiUrl, {headers: HEADERS})
.then(jsonify.bind(null, 'GET'))
.then(metadata => {
if (argv.metadata) {
// just print metadata and exit
console.log(JSON.stringify(metadata, null, 2))
return;
}
if (metadata.variables) {
let variables = metadata.variables;
if (argv.excludeElimination) {
variables = variables.filter(v => !v.elimination)
}
let query;
if (argv.query) {
query = JSON.parse(fs.readFileSync(argv.query, 'utf-8'));
} else {
query = {
query: variables.map(v => ({
code: v.code,
selection: (argv.limit ? { filter: 'item', values: v.values.slice(0, +argv.limit) } : { filter: 'all', values: ['*'] })
})),
response: {
format: 'json-stat'
}
};
}
const body = JSON.stringify(query, null, 2);
debug(apiUrl);
debug(body);
return fetch(apiUrl, {
method: 'POST',
body: body,
headers: HEADERS
})
.then(jsonify.bind(null, 'POST'))
.then(jsonStatRepl.start)
.catch(err => {
if (err.httpCode === 403) {
console.error(err.message);
console.error(errors.tooBig);
} else {
return Promise.reject(err);
}
})
} else {
console.error('invalid table\n\n', metadata);
}
})
.catch(err => console.error(err.message));