This commit is contained in:
chrissy 2025-10-30 19:46:24 +01:00
parent a41125582a
commit dbef782dc0
1 changed files with 10 additions and 5 deletions

View File

@ -9,9 +9,10 @@ from datetime import datetime
import pytz import pytz
timezone = pytz.timezone('Europe/Berlin') timezone = pytz.timezone('Europe/Berlin')
statusData = None statusData = None #initialize variables...
tripData = None tripData = None
# Request the raw data from ICE-Portal
def get_api_db(): def get_api_db():
global statusData global statusData
global tripData global tripData
@ -19,7 +20,8 @@ def get_api_db():
statusData = statusData_response.json() statusData = statusData_response.json()
tripData_response = get("https://iceportal.de/api1/rs/tripInfo/trip", timeout=5) tripData_response = get("https://iceportal.de/api1/rs/tripInfo/trip", timeout=5)
tripData = tripData_response.json() tripData = tripData_response.json()
# Look for the current SSID. Replace wlp2s0 with the real name of your wifi interface and check, that the path to iwlist is correct (just type 'which iwlist' in the terminal)
def get_ssid(): def get_ssid():
scanoutput = check_output(["/usr/sbin/iwlist", "wlp2s0", "scan"]) scanoutput = check_output(["/usr/sbin/iwlist", "wlp2s0", "scan"])
@ -35,6 +37,7 @@ def get_speed_db():
speed= str(statusData["speed"]) speed= str(statusData["speed"])
return speed return speed
# Figure out, whats the next station and filter the output for this.
def get_next_db(): def get_next_db():
next_stop=tripData["trip"]["stopInfo"]["actualNext"] next_stop=tripData["trip"]["stopInfo"]["actualNext"]
for i in tripData['trip']['stops']: for i in tripData['trip']['stops']:
@ -43,10 +46,12 @@ def get_next_db():
arrivalDelay = i['timetable']['arrivalDelay'] arrivalDelay = i['timetable']['arrivalDelay']
utime=i['timetable']['actualArrivalTime']/1000 utime=i['timetable']['actualArrivalTime']/1000
arrivalTime = datetime.fromtimestamp(utime, tz=timezone).strftime('%H:%M') arrivalTime = datetime.fromtimestamp(utime, tz=timezone).strftime('%H:%M')
nextStationTrack = str(i['track']['actual'])
break break
tripinfo = nextStationName+" "+ arrivalTime+" " + arrivalDelay tripinfo = nextStationName+" "+ arrivalTime+" ("+ arrivalDelay+") "+ nextStationTrack
return tripinfo return tripinfo
# Get the Trainnumber and final station of your current train
def get_trainInfo_db(): def get_trainInfo_db():
trainType = tripData["trip"]["trainType"] trainType = tripData["trip"]["trainType"]
trainNumber = tripData["trip"]["vzn"] trainNumber = tripData["trip"]["vzn"]
@ -54,11 +59,11 @@ def get_trainInfo_db():
return trainType+" "+trainNumber+" "+ finalStationName return trainType+" "+trainNumber+" "+ finalStationName
def main(): def main():
# Look for WIFIonICE otherwise do nothing...
if get_ssid() == "WIFIonICE": if get_ssid() == "WIFIonICE":
get_api_db() get_api_db()
print(get_trainInfo_db()+" | "+"SPEED: "+get_speed_db()+" km/h | "+"NEXT: "+get_next_db()) print(get_trainInfo_db()+" | "+"SPEED: "+get_speed_db()+" km/h | "+"NEXT: "+get_next_db())
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) # next section explains the use of sys.exit sys.exit(main())