From fbca29a82c573579be0518d8c66d6bc6e968c3cf Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Wed, 23 Oct 2019 00:19:27 +0100 Subject: Fix not switching to correct timetable based on day if there are no more buses today --- livetimes.sh | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/livetimes.sh b/livetimes.sh index 384466f..624b2b3 100755 --- a/livetimes.sh +++ b/livetimes.sh @@ -79,7 +79,7 @@ _help() { _nextbus() { time=$(date +"%H:%M") day=$(date +"%a" | tr [:upper:] [:lower:]) - # set day to one of three timetable options + # set routeday to one of three timetable options if [[ $(date +%u) -lt 6 ]]; then routeday=mon_fri else @@ -93,9 +93,28 @@ _nextbus() { 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 - this is probably wrong if the day is Friday, Saturday or Sunday as timetables switch on those days. - echo $(echo "Route $stop leaving from $(echo $nextbus | jq -s '.[0].stop_name') at $(echo $nextbus | jq -s '.[0].departure_time') tomorrow" | sed 's/"//g') - exit 0 + # if program didn't exit inside for loop then we know the next bus is on a future day so we need to check future days, unfortunately plenty of routes have days where no bus runs so we need to loop until we find the next day with a running bus + # for loop in range 7 used to prevent possible infinite loop with other methods - if there's not a bus for the next week then something is probably broken + for in in {1..7}; do + day=$(date --date="$i day" +"%a" | tr [:upper:] [:lower:]) + case $day in + mon|tue|wed|thu|fri) + routeday=mon_fri;; + sat) + routeday=sat;; + sun) + routeday=sun;; + *) // + esac + nextbus=$(echo $timetables | jq -r --arg routeday "$routeday" --arg day "$day" --arg stop "$stop" '.timetables[] | select(.service_number == $stop) | .timetable.outbound[$routeday].trips[] | select(.days_of_operation[$day] == true) | .stops[] | select(.stop_name | contains("Bus Station Stand"))') + if [[ ! -z "${nextbus// }" ]]; then + echo $(echo "Route $stop leaving from $(echo $nextbus | jq -s '.[0].stop_name') at $(echo $nextbus | jq -s '.[0].departure_time') on $day" | sed 's/"//g') + exit 0 + fi + done + # if program didn't exit inside for loop then something is probably broken + echo "Something broken - hopefully no one ever sees this..." + exit 1 } timetables=$(curl --fail --silent https://admin.libertybus.je/cache/timetables/timetable_full.json) -- cgit v1.2.3