Skip to content

Commit

Permalink
New methods to get next cycle when available
Browse files Browse the repository at this point in the history
  • Loading branch information
marchchad committed Oct 27, 2020
1 parent c6b833a commit 1de1d1e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 17 deletions.
48 changes: 45 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
const terminalProcedures = require('./')

terminalProcedures.fetchCurrentCycle().then(r => console.log(r))
// Should log current cycle effective object
terminalProcedures.fetchCycle().then(c => {
console.log('current cycle effective object, no param')
console.dir(c)
})

// Should log current cycle effective object
terminalProcedures.fetchCycle('Current').then(c => {
console.log('current cycle effective object, with param')
console.dir(c)
})

// Should log next cycle effective object
terminalProcedures.fetchCycle('Next').then(c => {
console.log('next cycle effective object, with param')
console.dir(c)
})

// Should log current cycle code
terminalProcedures.fetchCurrentCycleCode().then(c => {
console.log('current cycle code')
console.log(c)
})

// Should log the current cycle effective dates
terminalProcedures.getCycleEffectiveDates().then(c => {
console.log('the current cycle effective dates, no param')
console.log(c)
})
// Should log the current cycle effective dates
terminalProcedures.getCycleEffectiveDates('Current').then(c => {
console.log('the current cycle effective dates, with param')
console.log(c)
})
// Should log the next cycle effective dates
terminalProcedures.getCycleEffectiveDates('Next').then(c => {
console.log('the next cycle effective dates')
console.log(c)
})

terminalProcedures.currentCycleEffectiveDates().then(console.log)
// Should log the current cycle effective dates
terminalProcedures.currentCycleEffectiveDates().then(c => {
console.log('the current cycle effective dates')
console.log(c)
})

// Also try updateing the Flag property to include one or more of the following:
// A for only those that were Added since the last effective date
// C for only those that were Changed since the last effective date
// D for only those that were Deleted since the last effective date
// Leave empty to get all regardless of if they've been added or changed
terminalProcedures.list('PANC', { flag: [ 'A', 'C' ], }).then(results => {
terminalProcedures.list('PANC', { flag: [ ], }).then(results => {
console.log(results)
const out = results.map(tp => {
return {
Expand Down
83 changes: 69 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ACCEPT = 'text/html'

const defaultQueryOptions = {
flag: [], // 'A' - Added, 'C' - Changed, 'D' - Deleted, Empty - All valid procedures
getNextCycle: false // `false` do not get the 'Next' cycle, only get the 'Current' cycle; `true` get the 'Next' cycle if available
}

/**
Expand All @@ -28,27 +29,77 @@ terminalProcedures.list = (icaos, options = defaultQueryOptions) => {
return listOne(icaos, options)
}

terminalProcedures.currentCycleEffectiveDates = async () => {
/**
* 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)

const $ = cheerio.load(response.text)
var currentCycle = $('select#cycle > option:contains(Current)').text()
const $cycle = $(`select#cycle > option:contains(${cycle})`)
if (!$cycle) {
return $cycle
}
return {
text: $cycle.text(),
val: $cycle.val()
}
}

/**
* Returns the text and values of the targeted <select/> element
* @param {string} cycle - The target cycle to retrieve. Valid values are 'Current' or 'Next'
*/
terminalProcedures.fetchCycle = fetchCycle

terminalProcedures.getCycleEffectiveDates = async (cycle = 'Current') => {
const { text: currentCycle, } = await fetchCycle(cycle)
return parseEffectiveDates(currentCycle.replace(/(\n|\t)/gm, ''))
}

terminalProcedures.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 fetchCurrentCycle = (terminalProcedures.fetchCurrentCycle = async () => {
const response = await superagent
.get(BASE_URL)
.set('Accept', ACCEPT)
const fetchCurrentCycleCode = async () => {
const cycle = await fetchCycle('Current')
if (!cycle) {
console.warn('Current cycle not found or not available.')
return null
}
return cycle.val
}

const $ = cheerio.load(response.text)
return $('select#cycle > option:contains(Current)').val()
})
terminalProcedures.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
}

terminalProcedures.fetchNextCycleCode = fetchNextCycleCode

/**
* Using the current cycle, fetch the terminal procedures for a single ICAO code
Expand All @@ -58,7 +109,7 @@ const fetchCurrentCycle = (terminalProcedures.fetchCurrentCycle = async () => {
* @returns {Array} - The scraped terminal procedures
*/
const listOne = async (icao, options) => {
const searchCycle = await fetchCurrentCycle()
const searchCycle = await fetchCurrentCycleCode()

// Build up a base set of query params
let urlParams = [ 'sort=type', 'dir=asc', `ident=${icao}`, ]
Expand Down Expand Up @@ -225,10 +276,14 @@ const parseEffectiveDates = str => {
const [ startMonthDay, remainder ] = str.split('-')
const [ endMonthDay, yearAndCycle ] = remainder.split(',')
const [ year, _ ] = yearAndCycle.split('[')
return {
effectiveStartDate: new Date(`${startMonthDay.trim()} ${year}`),
effectiveEndDate: new Date(`${endMonthDay.trim()} ${year}`)
}

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 }
}

/**
Expand Down

0 comments on commit 1de1d1e

Please sign in to comment.