From f1902d6653ced4e841c13113a8a558ad248e121d Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Thu, 29 Oct 2020 19:55:11 +0000 Subject: Initial commit. All functionality to query information for a given flight. --- flightquery.conf.example | 4 ++ flightquery.sh | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 flightquery.conf.example create mode 100755 flightquery.sh diff --git a/flightquery.conf.example b/flightquery.conf.example new file mode 100644 index 0000000..67ffd7f --- /dev/null +++ b/flightquery.conf.example @@ -0,0 +1,4 @@ +# Example config + +# Enter your aviation stack API key there +appKey= 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 -- cgit v1.2.3