1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#!/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.iata')
else
airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.iata')
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')
deptTimeType="ATD"
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')
deptTimeType="ETD"
else
takeoff=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.scheduled')
deptTimeType="STD"
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')
arrTimeType="ATA"
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')
arrTimeType="ETA"
else
landing=$(echo $flightsBefore | jq -r --arg i $i '.[$i | 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") |"
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
if [[ $direction == "Origin" ]]
then
airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].departure.iata')
else
airport=$(echo $flightsBefore | jq -r --arg i $i '.[$i | tonumber].arrival.iata')
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')
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')
deptTimeType="ATD"
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')
deptTimeType="ETD"
else
takeoff=$(echo $flightsAfter | jq -r --arg i $i '.[$i | tonumber].departure.scheduled')
deptTimeType="STD"
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')
arrTimeType="ATA"
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')
arrTimeType="ETA"
else
landing=$(echo $flightsAfter | jq -r --arg i $i '.[$i | 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") |"
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
|