From cdc3152aa3c16d640f225ece8bc8391a22577024 Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Mon, 2 Nov 2020 17:47:06 +0000 Subject: Merge _departures and _arrivals. Filter output to two flights preceeding current time and three flights after and output on single line. Make output time more reasonable in _flight. --- flightquery.sh | 125 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 34 deletions(-) diff --git a/flightquery.sh b/flightquery.sh index bb4ed2c..19ea694 100755 --- a/flightquery.sh +++ b/flightquery.sh @@ -30,36 +30,91 @@ _getdata() { _curl } -_arrivals() { +# TODO: refactor to remove repetitive loops + +_airport() { _getdata - # Extract .data and sort by scheduled landing - result=$(echo $result | jq '.data | sort_by(.arrival.scheduled)') - # TODO: Limit to a certin number of flights (X before current time and Y after?) and format on single line of output - for json in $(echo "${result}" | jq -r '.[] | @base64') + currentTime=$(date --iso-8601=seconds) + # Get arrivals before current time and sort (we reverse so that they're in order of most recent to least recent) + flightsBefore=$(echo $result | jq --arg currentTime "$currentTime" '.data | map(select(.arrival.scheduled < $currentTime)) | sort_by(.arrival.scheduled) | reverse') + # Get arrivals after current time and sort + flightsAfter=$(echo $result | jq --arg currentTime "$currentTime" '.data | map(select(.arrival.scheduled > $currentTime)) | sort_by(.arrival.scheduled)') + + # Build output string of the two arrivals preceeding the current time + for i in {0..1} do - origin=$(_jq '.departure.airport') - takeoffSched=$(_jq '.departure.scheduled') - landingSched=$(_jq '.arrival.scheduled') - airline=$(_jq '.airline.name') - flightNumber=$(_jq '.flight.number') - echo "$airline $flightNumber Origin: $origin Takeoff: $takeoffSched Landed: $landingSched" + # Get origin/destination, airline and IATA flight number + if [[ $direction == "Origin" ]] + then + airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.airport') + else + airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.airport') + fi + airline=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].airline.name') + flightIATA=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].flight.iata') + + # Get departure and takeoff times using actual or estimated times where available or falling back to scheduled if not. + if [[ $(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.actual') != "null" ]] + then + takeoff=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.actual') + elif [[ $(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.estimated') != "null" ]] + then + takeoff=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.estimated') + else + takeoff=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.scheduled') + fi + + if [[ $(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.actual') != "null" ]] + then + landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.actual') + elif [[ $(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') != "null" ]] + then + landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') + else + landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.scheduled') + fi + + # Append to $output + output="$output $airline $flightIATA $direction: $airport Takeoff: $(date -d $takeoff +"%d/%m/%Y %H:%M") Landed: $(date -d $landing +"%d/%m/%Y %H:%M") |" done -} -_departures() { - _getdata - # Extract .data and sort by scheduled takeoff - result=$(echo $result | jq '.data | sort_by(.departure.scheduled)') - # TODO: Limit to a certin number of flights (X before current time and Y after?) and format on single line of output - for json in $(echo "${result}" | jq -r '.[] | @base64') + # Build output string of the three arrivals immediately after the current time + for i in {0..2} do - destination=$(_jq '.arrival.airport') - takeoffSched=$(_jq '.departure.scheduled') - landingSched=$(_jq '.arrival.scheduled') - airline=$(_jq '.airline.name') - flightNumber=$(_jq '.flight.number') - echo "$airline $flightNumber Destination: $destination Takeoff: $takeoffSched Landed: $landingSched" + # Get origin/destination, airline and IATA flight number + origin=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.airport') + airline=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].airline.name') + flightIATA=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].flight.iata') + + # Get departure and takeoff times using actual or estimated times where available or falling back to scheduled if not. + if [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.actual') != "null" ]] + then + takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.actual') + elif [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.estimated') != "null" ]] + then + takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.estimated') + else + takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.scheduled') + fi + + if [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.actual') != "null" ]] + then + landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.actual') + elif [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') != "null" ]] + then + landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') + else + landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.scheduled') + fi + + # Append to $output + output="$output $airline $flightIATA Origin: $origin Takeoff: $(date -d $takeoff +"%d/%m/%Y %H:%M") Landing: $(date -d $landing +"%d/%m/%Y %H:%M") |" done + + # remove trailing pipe + output=$(echo $output | sed '$s/|$//') + echo $output + exit } _flight() { @@ -80,33 +135,33 @@ _flight() { output="$airline - Origin: $origin " if [[ "$takeoffActual" != "null" ]] then - output="$output $takeoffActual" + output="$output $(date -d $takeoffActual +"%d/%m/%Y %H:%M")" elif [[ "$takeoffEst" != "null" ]] then - output="$output $takeoffEst" + output="$output $(date -d $takeoffEst +"%d/%m/%Y %H:%M")" else - output="$output $takeoffSched" + output="$output $(date -d $takeoffSched +"%d/%m/%Y %H:%M")" fi if [[ "$takeoffDelay" != "null" ]] then - output="$output Delay: $takeoffDelay" + output="$output Delay: $takeoffDelay mins" fi output="$output Dest: $destination" if [[ "$landingActual" != "null" ]] then - output="$output $landingActual" + output="$output $(date -d $landingActual +"%d/%m/%Y %H:%M")" elif [[ "$landingEst" != "null" ]] then - output="$output $landingEst" + output="$output $(date -d $landingEst +"%d/%m/%Y %H:%M")" else - output="$output $landingSched" + output="$output $(date -d $landingSched +"%d/%m/%Y %H:%M")" fi if [[ "$landingDelay" != "null" ]] then - output="$output Delay: $landingDelay" + output="$output Delay: $landingDelay mins" fi echo $output @@ -127,11 +182,13 @@ then elif [[ ${input} =~ "-a" ]] || [[ ${input} =~ "--arrivals" ]] then queryType="arr_iata" - _arrivals + direction="Origin" + _airport elif [[ ${input} =~ "-d" ]] || [[ ${input} =~ "--departures" ]] then + direction="Destination" queryType="dep_iata" - _departures + _airport # if it doesn't match an option then it's probably a specific plane else queryType="flight_iata" -- cgit v1.2.3