summaryrefslogtreecommitdiff
path: root/flightquery.sh
blob: 2382fa62663c4b7ff40fb6339f4e953628544b4a (plain)
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
#!/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
  result=$(echo $result | jq '.data | sort_by(.arrival.scheduled)')
  # TODO: Sort by something (arrival time?), 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
  result=$(echo $result | jq '.data | sort_by(.departure.scheduled)')
  # TODO: Sort by something (departure time?), 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 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/airport]. Options: -h help, -d departures [airport], -a arrivals [airport], -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