#!/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 read stop # function to provide data to a for loop _jq() { echo ${json} | base64 --decode | jq -r ${1} } _liveresult() { 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 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 } # function for now in case I need to reuse somewhere else... _help() { echo "Usage: '!bus ' where is either the route number, 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. -h --help - print this usage information." exit 0 } _nextbus() { time=$(date +"%H:%M") # set day to one of three timetable options if [[ $(date +%u) -lt 6 ]]; then day=mon_fri else day=$(date +"%a") fi # select all stops from todays trips for a given service_number where stop_name contains "Bus Station Stand" nextbus=$(echo $timetables | jq -r --arg day "$day" --arg stop "$stop" '.timetables[] | select(.service_number == $stop) | .timetable.outbound[$day].trips[].stops[] | select(.stop_name | contains("Bus Station Stand"))') for json in $(echo "${nextbus}" | jq -r '. | @base64'); do if [[ $(_jq '.departure_time') > $time ]]; then echo "Route $stop leaving from $(_jq '.stop_name') at $(_jq '.departure_time')" exit 0 fi done # if program didn't exit inside for loop then we know the next bus is actually tomorrow in which case we'll return the zeroth item of the array echo $(echo "Route $stop leaving from $(echo $nextbus | jq -s '.[0].stop_name') at $(echo $nextbus | jq -s '.[0].departure_time')" | sed 's/"//g') exit 0 } #stop=$(echo ${stop^^}) timetables=$(curl --fail --silent https://admin.libertybus.je/cache/timetables/timetable_full.json) services=$(echo $timetables | jq '.timetables[].service_number') #route=$(echo $timetables | jq -r --arg stop "$stop" '.timetables[] | select(.service_number == $stop) | .service_number') if [ "$stop" == "-h" ] || [ "$stop" == "--help" ] || [[ -z "${stop// }" ]]; then _help elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then _liveresult elif [[ ${services[@]} =~ ${stop^^} ]]; then _nextbus else _stopsreturn fi