gjohansson-ST / pytrafikverket

python module for communicating with the swedish trafikverket api
MIT License
21 stars 7 forks source link

Implement filtering of [non-]canceled trains #21

Closed farbro closed 1 year ago

farbro commented 1 year ago

This could possible be implemented for ferries as well, but they do not have a "Canceled" property. Instead they have something called "Deleted", but it is unclear what it does.

gjohansson-ST commented 1 year ago

I would suggest you only add --train-canceled with true as default and thereby you already have the option to set false to skip cancelled trains.

Should not be mixed with deleted which is something else

farbro commented 1 year ago

I would suggest you only add --train-canceled with true as default and thereby you already have the option to set false to skip cancelled trains.

But wouldn't that break backwards compatibility? How would you then request both canceled and non-canceled, as it is originally?

gjohansson-ST commented 1 year ago

I would see it like this with --train-canceled

Probably we should reverse this and call it --exclude-canceled or something

farbro commented 1 year ago

Ah I get it, see my new commit. We do lose the ability to filter by canceled trains, but that is a feature that will probably never be used anyway.

farbro commented 1 year ago

Thanks for your review. I applied your suggested changes and also bumped up the version.

gjohansson-ST commented 1 year ago

Did you test it?

farbro commented 1 year ago

I thought so. Now I tested it again, by finding a canceled departure, querying it with -date-time and toggling it with -exclude-canceled-trains.

$ python3 pytrafikverket.py -key <my-key>  -method get-next-train-stop -from-station "Stockholm C" -to-station "Norrköping C" -date-time "2022-11-27T21:38:00"                   
from_station_signature: Cst
to_station_signature:   Nr
advertised_time_at_location = 2022-11-27 21:38:00+01:00
canceled = True
deviations = ['Inställt']
estimated_time_at_location = None
id = 1500adde-115d-43e5-08da-c443c281f393
modified_time = 2022-11-27 15:32:03.108000
other_information = ['Movingo gäller.']
product_description = ['Mälartåg']
time_at_location = None
$ python3 pytrafikverket.py -key <my-key>  -method get-next-train-stop -from-station "Stockholm C" -to-station "Norrköping C" -date-time "2022-11-27T21:38:00" -exclude-canceled-trains
from_station_signature: Cst
to_station_signature:   Nr
advertised_time_at_location = 2022-11-27 21:54:00+01:00
canceled = False
deviations = []
estimated_time_at_location = None
id = 1500adde-115d-43e5-08da-c443d1e13afd
modified_time = 2022-11-13 06:24:58.810000
other_information = ['Movingo gäller ej.']
product_description = ['SJ InterCity']
time_at_location = None

-get-train-stop` behaves similarly but throws an exception for the canceled departure.

gjohansson-ST commented 1 year ago

I guess it would make sense to get an exception when filtering out canceled but you hit a canceled on the result. What is the exception you get?

farbro commented 1 year ago

What is the exception you get?

ValueError: No TrainAnnouncement found

thrown by get_train_stop(...) itself: https://github.com/farbro/pytrafikverket/blob/master/pytrafikverket/trafikverket_train.py#L259