summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsa Venton <asav1410@gmail.com>2019-10-23 00:19:27 +0100
committerAsa Venton <asav1410@gmail.com>2019-10-23 00:19:27 +0100
commitfbca29a82c573579be0518d8c66d6bc6e968c3cf (patch)
tree2097ef6e5e36fac7b516ac76aea7c88784e94fe9
parent933513d03daf5b3f040266f9a2f83a51bdf338c8 (diff)
Fix not switching to correct timetable based on day if there are no more buses today
-rwxr-xr-xlivetimes.sh27
1 files 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)