Created
November 12, 2022 15:16
-
-
Save Fluepke/7d6f01549cabfdbf74ccc3b27b5559eb to your computer and use it in GitHub Desktop.
Log train rides on german trains (location data, internet availability, speed, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if ! command -v curl &> /dev/null; then | |
echo "This script needs curl. Please install curl." | |
exit -1 | |
fi | |
if ! command -v jq &> /dev/null; then | |
echo "This script needs jq. Please install jq." | |
exit -1 | |
fi | |
echo "Fetching train ride metadata …" | |
echo "Make sure, you are connected to the train's WLAN" | |
TRAIN_SERIES='{"412":{"name":"ICE 4","type":"train","v_max":250},"403":{"name":"ICE 3","type":"train","v_max":300},"406":{"name":"ICE 3","type":"train","v_max":300},"407":{"name":"ICE 3","type":"train","v_max":300},"411":{"name":"ICE T","type":"train","v_max":230},"415":{"name":"ICE T","type":"train","v_max":230},"402":{"name":"ICE 2","type":"train","v_max":280},"401":{"name":"ICE T","type":"train","v_max":230},"801":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"First Class Intermediate Car"},"801.8":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"First Class Intermediate Car with Special Equipment"},"802":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"Second Class Intermediate Car"},"802.9":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"Second Class Intermediate Car with Special Configuration"},"803":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"Service Car"},"804":{"name":"ICE 1","v_max":280,"type":"wagon","wagon_type":"Restaurant Car"}}' | |
while : | |
do | |
TRIP=$(curl -s 'https://iceportal.de/api1/rs/tripInfo/trip' \ | |
-H 'Accept: application/json, text/plain, */*' \ | |
-H 'Accept-Language: en-US,en;q=0.9' \ | |
-H 'Cache-Control: no-cache' \ | |
-H 'Connection: keep-alive' \ | |
-H 'Content-Type: application/json' \ | |
-H 'Pragma: no-cache' \ | |
-H 'Referer: https://iceportal.de/Karte_neu' \ | |
--compressed) | |
STATUS=$(curl -s 'https://iceportal.de/api1/rs/status' \ | |
-H 'Accept: application/json, text/plain, */*' \ | |
-H 'Accept-Language: en-US,en;q=0.9' \ | |
-H 'Cache-Control: no-cache' \ | |
-H 'Connection: keep-alive' \ | |
-H 'Content-Type: application/json' \ | |
-H 'Pragma: no-cache' \ | |
-H 'Referer: https://iceportal.de/Karte_neu' \ | |
--compressed) | |
trainType=$(echo $TRIP | jq -r '.trip.trainType') | |
trainNumber=$(echo $TRIP | jq -r '.trip.vzn') | |
carSeries=$(echo $STATUS | jq -r '.series') | |
tripDate=$(echo $TRIP | jq -r '.trip.tripDate') | |
totalDistance=$(echo $TRIP | jq -r '.trip.totalDistance') | |
totalDistanceKm=$(expr $totalDistance / 1000) | |
distanceFromLastStop=$(echo $TRIP | jq -r '.trip.distanceFromLastStop') | |
speed=$(echo $STATUS | jq -r '.speed') | |
gpsStatus=$(echo $STATUS | jq -r '.gpsStatus') | |
vMax=$(echo $TRAIN_SERIES | jq -r ".\"${carSeries}\".\"v_max\"") | |
vMaxThreshold=$(expr $vMax \* 85 / 100) | |
tput clear | |
speedColor="" | |
if [ "$speed" -gt "$vMaxThreshold" ]; then | |
speedColor="\033[5;31m" | |
fi | |
mkdir -p train_rides/$tripDate | |
echo "Logging status data to ./train_rides/$tripDate/${trainType}_${trainNumber}_status.json" | |
echo "Logging trip data to ./train_rides/$tripDate/${trainType}_${trainNumber}_trip.json" | |
echo -e "\033[1mTrain Type\033[0m: $trainType" | |
echo -e "\033[1mTrain Number\033[0m: $trainNumber" | |
echo -e "\033[1mCar Series\033[0m: BR $trainSeries" | |
echo -e "\033[1mDate\033[0m: $tripDate" | |
echo -e "\033[1mTotal Distance\033[0m: $totalDistanceKm km" | |
if [[ $gpsStatus == "VALID" ]]; then | |
echo -e "\033[1mGPS Status\033[0m: \033[0;32m$gpsStatus\033[0m" | |
echo -e "\033[1mSince last stop\033[0m: $(echo $distanceFromLastStop | sed -e 's/...$/.&/;t' -e 's/.$/.0&/') km" | |
echo -e "\033[1mSpeed\033[0m: ${speedColor}${speed} km/h\033[0m (max $vMax km/h)" | |
else | |
echo -e "\033[1mGPS Status\033[0m: \033[0;31m$gpsStatus\033[0m" | |
echo -e "\033[1mSince last stop\033[0m: \e[3mNo GPS\e[0m" | |
echo -e "\033[1mSpeed\033[0m: \e[3mNo GPS\e[0m" | |
fi | |
echo $STATUS >> "train_rides/$tripDate/${trainType}_${trainNumber}_status.json" | |
echo $TRIP >> "train_rides/$tripDate/${trainType}_${trainNumber}_trip.json" | |
sleep 3 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment