diff options
Diffstat (limited to 'livetimes.sh')
-rwxr-xr-x | livetimes.sh | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/livetimes.sh b/livetimes.sh new file mode 100755 index 0000000..d44b6d8 --- /dev/null +++ b/livetimes.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +read stop + +_jq() { + echo ${json} | base64 --decode | jq -r ${1} +} + +_liveresult() { + livetimes=$(curl --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop}) + for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do + result="$result$(_jq '.destination') " + result="$result $(_jq '.eta') " + result="$result $(_jq '.service_number')\n" + done + echo -e $result + # is this the correct exit code? + exit 0 +} + +_stopsreturn() { + stops=$(curl --silent https://admin.libertybus.je/api/v1/stops/"${stop}" | jq -r '.') + stopslen=$(echo ${stops} | jq '. | length') + if [ $stopslen = 1 ]; then + stop=$(echo ${stops} | jq '.[].stop_id') + _liveresult + else + echo "requests returning multiple bus stops not yet supported" + # is this the correct exit code? + exit 0 + fi +} + +if [ "$stop" == "-h" ] || [ "$stop" == "--help" ]; then + echo "Usage: '!livetimes <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." +elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then + _liveresult +else + _stopsreturn +fi |