-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
193 lines (176 loc) · 4.81 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
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
const superagent = require('superagent')
const cheerio = require('cheerio')
const nfdcBaseUri =
'https://www.faa.gov/airports/airport_safety/airportdata_5010'
const nfdcFacilitiesBaseUri = `${nfdcBaseUri}/menu/nfdcfacilitiesexport.cfm`
const nfdcRunwaysBaseUri = `${nfdcBaseUri}/menu/nfdcrunwaysexport.cfm`
const nfdcRemarksBaseUri = `${nfdcBaseUri}/menu/nfdcremarksexport.cfm`
const nfdcSchedulesBaseUri = `${nfdcBaseUri}/menu/nfdcschedulesexport.cfm`
/**
* Query parameter options; for possible options, see
* https://www.faa.gov/airports/airport_safety/airportdata_5010/menu/
*/
const defaultOptions = {
region: '',
district: '',
state: '',
county: '',
city: '',
use: '',
certification: ''
}
/**
* Main fetching method used for each data type
*/
const fetch = async (url, options = defaultOptions) => {
const queryParams = Object.assign({}, defaultOptions, options)
try {
const response = await superagent
.get(url)
.set('Accept', 'text/html')
.query(queryParams)
.retry(3)
.timeout({
response: 10e3,
deadline: 30e3
})
.buffer()
if (response.text) {
return parseData(response.text)
}
} catch (err) {
console.error(`Could not fetch data from ${url}`, err)
}
}
/**
* Extract the given form options from a cheerio instance
*/
const extractOptions = ($, id) => {
const $options = $(`select#${id} > option`)
const options = []
$options.each(o => options.push($options[o].attribs.value))
return options.filter(o => o !== '')
}
const capitalizeWord = str => `${str[0].toUpperCase()}${str.slice(1)}`
/**
* Main fetching method used for available form select options
*/
const fetchFormOptions = async () => {
try {
const response = await superagent
.get(nfdcBaseUri)
.set('Accept', 'text/html')
.timeout({
response: 10e3,
deadline: 30e3
})
.retry(3)
if (response.text) {
const $ = cheerio.load(response.text)
const options = {}
Object.keys(defaultOptions).map(optionName => {
options[optionName] = extractOptions($, capitalizeWord(optionName))
})
return options
}
} catch (err) {
console.error(`Could not fetch data from ${nfdcBaseUri}`, err)
}
}
/**
* Selection Form Options
*/
exports.regions = async () => {
const { region } = await fetchFormOptions()
return { regions: region }
}
exports.districts = async () => {
const { district } = await fetchFormOptions()
return { districts: district }
}
exports.states = async () => {
const { state } = await fetchFormOptions()
return { states: state }
}
exports.counties = async () => {
const { county } = await fetchFormOptions()
return { counties: county }
}
exports.cities = async () => {
const { city } = await fetchFormOptions()
return { cities: city }
}
exports.uses = async () => {
const { use } = await fetchFormOptions()
return { uses: use }
}
exports.certifications = async () => {
const { certification } = await fetchFormOptions()
return { certifications: certification }
}
/**
* Airport Facilities Data
*/
exports.facilities = async options => fetch(nfdcFacilitiesBaseUri, options)
/**
* Airport Runways Data
*/
exports.runways = async options => fetch(nfdcRunwaysBaseUri, options)
/**
* Airport Remarks Data
*/
exports.remarks = async options => fetch(nfdcRemarksBaseUri, options)
/**
* Airport Schedules Data
*/
exports.schedules = async options => fetch(nfdcSchedulesBaseUri, options)
/**
* Parse the raw delimited data into an array of objects
*/
const parseData = data => {
const rows = data
.split('\n')
.map(row => row.split('\t').map(cell => cell.trim()))
const columnTitles = rows[0].map(columnTitle =>
columnTitle.replace(/("|\r)/g, '')
)
const parsed = []
rows.slice(1).map(row => {
const parsedRow = {}
columnTitles.map((title, i) => {
parsedRow[title] =
row[i] === ''
? null
: title === 'LastOwnerInformationDate'
? row[i]
: tryParseNumber(row[i])
// The documents have single quotes around some fields
// that aren't needed so we can remove them here
if (parsedRow[title] && typeof parsedRow[title] === 'string') {
parsedRow[title] = parsedRow[title].replace(/'/, '')
}
})
// All entries in the 5010 listing have a site number, so this
// ensures we don't add any empty rows to the output
if (parsedRow.SiteNumber === null || parsedRow.SiteNumber === undefined) {
return
}
parsed.push(parsedRow)
})
return parsed
}
/**
* Attempt to parse a string as a number or return the original string
*/
const tryParseNumber = str => {
const number = Number(str)
const float = parseFloat(number)
if (float === number) {
return float
}
const int = parseInt(number)
if (int === number) {
return int
}
return str
}