#!/bin/bash # TODO: add failure check for each cURL read stop # function to provide data to a for loop _jq() { echo ${json} | base64 --decode | jq -r ${1} } _liveresult() { stops=$(curl --fail --silent https://admin.libertybus.je/api/v1/stops/"${stop}") stopname=$(echo $stops | jq '.[].name') livetimes=$(curl --fail --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop}) exitcode=$? # sort livetimes by service_number and eta livetimes=$(echo ${livetimes} | jq 'sort_by((.service_number | sub("[^0-9]$"; "") | tonumber), (.service_number | sub("[^A-Z]$"; "") | tostring), .eta)') livetimeslen=$(echo ${livetimes} | jq '. | length') if [ $exitcode != 0 ]; then echo "cURL error - exiting." exit 1 elif [ $livetimeslen = 0 ]; then echo "No results found." exit 1 elif [[ ${stopname,,} == *"bus"* ]] || [[ ${stopname,,} == *"station"* ]] || [[ ${stopname,,} == *"stand"* ]]; then _rejectbusstation else for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do if [[ $(_jq '.service_number') != $outroute ]]; then result=$(echo $result | sed 's/,$//') result="$result | Dest: $(_jq '.destination')[$(_jq '.service_number')] - ETAs: $(_jq '.eta'), " else result="$result $(_jq '.eta'), " fi outroute=$(_jq '.service_number') done result=$(echo $result | sed 's/,$//' | sed 's/|//' ) echo -e $result exit 0 fi } _stopsreturn() { stops=$(curl --fail --silent https://admin.libertybus.je/api/v1/stops/"${stop}") exitcode=$? stopslen=$(echo ${stops} | jq '. | length') # if length of $stops array is 1 then only one bus stop matches and we can return live times for that stop if [ $exitcode != 0 ]; then echo "cURL error - exiting." exit 1 elif [ $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') - Code: $(_jq '.stop_id') |" #result="$result Code: $(_jq '.stop_id') |" done result=$(echo $result | sed 's/ |$//') echo -e $result exit 0 fi } _rejectbusstation() { echo "If you want to see when the next bus for a given route leaves the bus station then try ""!bus "" where is a valid bus route or try ""!bus -h"" for help." exit 1 } # function for now in case I need to reuse somewhere else... _help() { echo ""$error"Usage: 'Usage: '!bus (