formatted + minor stuff

This commit is contained in:
chrissy 2026-02-02 15:16:55 +01:00
parent 327de4b7e1
commit 2c5550cf05
1 changed files with 160 additions and 68 deletions

View File

@ -8,53 +8,83 @@ import sys
from datetime import datetime from datetime import datetime
import time import time
import pytz import pytz
import re
timezone = pytz.timezone('Europe/Berlin') timezone = pytz.timezone("Europe/Berlin")
statusData = None #initialize variables... statusData = None # initialize variables...
tripData = None tripData = None
# Your Travelynx-Api-token goes here # Your Travelynx-Api-token goes here
tlApiToken="[YOUR API TOKEN]" tlApiToken = "YOUR API TOKEN"
# Known SSIDs for some regional train Wifis... # Known SSIDs for some regional train Wifis...
ssid_regio=["Wifi@DB","RRX Hotspot","Abellio Free WiFi","Arriva","DVB-Hotspot","ENNO_WiFi","FlixTrain Wi-Fi","MAVSTART-WiFi","ODEG Free WiFi","OEBB","RegioJet - zluty","Regiojet - zluty","Wifi@start","alex WiFi","trilex WiFi"] ssid_regio = [
"Chrissy",
"Wifi@DB",
"WIFI@DB",
"RRX Hotspot",
"Abellio Free WiFi",
"Arriva",
"DVB-Hotspot",
"ENNO_WiFi",
"FlixTrain Wi-Fi",
"IntercityWiFi",
"MAVSTART-WiFi",
"ODEG Free WiFi",
"OEBB",
"RegioJet - zluty",
"Regiojet - zluty",
"Wifi@start",
"alex WiFi",
"trilex WiFi",
]
# Request the raw data from ICE-Portal and raise an exception, when it is not avialable. # 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
try: try:
statusData_response = requests.get("https://iceportal.de/api1/rs/status", timeout=5) statusData_response = requests.get(
"https://iceportal.de/api1/rs/status", timeout=5
)
statusData = statusData_response.json() statusData = statusData_response.json()
tripData_response = requests.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 return 1
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return 0 return 0
# Request data from travelynx # Request data from travelynx
def get_api_tl(): def get_api_tl():
global tripDataTl global tripDataTl
tripDataTl_response = requests.get(f"https://travelynx.de/api/v1/status/{tlApiToken}", timeout=5) tripDataTl_response = requests.get(
f"https://travelynx.de/api/v1/status/{tlApiToken}", timeout=5
)
tripDataTl = tripDataTl_response.json() tripDataTl = tripDataTl_response.json()
def get_api_cd(): def get_api_cd():
global statusDataCD global statusDataCD
global tripDataCD global tripDataCD
try: try:
statusDataCD_response = requests.get("http://cdwifi.cz/portal/api/vehicle/realtime", timeout=5) statusDataCD_response = requests.get(
"http://cdwifi.cz/portal/api/vehicle/realtime", timeout=5
)
statusDataCD = statusDataCD_response.json() statusDataCD = statusDataCD_response.json()
return 1 return 1
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return 0 return 0
# 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():
try: try:
output = check_output(['/usr/sbin/iwgetid']).decode('utf-8') output = check_output(["/usr/sbin/iwgetid"]).decode("utf-8")
ssid=output.split('"')[1] ssid = output.split('"')[1]
return ssid return ssid
except: except:
pass pass
@ -63,103 +93,165 @@ def get_ssid():
# Get Speed from ICE-Portal # Get Speed from ICE-Portal
def get_speed_db(): def get_speed_db():
try: try:
speed= str(statusData["speed"]) speed = str(statusData["speed"])
if speed==None: if speed == None:
speed="No Speed" speed = "No Speed"
return speed return speed
except: except:
return "No Speed" return "No Speed"
def get_speed_cd(): def get_speed_cd():
try: try:
speed=str(statusDataCD['speed']) speed = str(statusDataCD["speed"])
if speed==None: if speed == None:
speed="No Speed" speed = "No Speed"
return speed return speed
except: except:
return "No Speed" return "No 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 : if tripData["trip"]["stopInfo"] == None:
return "No TripInfo" return "No TripInfo"
else: 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:
nextStationName = i['station']['name'] nextStationName = i["station"]["name"]
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(
nextStationTrack = str(i['track']['actual']) "%H:%M"
)
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(): def get_next_tl():
try: try:
for i in tripDataTl['intermediateStops']: for i in tripDataTl["intermediateStops"]:
if i['realArrival'] >= time.time(): if i["realArrival"] >= time.time():
nextStationName = i['name'] if i["realArrival"] == None:
uScheduledTime=i['scheduledArrival'] next
uActualTime=i['realArrival'] nextStationName = i["name"]
arrivalDelay="{:+.0f}".format(((uActualTime-uScheduledTime)/60)) uScheduledTime = i["scheduledArrival"]
arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime('%H:%M') uActualTime = i["realArrival"]
tripinfo = nextStationName+" "+arrivalTime+" ("+arrivalDelay+")" arrivalDelay = "{:+.0f}".format(((uActualTime - uScheduledTime) / 60))
arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime(
"%H:%M"
)
tripinfo = (
nextStationName + " " + arrivalTime + " (" + arrivalDelay + ")"
)
break break
else: else:
nextStationName=tripDataTl['toStation']['name'] nextStationName = tripDataTl["toStation"]["name"]
tripinfo=nextStationName tripinfo = nextStationName
except: except:
nextStationName=tripDataTl['toStation']['name'] nextStationName = tripDataTl["toStation"]["name"]
tripinfo=nextStationName tripinfo = nextStationName
return tripinfo 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: if tripData["trip"]["trainType"] == None:
return "No TrainInfo" return "No TrainInfo"
else: 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 # Get the Trainnumber and final station of your current travelynx trip
def get_trainInfo_tl(): def get_trainInfo_tl():
checkin_tl()
trainType = tripDataTl["train"]["type"] trainType = tripDataTl["train"]["type"]
trainNumber = tripDataTl["train"]["no"] trainNumber = tripDataTl["train"]["no"]
fromStationName = tripDataTl["fromStation"]["name"] fromStationName = tripDataTl["fromStation"]["name"]
toStationName = tripDataTl["toStation"]["name"] toStationName = tripDataTl["toStation"]["name"]
toStationTrack = tripDataTl["toStation"]["platform"] toStationTrack = tripDataTl["toStation"]["platform"]
uScheduledTime=tripDataTl['toStation']['scheduledTime'] uScheduledTime = tripDataTl["toStation"]["scheduledTime"]
uActualTime=tripDataTl['toStation']['realTime'] uActualTime = tripDataTl["toStation"]["realTime"]
arrivalDelay="{:+.0f}".format(((uActualTime-uScheduledTime)/60)) arrivalDelay = "{:+.0f}".format(((uActualTime - uScheduledTime) / 60))
arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime('%H:%M') arrivalTime = datetime.fromtimestamp(uActualTime, tz=timezone).strftime("%H:%M")
return trainType+" "+trainNumber+" | "+fromStationName+""+ toStationName+" "+arrivalTime+" ("+arrivalDelay+") @ "+ toStationTrack return (
trainType
+ " "
+ trainNumber
+ " | "
+ fromStationName
+ ""
+ toStationName
+ " "
+ arrivalTime
+ " ("
+ arrivalDelay
+ ") @ "
+ toStationTrack
)
def checkin_tl():
if tripDataTl["checkedIn"] == False:
sys.exit()
def main(): def main():
# Look for WIFIonICE or WIFI@DB otherwise do nothing... # Look for WIFIonICE or WIFI@DB otherwise do nothing...
if get_ssid() == "WIFIonICE": if get_ssid() == "WIFIonICE":
if get_api_db() == 1: if get_api_db() == 1:
print(get_trainInfo_db()+" | "+"SPEED: "+get_speed_db()+" km/h | "+"NEXT: "+get_next_db()) print(
re.sub(
r"\s+",
" ",
get_trainInfo_db()
+ " | "
+ "SPEED: "
+ get_speed_db()
+ " km/h | "
+ "NEXT: "
+ get_next_db(),
)
)
else: else:
get_api_tl() get_api_tl()
print(get_trainInfo_tl()+" | NEXT: "+get_next_tl()) print(re.sub(r"\s+", " ", get_trainInfo_tl() + " | NEXT: " + get_next_tl()))
if get_ssid() in ssid_regio: if get_ssid() in ssid_regio:
get_api_tl() get_api_tl()
print(get_trainInfo_tl()+" | NEXT: "+get_next_tl()) print(re.sub(r"\s+", " ", get_trainInfo_tl() + " | NEXT: " + get_next_tl()))
if get_ssid() == "CDWiFi": if get_ssid() == "CDWiFi":
get_api_tl() get_api_tl()
get_api_cd() get_api_cd()
print(get_trainInfo_tl()+" | "+"SPEED: "+get_speed_cd()+" km/h | NEXT: "+get_next_tl()) print(
re.sub(
r"\s+",
" ",
get_trainInfo_tl()
+ " | "
+ "SPEED: "
+ get_speed_cd()
+ " km/h | NEXT: "
+ get_next_tl(),
)
)
if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main()) sys.exit(main())