Re-Wrote CSV file parsing functions
authorCian Bagshaw <cian@cianb.xyz>
Wed, 17 May 2023 04:50:50 +0000 (05:50 +0100)
committerCian Bagshaw <cian@cianb.xyz>
Wed, 17 May 2023 04:50:50 +0000 (05:50 +0100)
- stops.csv strings now contained with quotes
- general csv parsing function added
- better stop info output

buseir

diff --git a/buseir b/buseir
index fa441b5..972230c 100755 (executable)
--- a/buseir
+++ b/buseir
@@ -1,9 +1,10 @@
 #!/bin/sh
+# Download Bus Eireann data to CSV file cache, and provide useful functions to
+# view data
 
 SITE=https://buseireann.ie
 CACHE=$XDG_CACHE_HOME/buseir
 TMP=/tmp/buseir
-MAP=http://www.google.com/maps/place
 MENU="fzf --reverse --height=12"
 
 sync() {
@@ -19,24 +20,32 @@ sync() {
                do echo "$SITE/inc/proto/fareDestinations.php?fare=$i"; done \
                | wget -q -i - -O - | jq -r '[.fareDestinations[]|.name]|@csv' \
                | paste -d, $TMP/routes.csv - > $CACHE/routes.csv
-       grep -o "{.*}" $TMP/bus_stop_points.php | jq -r '.bus_stops[][]' \
-               | paste -d, - - - - - > $CACHE/stops.csv
+       grep -o "{.*}" $TMP/bus_stop_points.php \
+               | jq -r '.bus_stops[]|[.name,.num,.duid,.lat,.lng]|@csv' > $CACHE/stops.csv
 }
 
-stopInfo() {
-       [ "$1" == "search" ] && STOP=$(stopInfo | $MENU) || STOP=$1
-       [ -z "$2" ] && COL=2 \
-               || case $2 in
-                       duid) COL=1;; name) COL=2;; lat) COL=3;; long) COL=4;;
-                       num) COL=5;; pos) COL=3-4;; all) COL=1-5;;
-                       map) xdg-open $MAP/$(stopInfo "$STOP" "pos"); exit;;
-               esac 
-       grep "$STOP" $CACHE/stops.csv | cut -d, -f$COL
+csv() {
+       if [ "$2" == "all" ];
+               then cat "$1"; else grep "$2" "$1"
+       fi | cut -d, -f"$3" | tr -d \"
+}
+
+stops() {
+       case $2 in
+               name) COL=1;;   num) COL=2;;    duid) COL=3;;
+               pos) COL=4-5;;  all) COL=1-;;   *) COL=1;;
+       esac 
+       csv $CACHE/stops.csv "$1" "$COL" 
+}
+
+printStop() {
+       tr "," "\n" | sed -e '1s/^/Name:\t/; 2s/^/Number:\t/; 3s/^/DUID:\t/' \
+               -e '4{N; s/\n/,/; s/^/Map:\thttps:\/\/www.google.com\/maps\/place\//}'  
 }
 
 [ ! -d $CACHE ] && mkdir -p "$CACHE" && sync
 
 case $1 in
        sync) sync;;
-       stop) stopInfo $2 $3;;
+       stops) stops "\"$(stops all | $MENU)"\" all | printStop;;
 esac