built in travelynx request

This commit is contained in:
chrissy 2025-11-08 01:06:05 +01:00
parent dbef782dc0
commit c1279ee2ad
2 changed files with 84 additions and 27 deletions

1
.gitignore vendored
View File

@ -160,3 +160,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
travelInfo_lct.py

View File

@ -2,24 +2,36 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from subprocess import check_output from subprocess import check_output
from requests import get import requests
import json import json
import sys import sys
from datetime import datetime from datetime import datetime
import time
import pytz import pytz
timezone = pytz.timezone('Europe/Berlin') timezone = pytz.timezone('Europe/Berlin')
statusData = None #initialize variables... statusData = None #initialize variables...
tripData = None tripData = None
# Request the raw data from ICE-Portal # Request the raw data from ICE-Portal and raise an exception, when it is not avialable.
def get_api_db(): def get_api_db():
global statusData global statusData
global tripData global tripData
statusData_response = get("https://iceportal.de/api1/rs/status", timeout=5) try:
statusData_response = requests.get("https://iceportal.de/api1/rs/status", timeout=5)
statusData = statusData_response.json() statusData = statusData_response.json()
tripData_response = get("https://iceportal.de/api1/rs/tripInfo/trip", timeout=5) tripData_response = requests.get("https://iceportal.de/api1/rs/tripInfo/trip", timeout=5)
tripData = tripData_response.json() tripData = tripData_response.json()
return 1
except requests.exceptions.RequestException:
return 0
# Request data from travelynx
def get_api_tl():
global tripDataTl
tripDataTl_response = requests.get("https://travelynx.de/api/v1/status/[YOUR_API_KEY_GOES_HERE", timeout=5)
tripDataTl = tripDataTl_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) # 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():
@ -34,11 +46,17 @@ def get_ssid():
# Get Speed from ICE-Portal # Get Speed from ICE-Portal
def get_speed_db(): def get_speed_db():
if statusData["speed"]==None:
return "No Speed"
else:
speed= str(statusData["speed"]) speed= str(statusData["speed"])
return speed return speed
# Figure out, whats the next station and filter the output for this. # Figure out, whats the next station and filter the output for this.
def get_next_db(): def get_next_db():
if tripData["trip"]["stopInfo"] == None :
return "No TripInfo"
else:
next_stop=tripData["trip"]["stopInfo"]["actualNext"] next_stop=tripData["trip"]["stopInfo"]["actualNext"]
for i in tripData['trip']['stops']: for i in tripData['trip']['stops']:
if i['station']['evaNr'] == next_stop: if i['station']['evaNr'] == next_stop:
@ -48,22 +66,60 @@ def get_next_db():
arrivalTime = datetime.fromtimestamp(utime, tz=timezone).strftime('%H:%M') arrivalTime = datetime.fromtimestamp(utime, tz=timezone).strftime('%H:%M')
nextStationTrack = str(i['track']['actual']) nextStationTrack = str(i['track']['actual'])
break break
tripinfo = nextStationName+" "+ arrivalTime+" ("+ arrivalDelay+") "+ nextStationTrack tripinfo = nextStationName+" "+ arrivalTime+" ("+ arrivalDelay+") @ "+ nextStationTrack
return tripinfo return tripinfo
def get_next_tl():
for i in tripDataTl['intermediateStops']:
if i['realArrival'] >= time.time():
nextStationName = i['name']
uScheduledTime=i['scheduledArrival']
uActualTime=i['realArrival']
arrivalDelay="{:+.0f}".format(((uActualTime-uScheduledTime)/60))
arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime('%H:%M')
break
tripinfo = nextStationName+" "+arrivalTime+" ("+arrivalDelay+")"
#tripinfo = nextStationName+" "+ arrivalTime+" ("+ arrivalDelay+") @ "+ nextStationTrack
return tripinfo
# Get the Trainnumber and final station of your current train # Get the Trainnumber and final station of your current train
def get_trainInfo_db(): def get_trainInfo_db():
if tripData['trip']["trainType"]==None:
return "No TrainInfo"
else:
trainType = tripData["trip"]["trainType"] trainType = tripData["trip"]["trainType"]
trainNumber = tripData["trip"]["vzn"] trainNumber = tripData["trip"]["vzn"]
finalStationName = tripData["trip"]["stopInfo"]["finalStationName"] finalStationName = tripData["trip"]["stopInfo"]["finalStationName"]
return trainType+" "+trainNumber+" "+ finalStationName return trainType+" "+trainNumber+" "+ finalStationName
# Get the Trainnumber and final station of your current travelynx trip
def get_trainInfo_tl():
trainType = tripDataTl["train"]["type"]
trainNumber = tripDataTl["train"]["no"]
fromStationName = tripDataTl["fromStation"]["name"]
toStationName = tripDataTl["toStation"]["name"]
toStationTrack = tripDataTl["toStation"]["platform"]
uScheduledTime=tripDataTl['toStation']['scheduledTime']
uActualTime=tripDataTl['toStation']['realTime']
arrivalDelay="{:+.0f}".format(((uActualTime-uScheduledTime)/60))
arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime('%H:%M')
return trainType+" "+trainNumber+" | "+fromStationName+""+ toStationName+" "+arrivalTime+" ("+arrivalDelay+") @ "+ toStationTrack
def main(): def main():
# Look for WIFIonICE otherwise do nothing... # Look for WIFIonICE or WIFI@DB otherwise do nothing...
if get_ssid() == "WIFIonICE": if get_ssid() == "WIFIonICE":
get_api_db() if get_api_db() == 1:
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 else:
get_api_tl()
print(get_trainInfo_tl())
if get_ssid() == "WIFI@DB":
get_api_tl()
print(get_trainInfo_tl()+" | NEXT: "+get_next_tl())
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())