From e062dec258935b3ad84c85e46a6718e470504f75 Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Tue, 17 May 2022 19:23:00 +0100 Subject: Huge refactor from before I put this project down... not sure why but I shall work with it! --- flightquery.sh | 94 +++++++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/flightquery.sh b/flightquery.sh index 6a84d54..7bf67f2 100755 --- a/flightquery.sh +++ b/flightquery.sh @@ -35,99 +35,72 @@ _getdata() { _airport() { _getdata 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 + # Get arrivals before current time and sort (we reverse so that they're in order of most recent to least recent) and extract only the latest two flights + flightsBefore=$(echo $result | jq --arg currentTime "$currentTime" '.data | map(select(.arrival.scheduled < $currentTime)) | sort_by(.arrival.scheduled)') + flightsBefore=$(echo $flightsBefore | jq '.[-2:]') + # Get arrivals after current time and sort and extract only the following three flights flightsAfter=$(echo $result | jq --arg currentTime "$currentTime" '.data | map(select(.arrival.scheduled > $currentTime)) | sort_by(.arrival.scheduled)') + flightsAfter=$(echo $flightsAfter | jq '.[:3]') + # Now merge the two objects + flights=$(echo $flightsBefore; echo $flightsAfter | jq -s add) + #flights=$(jq -s . "$flightsBefore" "$flightsAfter") + echo $flights | jq . + exit - # Build output string of the two arrivals preceeding the current time - for i in {0..1} - do - # Get origin/destination, airline and IATA flight number - if [[ $direction == "Origin" ]] - then - airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.iata') - else - airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.iata') - 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') - deptTimeType="ATD" - 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') - deptTimeType="ETD" - else - takeoff=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.scheduled') - deptTimeType="STD" - 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') - arrTimeType="ATA" - elif [[ $(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') != "null" ]] + for i in {0..4} + do + # Swap between flightsBefore and flightsAfter + if [[ $i > 1 ]] && [[ $swapped != "True" ]] then - landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') - arrTimeType="ETA" + flights=$flightsAfte + swapped="True" + count=0 else - landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.scheduled') - arrTimeType="STA" + flights=$flightsBefore fi - # Append to $output - output="$output $airline, $flightIATA, $direction: $airport, $deptTimeType: $(date -d $takeoff +"%d/%m/%Y %H:%M"), $arrTimeType: $(date -d $landing +"%d/%m/%Y %H:%M") |" - done - - # Build output string of the three arrivals immediately after the current time - for i in {0..2} - do # Get origin/destination, airline and IATA flight number if [[ $direction == "Origin" ]] then - airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.iata') + airport=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.iata') else - airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.iata') + airport=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.iata') 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') - airline=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].airline.name') - flightIATA=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].flight.iata') + airline=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].airline.name') + flightIATA=$(echo $flightsBefore | jq -r --arg count $count '.[$count | 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" ]] + if [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.actual') != "null" ]] then - takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.actual') + takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.actual') deptTimeType="ATD" - elif [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.estimated') != "null" ]] + elif [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.estimated') != "null" ]] then - takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.estimated') + takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.estimated') deptTimeType="ETD" else - takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.scheduled') + takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.scheduled') deptTimeType="STD" fi - if [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.actual') != "null" ]] + if [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.actual') != "null" ]] then - landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.actual') + landing=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.actual') arrTimeType="ATA" - elif [[ $(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') != "null" ]] + elif [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.estimated') != "null" ]] then - landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.estimated') + landing=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.estimated') arrTimeType="ETA" else - landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].arrival.scheduled') + landing=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.scheduled') arrTimeType="STA" fi # Append to $output output="$output $airline, $flightIATA, $direction: $airport, $deptTimeType: $(date -d $takeoff +"%d/%m/%Y %H:%M"), $arrTimeType: $(date -d $landing +"%d/%m/%Y %H:%M") |" + ((count=count+1)) done # remove trailing pipe @@ -195,7 +168,6 @@ then _help elif [[ ${input} == "--source" ]] || [[ ${input} == "-s" ]] then - # TODO add real source echo "https://www.blatech.co.uk/ars/flight-query" exit 0 elif [[ ${input} =~ "-a" ]] || [[ ${input} =~ "--arrivals" ]] -- cgit v1.2.3