summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-11-15 21:27:08 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2025-11-15 21:27:08 +0100
commite94bed9186dc0636567604056edef7e0af7bb29c (patch)
treecac608878dd0ff34076d4c33e12560ad94b38fb6 /lib
parent5f42920c86ec1cf51eccd8970757909308fd7617 (diff)
Expose operator names (if available), fall back to admin IDs otherwise
See #10
Diffstat (limited to 'lib')
-rw-r--r--lib/Travel/Status/DE/DBRIS/Journey.pm34
-rw-r--r--lib/Travel/Status/DE/DBRIS/Operators.pm.PL66
2 files changed, 99 insertions, 1 deletions
diff --git a/lib/Travel/Status/DE/DBRIS/Journey.pm b/lib/Travel/Status/DE/DBRIS/Journey.pm
index 2a8cb49..5543abc 100644
--- a/lib/Travel/Status/DE/DBRIS/Journey.pm
+++ b/lib/Travel/Status/DE/DBRIS/Journey.pm
@@ -7,13 +7,15 @@ use 5.020;
use parent 'Class::Accessor';
use Travel::Status::DE::DBRIS::Location;
+use Travel::Status::DE::DBRIS::Operators;
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(admin_id day id train train_no line_no type number is_cancelled));
+ qw(admin_id day id train train_no line_no type number operator is_cancelled)
+);
sub new {
my ( $obj, %opt ) = @_;
@@ -52,10 +54,24 @@ sub new {
if ( defined( my $admin_id = $admin_id_ml{ $admin_id_argmax[0] } ) )
{
$ref->{admin_id} = $admin_id;
+ if (
+ my $op
+ = Travel::Status::DE::DBRIS::Operators::get_operator_name(
+ $admin_id)
+ )
+ {
+ $ref->{operator} = $admin_id;
+ }
}
# return most frequent admin ID first
$ref->{admin_ids} = \@admin_id_argmax;
+ $ref->{operators} = [
+ map {
+ Travel::Status::DE::DBRIS::Operators::get_operator_name($_)
+ // $_
+ } @admin_id_argmax
+ ];
}
if (%trip_no_ml) {
@@ -202,6 +218,12 @@ sub admin_ids {
return @{ $self->{admin_ids} // [] };
}
+sub operators {
+ my ($self) = @_;
+
+ return @{ $self->{operators} // [] };
+}
+
sub TO_JSON {
my ($self) = @_;
@@ -274,6 +296,16 @@ majority of stops.
List of strings indirectly identifying the operators of the journey, in
descending order of the number of stops they are responsible for.
+=item $journey->operator
+
+String naming the operator of the journey. In case there are mulitple
+operators, returns the one responsible for the majority of stops.
+
+=item $journey->operators
+
+List of strings naming 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
diff --git a/lib/Travel/Status/DE/DBRIS/Operators.pm.PL b/lib/Travel/Status/DE/DBRIS/Operators.pm.PL
new file mode 100644
index 0000000..91ad8d2
--- /dev/null
+++ b/lib/Travel/Status/DE/DBRIS/Operators.pm.PL
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.020;
+use utf8;
+use File::Slurp qw(read_file write_file);
+use JSON;
+
+my $json_str = read_file('share/admin-id-to-operator.json');
+my $adm_to_op = JSON->new->utf8->decode($json_str);
+
+my $buf = <<'EOF';
+package Travel::Status::DE::DBRIS::Operators;
+
+# vim:readonly
+# This module has been automatically generated from share/admin-id-to-operator.json
+# by lib/Travel/Status/DE/DBRIS/Operators.pm.PL.
+# Do not edit, changes will be lost.
+
+use strict;
+use warnings;
+use 5.020;
+use utf8;
+
+our $VERSION = '0.16';
+
+# Automatically generated, see share/stations.json
+my %admin_id_to_operator = (
+EOF
+
+while ( my ( $k, $v ) = each %{$adm_to_op} ) {
+ if ( $k =~ m{'} or $v->[0] =~ m{'} or $v->[1] =~ m{'} ) {
+ die("Unsupported entry: $k");
+ }
+ $buf .= sprintf( "'%s' => ['%s', '%s'],\n", $k, @{$v} );
+}
+
+$buf .= <<'EOF';
+);
+
+sub get_operator {
+ my ($id) = @_;
+ return $admin_id_to_operator{$id};
+}
+
+sub get_operator_abbr {
+ my ($id) = @_;
+ if (my $op = $admin_id_to_operator{$id}) {
+ return $op->[0];
+ }
+ return;
+}
+
+sub get_operator_name {
+ my ($id) = @_;
+ if (my $op = $admin_id_to_operator{$id}) {
+ return $op->[1];
+ }
+ return;
+}
+
+1;
+EOF
+
+write_file( $ARGV[0], { binmode => ':utf8' }, $buf );