55 lines
1.6 KiB
Bash
Executable File
55 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
if [[ $# -ne 1 ]]; then
|
|
echo "Usage: $0 <dvb_tram_number>"
|
|
exit
|
|
fi
|
|
|
|
|
|
#if [[ $1 -lt 1 || $1 -gt 13 || $1 -eq 5 ]]; then
|
|
# >&2 echo "$1 is not a valid Tram in Dresden"
|
|
# exit 1
|
|
#fi
|
|
|
|
if [[ ! -x "$(command -v jq)" ]]; then
|
|
>&2 echo "You need to have 'jq' installed. You can download it here: https://jqlang.org/download/"
|
|
exit 1
|
|
fi
|
|
line_num="$1"
|
|
|
|
current_date=$(date -Iseconds)
|
|
window=$(date -Iseconds -d "+5 minutes")
|
|
|
|
api="https://api.transitous.org/api"
|
|
|
|
response=$(curl --get \
|
|
--data-urlencode "startTime=$current_date" \
|
|
--data-urlencode "endTime=$current_date" \
|
|
--data-urlencode "min=51.1626,13.5609" \
|
|
--data-urlencode "max=50.9973,13.8596" \
|
|
--data-urlencode "zoom=13" \
|
|
--data-urlencode "precision=2" \
|
|
"$api/v1/map/trips")
|
|
|
|
query='[.[] | select(.trips[0].routeShortName == "'$line_num'")]'
|
|
|
|
all_trams=$(jq -c "$query" <<< "$response")
|
|
|
|
# echo "$all_trams"
|
|
|
|
tripId=$(jq -r '.[0].trips[0].tripId' <<< "$all_trams")
|
|
response=$(curl --get \
|
|
--data-urlencode "tripId=$tripId" \
|
|
"$api/v6/trip")
|
|
|
|
trip=$(jq -c '[.legs[0].from] + [.legs[0].intermediateStops[]] + [.legs[0].to] | map({stopId: .stopId, name: .name})' <<< $response)
|
|
|
|
# jq -c <<< $trip
|
|
|
|
currentStops=$(jq -c 'map(.from.stopId[:-2])' <<< "$all_trams")
|
|
|
|
parsed=$(jq --argjson stops "${currentStops}" 'map(. as $t | (if $stops | index($t.stopId[:-2]) then $t.tram = true else $t.tram = false end))' <<< "$trip")
|
|
green=$'\e[38;5;11m'
|
|
end=$'\e[0m'
|
|
jq -r --arg green "$green" --arg end "$end" 'map(if .tram then $green + " " + .name + $end else " " + .name end) | .[]'<<< "$parsed"
|