Unverified Commit ab108ee2 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Journey: expose admin_id / admin_ids

parent 6d406383
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -389,11 +389,16 @@ elsif ( $opt{journey} and not( $raw_json_output or $json_output ) ) {
		}
	}

	printf( "%s %s am %s\n\n",
	printf( "%s %s am %s\n",
		$trip->type,
		$trip->train_no // $trip->number,
		$trip->day->strftime('%d.%m.%Y') );

	if ( $trip->admin_ids ) {
		printf( "Betrieb: %s\n", join( q{, }, $trip->admin_ids ) );
	}
	say q{};

	for my $stop ( $trip->route ) {
		if ( $stop == $mark_stop ) {
			print($output_bold);
+53 −3
Original line number Diff line number Diff line
@@ -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