diff options
author | Asa Venton <asav1410@gmail.com> | 2019-10-20 12:27:30 +0100 |
---|---|---|
committer | Asa Venton <asav1410@gmail.com> | 2019-10-20 12:27:30 +0100 |
commit | b5361dca1b65b3786e84fec56885b28f7e714b5b (patch) | |
tree | 31ec9dd0485ac0613c184f57c2b8daa131349eb4 /livetimes.sh | |
parent | ca408ede4ac14fc2631e0ff4de60ded3c737aa45 (diff) |
Add support for multiple matching stops, comments and TODOs
Diffstat (limited to 'livetimes.sh')
-rwxr-xr-x | livetimes.sh | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/livetimes.sh b/livetimes.sh index d44b6d8..4f33afe 100755 --- a/livetimes.sh +++ b/livetimes.sh @@ -1,17 +1,19 @@ #!/bin/bash - +# TODO: Reject $stop similar to "Bus Station" and suggest user tries 'nextbus' script instead - pending creation of nextbus script read stop +# function to provide data to a for loop _jq() { echo ${json} | base64 --decode | jq -r ${1} } _liveresult() { livetimes=$(curl --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop}) + # doesn't work without encoding in base64 (why?) for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do - result="$result$(_jq '.destination') " - result="$result $(_jq '.eta') " - result="$result $(_jq '.service_number')\n" + result="$result Dest: $(_jq '.destination') |" + result="$result ETA: $(_jq '.eta') |" + result="$result Route: $(_jq '.service_number')\n" done echo -e $result # is this the correct exit code? @@ -21,11 +23,17 @@ _liveresult() { _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 + # multiple stops returned in $stops so we will return stop names and codes so user can retry else - echo "requests returning multiple bus stops not yet supported" + for json in $(echo "${stops}" | jq -r '.[] | @base64'); do + result="$result Stop: $(_jq '.name') |" + result="$result Code: $(_jq '.stop_id')\n" + done + echo -e $result # is this the correct exit code? exit 0 fi @@ -33,6 +41,8 @@ _stopsreturn() { 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." + # is this the correct exit code? + exit 0 elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then _liveresult else |