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
9 stars 9 forks source link

decode messages to find canceled trains #1

Closed asraelvudogel closed 1 year ago

asraelvudogel commented 1 year ago

Hi,

thanks for the great interface. I haven't found a way to filter out canceled trains yet. The API only outputs a message, which is not decoded in your code, so I can read it or filter out the train.

Example for such a train:

print (train.__dict__)
    {'stop_id': '9122331714189316075-2303151606-1', 'train_type': 'RE', 'train_number': '11424', 'platform': '6A-C', 'stations': 'Bad Oldesloe|Reinfeld(Holst)|Lübeck Hbf', 'departure': '2303151606', 'trip_type': 'N', 'train_line': '8', 'train_changes': <deutsche_bahn_api. train_changes.TrainChanges object at 0x7f6e4b0ace20>}

print (train.train_changes.__dict__)
    {'messages': [<deutsche_bahn_api.message.Message object at 0x7f6e4b0ace50>], 'stations': 'Bad Oldesloe|Reinfeld(Holst)|Lübeck Hbf'}
Tutorialwork commented 1 year ago

Hey, sorry for the last response.

There is a way to check if the train is canceled or not. If a train got canceled, the stations for the trains are set to an empty string. The Train object represents the planned data, and the train changes property the changed data. That means you must be checking if the property train.train_changes.stations is an empty string.

Below I provided you an example.

timetable_helper = TimetableHelper(found_stations_by_name[1], api)
trains = timetable_helper.get_timetable(13)
trains = timetable_helper.get_timetable_changes(trains)

for train in trains:
    print(train.train_type + " " + train.train_line + " - " + train.departure + " ("  + train.platform + ")")
    try:
        if len(train.train_changes.stations) == 0:
            print("Train is canceled")
    except AttributeError:
        pass