From ab108ee2da48b4e234c73ea3712e34b566759c0f Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Sat, 15 Nov 2025 21:11:40 +0100 Subject: Journey: expose admin_id / admin_ids --- lib/Travel/Status/DE/DBRIS/Journey.pm | 56 +++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/Travel/Status/DE/DBRIS/Journey.pm b/lib/Travel/Status/DE/DBRIS/Journey.pm index 17ee1ea..2a8cb49 100644 --- a/lib/Travel/Status/DE/DBRIS/Journey.pm +++ b/lib/Travel/Status/DE/DBRIS/Journey.pm @@ -13,7 +13,7 @@ our $VERSION = '0.16'; # ->number is deprecated # TODO: Rename ->train, ->train_no to ->trip, ->trip_no Travel::Status::DE::DBRIS::Journey->mk_ro_accessors( - qw(day id train train_no line_no type number is_cancelled)); + qw(admin_id day id train train_no line_no type number is_cancelled)); sub new { my ( $obj, %opt ) = @_; @@ -33,7 +33,39 @@ sub new { }; if ( $json->{halte} and @{ $json->{halte} } ) { - $ref->{train_no} = $json->{halte}[0]{nummer}; + my %admin_id_ml; + my %trip_no_ml; + + for my $stop ( @{ $json->{halte} } ) { + if ( defined $stop->{adminID} ) { + $admin_id_ml{ $stop->{adminID} } += 1; + } + if ( defined $stop->{nummer} ) { + $trip_no_ml{ $stop->{nummer} } += 1; + } + } + + if (%admin_id_ml) { + my @admin_id_argmax + = reverse sort { $admin_id_ml{$a} <=> $admin_id_ml{$b} } + keys %admin_id_ml; + if ( defined( my $admin_id = $admin_id_ml{ $admin_id_argmax[0] } ) ) + { + $ref->{admin_id} = $admin_id; + } + + # return most frequent admin ID first + $ref->{admin_ids} = \@admin_id_argmax; + } + + if (%trip_no_ml) { + my @trip_no_argmax + = reverse sort { $trip_no_ml{$a} <=> $trip_no_ml{$b} } + keys %trip_no_ml; + if ( defined( my $trip_no = $trip_no_ml{ $trip_no_argmax[0] } ) ) { + $ref->{train_no_no} = $trip_no; + } + } } # Number is either train no (ICE, RE) or line no (S, U, Bus, ...) @@ -44,7 +76,8 @@ sub new { # For some trains, the train type also contains the train number like "MEX19161" # If we can detect this, remove the number from the train type - if ( $ref->{train_no} and $ref->{type} + if ( $ref->{train_no} + and $ref->{type} and $ref->{type} =~ qr{ (? [^\d]+ ) $ref->{train_no} $ }x ) { $ref->{type} = $+{actualtype}; @@ -163,6 +196,12 @@ sub messages { return @{ $self->{messages} // [] }; } +sub admin_ids { + my ($self) = @_; + + return @{ $self->{admin_ids} // [] }; +} + sub TO_JSON { my ($self) = @_; @@ -224,6 +263,17 @@ origin station. Trip ID / journey ID, i.e., the argument passed to Travel::Status::DE::DBRIS->new's B key. +=item $journey->admin_id + +Admin ID identifying the operator of the journey. +In case there are mulitple operators, returns the one responsible for the +majority of stops. + +=item $journey->admin_ids + +List of strings indirectly identifying the operators of the journey, in +descending order of the number of stops they are responsible for. + =item $journey->train Textual description of the departure, typically consisting of type identifier -- cgit v1.2.3