#!/bin/bash read input # read config file source flightquery.conf # curl options OPTS='--silent' # sadly HTTPS isn't supported on the free plan baseURL='http://api.aviationstack.com/v1/flights?access_key='$appKey # get data from API _curl() { result=$(curl $OPTS "$URL") ret=$? if [ "$ret" -ne 0 ] then echo "cURL error $ret when fetching." exit fi } _jq() { echo ${json} | base64 --decode | jq -r ${1} } _getdata() { # Take everything after final space as the requested input (be it an IATA airport code or IATA flight number), assuming everything before the last space is a flag code=$(echo $input | sed 's/.* //') URL="$baseURL&$queryType=$code" _curl } # TODO: refactor to remove repetitive loops _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) 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 for i in {0..4} do # Swap between flightsBefore and flightsAfter if [[ $i > 1 ]] && [[ $swapped != "True" ]] then flights=$flightsAfte swapped="True" count=0 else flights=$flightsBefore fi # Get origin/destination, airline and IATA flight number if [[ $direction == "Origin" ]] then airport=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.iata') else airport=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.iata') fi 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 $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.actual') != "null" ]] then takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.actual') deptTimeType="ATD" elif [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.estimated') != "null" ]] then takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.estimated') deptTimeType="ETD" else takeoff=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].departure.scheduled') deptTimeType="STD" fi if [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.actual') != "null" ]] then landing=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.actual') arrTimeType="ATA" elif [[ $(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.estimated') != "null" ]] then landing=$(echo $flightsBefore | jq -r --arg count $count '.[$count | tonumber].arrival.estimated') arrTimeType="ETA" else 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 output=$(echo $output | sed '$s/|$//') echo $output exit } _flight() { _getdata airline=$(echo $result | jq -r '.data[0].airline.name') origin=$(echo $result | jq -r '.data[0].departure.iata') takeoffSched=$(echo $result | jq -r '.data[0].departure.scheduled') takeoffEst=$(echo $result | jq -r '.data[0].departure.estimated') takeoffActual=$(echo $result | jq -r '.data[0].departure.actual') takeoffDelay=$(echo $result | jq -r '.data[0].departure.delay') destination=$(echo $result | jq -r '.data[0].arrival.iata') landingSched=$(echo $result | jq -r '.data[0].arrival.scheduled') landingEst=$(echo $result | jq -r '.data[0].arrival.estimated') landingActual=$(echo $result | jq -r '.data[0].arrival.actual') landingDelay=$(echo $result | jq -r '.data[0].arrival.delay') # Build output string giving actual or estimated times and delays where available. output="$airline - Origin: $origin " if [[ "$takeoffActual" != "null" ]] then output="$output $(date -d $takeoffActual +"%d/%m/%Y %H:%M")" elif [[ "$takeoffEst" != "null" ]] then output="$output $(date -d $takeoffEst +"%d/%m/%Y %H:%M")" else output="$output $(date -d $takeoffSched +"%d/%m/%Y %H:%M")" fi if [[ "$takeoffDelay" != "null" ]] then output="$output Delay: $takeoffDelay mins" fi output="$output Dest: $destination" if [[ "$landingActual" != "null" ]] then output="$output $(date -d $landingActual +"%d/%m/%Y %H:%M")" elif [[ "$landingEst" != "null" ]] then output="$output $(date -d $landingEst +"%d/%m/%Y %H:%M")" else output="$output $(date -d $landingSched +"%d/%m/%Y %H:%M")" fi if [[ "$landingDelay" != "null" ]] then output="$output Delay: $landingDelay mins" fi echo $output } _help() { echo "Usage: !flight [option] [flight/airport]. Options: -h help, -d departures [IATA airport code], -a arrivals [IATA airport code], -s source. If no option is set then you must provide an IATA flight number e.g. BA2775." } if [[ ${input} == "-h" ]] || [[ ${input} == "--help" ]] || [[ -z $input ]] then _help elif [[ ${input} == "--source" ]] || [[ ${input} == "-s" ]] then echo "https://www.blatech.co.uk/ars/flight-query" exit 0 elif [[ ${input} =~ "-a" ]] || [[ ${input} =~ "--arrivals" ]] then queryType="arr_iata" direction="Origin" _airport elif [[ ${input} =~ "-d" ]] || [[ ${input} =~ "--departures" ]] then direction="Destination" queryType="dep_iata" _airport # if it doesn't match an option then it's probably a specific plane else queryType="flight_iata" _flight fi