#!/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 } _arrivals() { _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') 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" 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') 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" done } _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') 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') landingDelay=$(echo $result | jq -r '.data[0].arrival.delay') # Build output string giving estimated times and delays where available. output="$airline - Origin: $origin " if [[ "$takeoffEst" != "null" ]] then output="$output $takeoffEst" else output="$output $takeoffSched" fi if [[ "$takeoffDelay" != "null" ]] then output="$output Delay: $takeoffDelay" fi output="$output Dest: $destination" if [[ "$landingEst" != "null" ]] then output="$output $landingEst" else output="$output $landingSched" fi if [[ "$landingDelay" != "null" ]] then output="$output Delay: $landingDelay" 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" _arrivals elif [[ ${input} =~ "-d" ]] || [[ ${input} =~ "--departures" ]] then queryType="dep_iata" _departures # if it doesn't match an option then it's probably a specific plane else queryType="flight_iata" _flight fi