diff --git a/bvg.sh b/bvg.sh new file mode 100755 index 0000000..ccecb05 --- /dev/null +++ b/bvg.sh @@ -0,0 +1,77 @@ +#!/bin/sh + +API="https://1.bvg.transport.rest" + +JQ_PRELUDE=' + def ansicolor(code): "\u001b[\(code)m\(.)\u001b[0m"; + + def prettytime: "\(.[0:19] | strptime("%FT%T") | mktime | strftime("%R"))" | ansicolor(90); + + def prettyline: "\u001b[\( + if .product == "subway" then 34 # blue + elif .product == "bus" then 35 # magenta + elif .product == "tram" then 31 # red + elif .product == "suburban" then 32 # green + elif .product == "regional" then 37 # white + else 37 # white + end + )m\(.name)\u001b[0m"; +' + +find_id() { + query="$1" + curl -sG "${API}/locations" -d results=1 -d fuzzy=false -d poi=false -d addresses=false -d query="$query" \ + | jq -r ".[0].id" +} + +station() { + query="$1" + curl -sG "${API}/locations" -d fuzzy=false -d poi=false -d addresses=false -d query="$query" \ + | jq -r '.[] | "\u001b[33m\(.id)\u001b[0m \(.name)"' +} + +departures() { + curl -sG "${API}/stations/$1/departures" -d remarks=false \ + | jq -r "$JQ_PRELUDE"' + .[] + | "\(.when | prettytime) \(.line | prettyline)\t\(.direction)" + ' +} + +journeys() { + origin_id="$1" + destination_id="$2" + curl -sG "${API}/journeys" -d results=1 -d from="$origin_id" -d to="$destination_id" \ + | jq -r "$JQ_PRELUDE"' + .[0] + | .legs + | .[] + | "\(.origin.name) \(.departure | prettytime) \(.line | prettyline) \(.arrival | prettytime) \(.destination.name)" + ' +} + +case $1 in + station) + shift + station "$@" + ;; + departures:id) + shift + departures "$@" + ;; + journeys:id) + shift + departures "$@" + ;; + journeys) + shift + journeys "$(find_id "$1")" "$(find_id "$2")" + ;; + departures) + shift + departures "$(find_id "$1")" + ;; + --help) + echo "Usage: $0 [station QUERY | journeys QUERY QUERY | departures QUERY]" + ;; +esac