diff options
author | Asa Venton <asav1410@gmail.com> | 2019-10-23 18:57:10 +0100 |
---|---|---|
committer | Asa Venton <asav1410@gmail.com> | 2019-10-23 18:57:10 +0100 |
commit | a88accaf91c45e4b0e2f0fc43abc452b1cc974c4 (patch) | |
tree | 7f2fdc2c53bf9e212660fc21e7043d388e06409b | |
parent | fbca29a82c573579be0518d8c66d6bc6e968c3cf (diff) |
Add ability to specify time when requesting next bus, convert short day to long day when nextbus outputs a bus on a day in the future
-rwxr-xr-x | livetimes.sh | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/livetimes.sh b/livetimes.sh index 624b2b3..34a7fd7 100755 --- a/livetimes.sh +++ b/livetimes.sh @@ -72,12 +72,14 @@ _rejectbusstation() { # 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." + echo ""$error"Usage: 'Usage: '!bus <input> (<time>)' where <input> is: a valid route number, this returns the next bus leaving the station, if <time> is also specified then the next bus after that time is returned; a full or partial bus stop name, if only one result is returned then live times for that stop are returned, if multiple results are returned then a list of matching stops are returned; a valid bus stop code, this returns live tops for that stop; -h --help, returns this help info." exit 0 } _nextbus() { - time=$(date +"%H:%M") + if [ -z ${time} ]; then + time=$(date +"%H:%M") + fi day=$(date +"%a" | tr [:upper:] [:lower:]) # set routeday to one of three timetable options if [[ $(date +%u) -lt 6 ]]; then @@ -108,7 +110,7 @@ _nextbus() { 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') + echo $(echo "Route $stop leaving from $(echo $nextbus | jq -s '.[0].stop_name') at $(echo $nextbus | jq -s '.[0].departure_time') on $(date -d $day +%A)" | sed 's/"//g') exit 0 fi done @@ -127,6 +129,21 @@ elif [[ "$stop" =~ ^-?[0-9]+$ ]] && [ $(echo ${#stop} == 4) ]; then # checks if input is a valid bus route elif [[ ${services[@]} =~ ${stop^^} ]]; then _nextbus +# if input has one space and a valid time then the user is probably trying to search for the next bus leaving the station for a given route and time +elif [[ $(echo $stop | tr -cd ' \t' | wc -c) == 1 ]] && [[ "$stop" =~ [0-9][0-9]:[0-9][0-9] ]]; then + time=$(echo $stop | cut -d ' ' -f2) + stop=$(echo $stop | cut -d ' ' -f1) + if [[ ! "$time" =~ ^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$ ]]; then + error="Invalid time, specify time with HH:MM '!bus <route> <time>' - " + _help + elif [[ ! ${services[@]} =~ ${stop^^} ]]; then + echo $stop + echo $services + error="Invalid route provided - " + _help + else + _nextbus + fi elif [[ ${stop,,} == *"bus"* ]] || [[ ${stop,,} == *"station"* ]] || [[ ${stop,,} == *"stand"* ]]; then _rejectbusstation else |