summaryrefslogtreecommitdiff
path: root/flightquery.sh
blob: 16910e6bda257bca607601d98f9ae9d4b3eeb776 (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
#!/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