diff options
Diffstat (limited to 'flightquery.sh')
-rwxr-xr-x | flightquery.sh | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/flightquery.sh b/flightquery.sh new file mode 100755 index 0000000..16910e6 --- /dev/null +++ b/flightquery.sh @@ -0,0 +1,111 @@ +#!/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 airport IATA or flight ICAO), assuming everything before the last space is a flag + code=$(echo $input | sed 's/.* //') + URL="$baseURL&$queryType=$code" + _curl + #echo $result | python -m json.tool +} + +_arrivals() { + _getdata +} + +_departures() { + _getdata +} + +_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') + + takeoffEst="null" + # Build output strings 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]. Options: -h help, -d departures, -a arrivals, -s source." +} + +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 |