summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsa Venton <asav1410@gmail.com>2019-10-22 21:02:14 +0100
committerAsa Venton <asav1410@gmail.com>2019-10-22 21:02:14 +0100
commit2ed57548a38a92d702fc05b2b3d8ed941c42412b (patch)
tree4d3bd5be12296db6fb27f60f3e28241c5c1c3edd
parentd637ce2f84247f08e9fe1c193581fad07061979d (diff)
Reject input requesting live times for Bus Station and suggest they try next bus functionality instead
-rwxr-xr-xlivetimes.sh19
1 files changed, 15 insertions, 4 deletions
diff --git a/livetimes.sh b/livetimes.sh
index 209bec8..da03c98 100755
--- a/livetimes.sh
+++ b/livetimes.sh
@@ -1,5 +1,5 @@
#!/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
@@ -8,6 +8,8 @@ _jq() {
}
_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
@@ -19,6 +21,8 @@ _liveresult() {
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
@@ -61,6 +65,11 @@ _stopsreturn() {
fi
}
+_rejectbusstation() {
+ echo "If you want to see when the next bus for a given route leaves the bus station then try ""!bus <route>"" where <route> 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 "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."
@@ -88,16 +97,18 @@ _nextbus() {
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
+# checks if input pattern matches a 4 digit bus stop code
elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then
_liveresult
+# checks if input is a valid bus route
elif [[ ${services[@]} =~ ${stop^^} ]]; then
- _nextbus
+ _nextbus
+elif [[ ${stop,,} == *"bus"* ]] || [[ ${stop,,} == *"station"* ]] || [[ ${stop,,} == *"stand"* ]]; then
+ _rejectbusstation
else
_stopsreturn
fi