nix-config/home/wm/waybar/src/ice_fetch.sh

55 lines
2.2 KiB
Bash
Executable file

# Fetch the JSON data from the APIs
status_data=$(curl -s https://iceportal.de/api1/rs/status)
trip_data=$(curl -s https://iceportal.de/api1/rs/tripInfo/trip)
# Extract the speed value
speed=$(echo "$status_data" | jq -r '.speed')
# Check if the speed value is empty
if [ -z "$speed" ]; then
echo "{\"text\": \"N/A\", \"alt\": \"Speed data not available\", \"tooltip\": \"\", \"class\": \"unavailable\", \"percentage\": 0}"
exit 1
fi
# Extract additional information for the tooltip
train_type=$(echo "$trip_data" | jq -r '.trip.trainType')
line_number=$(echo "$trip_data" | jq -r '.trip.vzn')
train_number=$(echo "$status_data" | jq -r '.tzn | gsub("ICE"; "Tz")')
train_series=$(echo "$status_data" | jq -r '.series')
# Get next stop information
next_stop_eva=$(echo "$trip_data" | jq -r '.trip.stopInfo.actualNext')
next_stop_info=$(echo "$trip_data" | jq -r --arg eva "$next_stop_eva" '.trip.stops[] | select(.station.evaNr == $eva)')
next_stop_name=$(echo "$next_stop_info" | jq -r '.station.name')
next_stop_arrival=$(echo "$next_stop_info" | jq -r '.timetable.actualArrivalTime')
next_stop_arrival_delay=$(echo "$next_stop_info" | jq -r '.timetable.arrivalDelay')
next_stop_distance=$(echo "$next_stop_info" | jq -r '.info.distanceFromStart')
current_position=$(echo "$trip_data" | jq -r '.trip.actualPosition')
# Calculate distance to next stop
distance_to_next=$((next_stop_distance - current_position))
distance_km=$(echo "scale=2; $distance_to_next/1000" | bc)
# Convert timestamps to human-readable format
if [ "$next_stop_arrival" != "null" ]; then
next_stop_arrival_time=$(date -d @$((next_stop_arrival/1000)) +"%H:%M")
else
next_stop_arrival_time="N/A"
fi
# Create the tooltip text
tooltip="${train_type} ${line_number} (${train_number} - Br${train_series})\n\n"
tooltip+="Next stop: ${next_stop_name}\n"
tooltip+="${next_stop_arrival_time} (${next_stop_arrival_delay}) - ${distance_km} km"
# Create the JSON object
text="${speed} km/h"
alt="Current speed"
class="icestats"
percentage=0
json_output="{\"text\": \"${text}\", \"alt\": \"${alt}\", \"tooltip\": \"${tooltip}\", \"class\": \"${class}\", \"percentage\": ${percentage}}"
# Print the JSON object
echo "$json_output"