-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (73 loc) · 2.38 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
#!/usr/bin/env node
"use strict"
let fs = require('fs')
let request = require('request')
let commandLineArgs = require('command-line-args')
let optionDefinitions = [
{ name: 'level', alias: 'l', type: String, defaultValue: 'state' },
{ name: 'state', alias: 's', type: String },
{ name: 'endpoint', alias: 'e', type: String, defaultValue: 'acs' },
{ name: 'county', alias: 'c', type: String },
{ name: 'out', alias: 'o', type: String },
{ name: 'year', alias: 'y', type: String, defaultValue: '2017' },
{ name: 'fields', alias: 'f', type: String },
{ name: 'key', alias: 'k', type: String }
]
function parseFields(f) {
let fieldsArray = f.split(',')
let parsedFields = []
for (let i = 0; i < fieldsArray.length; i++) {
if (fieldsArray[i].length == 6 ) {
parsedFields.push(fieldsArray[i] + '_001E')
} else {
parsedFields.push(fieldsArray[i])
}
}
return parsedFields.join(',')
}
let options = commandLineArgs(optionDefinitions)
let year = options.year
let endpoint = options.endpoint
let inArgs = ''
let apiKey = ''
let endpointUrl = ''
let fields = ''
if (options.state && !options.county) {
inArgs = '&in=state:' + options.state
} else if (options.state && options.county) {
inArgs = '&in=state:' + options.state + '%20county:' + options.county
} else if (!options.state && options.county) {
console.log('Please specify a state FIPS code using the -s argument')
}
if (options.key) {
apiKey = '&key=' + options.key
}
if (options.endpoint === 'acs') {
endpointUrl = 'acs/acs5'
fields = parseFields(options.fields)
} else if (options.endpoint === 'dc') {
endpointUrl = 'dec/sf1'
if (year === '2000' || year === '2010') {
year = options.year
fields = options.fields
} else {
console.log('Please specify a valid Census year (multiples of 10, yo)')
process.exit(1);
}
}
let requestUrl = 'https://api.census.gov/data/' + year + '/' + endpointUrl + '?get=' + fields + '&for=' + options.level + ':*' + inArgs + apiKey
//console.log(requestUrl)
request(requestUrl, function (error, response, body) {
if (error) {
console.log(error)
} else if (!error && response.statusCode == 200) {
let payload = JSON.parse(body)
if (options.out) {
fs.writeFileSync(options.out, payload.map(row => row.join(',')).join('\n'))
} else {
console.log(payload.map(row => row.join(',')).join('\n'))
}
} else {
console.log(body)
}
})