forked from fdesjardins/chart-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
245 lines (210 loc) · 6.47 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const superagent = require('superagent')
const cheerio = require('cheerio')
const BASE_URL = 'https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dafd/search/'
// For some reason the server takes forever to respond without this request header
const ACCEPT = 'text/html'
const defaultQueryOptions = {
getNextCycle: false // `false` do not get the 'Next' cycle, only get the 'Current' cycle; `true` get the 'Next' cycle if available
}
/**
* Provide a shortcut to the list method
*/
const chartSupplements = module.exports = (icaos, options = defaultQueryOptions) => {
return chartSupplements.list(icaos, options)
}
/**
* Main listing method; accepts one or more ICAO codes
*/
chartSupplements.list = (icaos, options = defaultQueryOptions) => {
if (Array.isArray(icaos)) {
return Promise.all(icaos.map(icao => listOne(icao, options)))
}
return listOne(icaos, options)
}
chartSupplements.currentCycleEffectiveDates = async () => {
const response = await superagent
.get(BASE_URL)
.set('Accept', ACCEPT)
.timeout({ deadline: 30000 })
.retry(3)
const $ = cheerio.load(response.text)
var currentCycle = $('select#cycle > option:contains(Current)').text()
return parseEffectiveDates(currentCycle.replace(/(\n|\t)/gm, ''))
}
/**
* Returns the text and values of the targeted <select/> element
* @param {string} cycle - The target cycle to retrieve. Valid values are 'Current' or 'Next'
*/
const fetchCycle = async (cycle = 'Current') => {
// Only all the values 'Current' or 'Next'
if (cycle !== 'Current' && cycle !== 'Next') {
cycle = 'Current'
}
const response = await superagent
.get(BASE_URL)
.set('Accept', ACCEPT)
.timeout({ deadline: 30000 })
.retry(3)
const $ = cheerio.load(response.text)
const $cycle = $(`select#cycle > option:contains(${cycle})`)
if (!$cycle) {
return $cycle
}
return {
text: $cycle.text(),
val: $cycle.val()
}
}
chartSupplements.fetchCycle = fetchCycle
chartSupplements.getCycleEffectiveDates = async (cycle = 'Current') => {
const { text: currentCycle, } = await fetchCycle(cycle)
return parseEffectiveDates(currentCycle.replace(/(\n|\t)/gm, ''))
}
chartSupplements.currentCycleEffectiveDates = async () => {
const { text: currentCycle, } = await fetchCycle()
if (!currentCycle) {
console.warn('Could not retrieve current cycle effective dates')
return
}
return parseEffectiveDates(currentCycle.replace(/(\n|\t)/gm, ''))
}
/**
* Fetch the current diagrams distribution cycle numbers (.e.g, 1813)
*/
const fetchCurrentCycleCode = async () => {
const cycle = await fetchCycle('Current')
if (!cycle) {
console.warn('Current cycle not found or not available.')
return null
}
return cycle.val
}
chartSupplements.fetchCurrentCycleCode = fetchCurrentCycleCode
/**
* Fetch the current diagrams distribution cycle numbers (.e.g, 1813)
*/
const fetchNextCycleCode = async () => {
const cycle = await fetchCycle('Next')
if (!cycle) {
console.warn('Next cycle not found or not available. The Next cycle is available 19 days before the end of the current cycle.')
return null
}
return cycle.val
}
chartSupplements.fetchNextCycleCode = fetchNextCycleCode
/**
* Fetch chart supplements for a single ICAO code
*/
const listOne = async (icao, options) => {
let searchCycle = null
if (options.getNextCycle === true) {
searchCycle = await fetchNextCycleCode()
}
// If the next cycle is not requested, or it is, but it is not
// available, default to the current cycle
if (searchCycle === null) {
if (options.getNextCycle === true) {
console.warn('Next cycle not available. Retrieving current cycle instead.')
}
searchCycle = await fetchCurrentCycleCode()
}
// Build up a base set of query params
let urlParams = [ `ident=${icao}`, ]
// The searchCycle is optional as the API assumes the latest already
// and this function uses the latest cycle
if (searchCycle) {
urlParams.push(`cycle=${searchCycle}`)
}
const res = await superagent
.get(`${BASE_URL}results/?cycle=${searchCycle}&ident=${icao}&navaid=`)
.set('Accept', ACCEPT)
.timeout({ deadline: 30000 })
.retry(3)
return await parse(res.text, options.getNextCycle)
}
/**
* Parsing helper methods
*/
const text = ($row, columnIndex) =>
$row
.find(`td:nth-child(${columnIndex})`)
.text()
.trim()
const link = ($row, columnIndex) =>
$row
.find(`td:nth-child(${columnIndex})`)
.find('a')
.attr('href')
/**
* Extract the relevant information from the dom node and return
* an object with the data mapped by the appropriate named key
* @param {HTMLNode} $row - The dom node that contains the tabular data
*/
const extractRow = ($row) => {
const type = text($row, 7)
if (!type) {
return null
}
return {
ident: text($row, 1),
city: text($row, 2),
state: text($row, 3),
airport: text($row, 4),
navAid: text($row, 5),
chart: text($row, 6),
volBackPages: {
name: text($row, 7),
url: link($row, 7)
},
airportNavAidListing: {
name: text($row, 8),
url: link($row, 8)
}
}
}
/**
* Parse the documents out of the response HTML
*/
const parse = async (html, isNextCycle) => {
const $ = cheerio.load(html)
const $resultsTable = $('#resultsTable')
const noResultsFound = $('.message-box.info').text().trim()
if (!!noResultsFound && noResultsFound === 'No results found.') {
console.warn(noResultsFound)
return null
}
else if (!$resultsTable.html()) {
console.error('Unable to parse the #resultsTable page element')
return null
}
const { effectiveStartDate, effectiveEndDate, } = await chartSupplements.getCycleEffectiveDates(isNextCycle ? 'Next': 'Current')
const results = $resultsTable
.find('tr')
.toArray()
.map(row => {
var row = extractRow($(row))
if (!row) {
return row
}
return {
...row,
effectiveStartDate,
effectiveEndDate
}
})
.filter(x => !!x)
return results
}
const parseEffectiveDates = str => {
if (!str) {
return null
}
const [ startMonthDay, remainder ] = str.split('-')
const [ endMonthDay, yearAndCycle ] = remainder.split(',')
const [ year, _ ] = yearAndCycle.split('[')
const effectiveStartDate = new Date(`${startMonthDay.trim()} ${year}`)
effectiveStartDate.setUTCHours(0,0,0,0)
const effectiveEndDate = new Date(`${endMonthDay.trim()} ${year}`)
effectiveEndDate.setUTCHours(0,0,0,0)
return { effectiveStartDate, effectiveEndDate }
}