Open illesbalog opened 1 year ago
Hey,
your code is correct but you are not outputting the correct variable.
The train.departure
prints out the planned departure from the timetable.
For the departure with delays and stuff you need to print out train.train_changes.departure
.
This can be null if no delays are in the timetable.
Thanks for the answer so this is the updated code:
from deutsche_bahn_api.api_authentication import ApiAuthentication
from deutsche_bahn_api.station_helper import StationHelper
from deutsche_bahn_api.timetable_helper import TimetableHelper
api = ApiAuthentication("client id", "Api key")
success: bool = api.test_credentials()
station_helper = StationHelper()
station_helper.load_stations()
found_stations_by_name = station_helper.find_stations_by_name("Ulm Hbf")
station = found_stations_by_name[0]
timetable_helper = TimetableHelper(station, api)
connections = timetable_helper.get_timetable()
trains_with_changes = timetable_helper.get_timetable_changes(connections)
for train in trains_with_changes:
print("Train Number:", train.train_number)
print("Train Type:", train.train_type)
print("Platform:", train.platform)
print("Departure:", train.train_changes.departure)
and when running it i get the following error:
Traceback (most recent call last):
File "D:\Programming\Projects\Amongus\main.py", line 16, in
do you know why can that be?
i'm reopening it because i accidently closed the issue but my question still stands.
Hi, just saw this issue. For easier output I have enhanced the Train class in my project and added the departureReal attribute. With the method below this field is populated with the departure time if there‘s delay or not. Hope this helps.
from __future__ import annotations
from train_changes import TrainChanges
from otsi_parameters import *
class Train:
stop_id: str
trip_type: str
train_type: str
train_number: str
train_line: str | None
platform: str
passed_stations: str | None
stations: str
arrival: str
departure: str | None
departureReal: str | None
train_changes: TrainChanges | None
ex: str | None
nextStops: [] | None
nextStop: str | None
departureDate: datetime
departureDateReal: datetime
delay: timedelta
def calculateFields(self):
date_format = "%y%m%d%H%M"
try:
self.departureReal = self.train_changes.departure
except:
self.departureReal = self.departure
self.departureDate = datetime.strptime(self.departure, date_format)
self.departureDateReal = datetime.strptime(self.departureReal, date_format)
self.delay = self.departureDateReal - self.departureDate
self.nextStops = self.stations.split("|")
self.nextStop = self.nextStops[0]
edit: self was missing in the method
Hi, when i try to get the current departure times using the get_timetable_changes() method i just get the departure times without the current delays and stuff. Why is that?