Tutorialwork / deutsche_bahn_api

This is a package to interact with the Deutsche Bahn timetables api
https://pypi.org/project/deutsche-bahn-api
GNU General Public License v3.0
13 stars 11 forks source link

Cannot get departue times with delays using get_timetable_changes() method #3

Open illesbalog opened 1 year ago

illesbalog commented 1 year ago

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?

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("Train_station_Name")

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.departure)
Tutorialwork commented 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.

illesbalog commented 1 year ago

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 trains_with_changes = timetable_helper.get_timetable_changes(connections) File "D:\Programming\Projects\Amongus\venv\lib\site-packages\deutsche_bahn_api\timetable_helper.py", line 128, in get_timetable_changes new_message.time = message.attrib["ts"] KeyError: 'ts'

do you know why can that be?

illesbalog commented 1 year ago

i'm reopening it because i accidently closed the issue but my question still stands.

offle commented 1 year ago

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