summaryrefslogtreecommitdiff
path: root/lib/Travel/Status
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-11-15 21:11:40 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2025-11-15 21:11:40 +0100
commitab108ee2da48b4e234c73ea3712e34b566759c0f (patch)
tree0438745231221b6c6c04939ee7f809e3b9568f9b /lib/Travel/Status
parent6d406383f4852273cd89f6aa8fa07ac98328930a (diff)
Journey: expose admin_id / admin_ids
Diffstat (limited to 'lib/Travel/Status')
-rw-r--r--lib/Travel/Status/DE/DBRIS/Journey.pm56
1 files changed, 53 insertions, 3 deletions
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{ (?<actualtype> [^\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<journey> 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