diff options
-rwxr-xr-x | livetimes.sh | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/livetimes.sh b/livetimes.sh index 39ebc07..d4bd050 100755 --- a/livetimes.sh +++ b/livetimes.sh @@ -61,14 +61,41 @@ _stopsreturn() { 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." +# function for now in case I need to reuse somewhere else... +_help() { + echo "Usage: '!bus <stop>' where <stop> 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"))') + echo $nextbus > foo.json + 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 +} + +#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 [[ -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 +elif [[ ${services[@]} =~ ${stop^^} ]]; then + _nextbus else - _stopsreturn + _stopsreturn fi |