#!/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) 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 # 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 # 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 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() { _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 # TODO add real source 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