summaryrefslogtreecommitdiff
path: root/livetimes.sh
blob: 9c9d7bdeb52bc344e70b9d0444900c66bc4d6bee (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
#!/bin/bash
# TODO: If $stop similar to "Bus Station" then reject and suggest user tries supplying a route number to see when their next bus leaves the station
# TODO: Fix duplication of destinations and routes in result
read stop

# function to provide data to a for loop
_jq() {
  echo ${json} | base64 --decode | jq -r ${1}
}

# TODO: efficiency of _liveresult?
_liveresult() {
  livetimes=$(curl --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop})
  result="$result Dest: "
  for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do
    result="$result $(_jq '.destination'), "
  done
  result=$(echo $result | sed 's/,$//')
  result="$result | Routes:"
  for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do
    result="$result $(_jq '.service_number'), "
  done
  result=$(echo $result | sed 's/,$//')
  result="$result | ETAs: "
  for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do
    result="$result $(_jq '.eta'), "
  result=$(echo $result | sed 's/,$//')
  done
  echo -e $result
  exit 0
}

_stopsreturn() {
  stops=$(curl --silent https://admin.libertybus.je/api/v1/stops/"${stop}" | jq -r '.')
  stopslen=$(echo ${stops} | jq '. | length')
  # if length of $stops array is 1 then only one bus stop matches and we can return live tiems for that stop
  if [ $stopslen = 1 ]; then
    stop=$(echo ${stops} | jq '.[].stop_id')
    _liveresult
  elif [ $stopslen = 0 ]; then
    echo "No results found."
    exit 1
  # multiple stops returned in $stops so we will return stop names and codes so user can retry
  else
    for json in $(echo "${stops}" | jq -r '.[] | @base64'); do
      result="$result Stop: $(_jq '.name') -"
      result="$result Code: $(_jq '.stop_id') |"
    done
    result=$(echo $result | sed 's/ |$//')
    echo -e $result
    exit 0
  fi
}

if [ "$stop" == "-h" ] || [ "$stop" == "--help" ]; then
  echo "Usage: '!bus <stop>' where <stop> is either the 4 digit stop code, the full unique or partial bus stop name. If a bus stop name is used and there are multiple matches then a list of matching stops will be returned with their codes."
  exit 0
elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then
  _liveresult
elif [[ -z "${stop// }" ]]; then
  echo "Input empty. Usage: '!bus <stop>' where <stop> is either the 4 digit stop code, the full unique or partial bus stop name. If a bus stop name is used and there are multiple matches then a list of matching stops will be returned with their codes."
  exit 1
else
  _stopsreturn
fi