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

add mot filter ,transfers, and routes

parent 9bac2c56
Loading
Loading
Loading
Loading
+42 −10
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ my $output_reset = -t STDOUT ? "\033[0m" : q{};
GetOptions(
	'd|date=s'             => \$date,
	'h|help'               => sub { show_help(0) },
	'm|modes-of-transit=s' => \$mots,
	't|time=s'             => \$time,
	'V|version'            => \&show_version,
	'cache!'               => \$use_cache,
@@ -129,6 +130,10 @@ if ( $date or $time ) {
	$opt{datetime} = $dt;
}

if ($mots) {
	$opt{modes_of_transit} = [ split( qr{, *}, $mots ) ];
}

sub show_help {
	my ($code) = @_;

@@ -201,9 +206,18 @@ for my $connection ( $ris->connections ) {

	my $header = q{};
	for my $segment ( $connection->segments ) {
		$header .= sprintf( '  %s', $segment->train_short, );
		if ( $segment->train_short ) {
			$header .= sprintf( '  %s', $segment->train_short );
		}
		elsif ( $segment->is_transfer ) {
			$header .= sprintf( '  %.1fkm', $segment->distance_m / 1e3 );
		}
		else {
			$header .= q{  ??};
		}
	}

	say q{};
	printf(
		"%s  (%02d:%02d)  %s  %s%s\n\n",
		$connection->dep
@@ -215,10 +229,28 @@ for my $connection ( $ris->connections ) {
		$header,
	);
	for my $segment ( $connection->segments ) {
		printf( "%s → %s\n", $segment->train_mid, $segment->direction );
		if ( $segment->is_transfer ) {
			for my $note ( $segment->transfer_notes ) {
				say $note;
			}
		}
		elsif ( $segment->direction ) {
			printf( "${output_bold}%s${output_reset} → %s  %s\n",
				$segment->train_mid, $segment->direction,
				format_occupancy($segment) );
		}
		else {
			printf( "${output_bold}%s${output_reset}\n", $segment->train_long );
		}
		printf( "%s  ab  %s\n",
			$segment->dep->strftime('%H:%M'),
			$segment->dep_name );

		for my $stop ( $segment->route ) {
			printf( "%s  %s  %s\n",
				$stop->arr ? $stop->arr->strftime('%H:%M') : q{     },
				format_occupancy($stop), $stop->name, );
		}
		printf( "%s  an  %s\n",
			$segment->arr->strftime('%H:%M'),
			$segment->arr_name );
+52 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use 5.020;
use parent 'Class::Accessor';

use DateTime::Duration;
use Travel::Status::DE::DBRIS::Location;

our $VERSION = '0.01';

@@ -18,6 +19,8 @@ Travel::Routing::DE::DBRIS::Connection::Segment->mk_ro_accessors(
	  sched_arr rt_arr arr
	  sched_duration rt_duration duration duration_percent
	  journey_id
	  occupancy occupancy_first occupancy_second
	  is_transfer distance_m
	)
);

@@ -71,9 +74,58 @@ sub new {
	}
	$ref->{duration} = $ref->{rt_duration} // $ref->{sched_duration};

	for my $occupancy ( @{ $json->{auslastungsmeldungen} // [] } ) {
		if ( $occupancy->{klasse} eq 'KLASSE_1' ) {
			$ref->{occupancy_first} = $occupancy->{stufe};
		}
		if ( $occupancy->{klasse} eq 'KLASSE_2' ) {
			$ref->{occupancy_second} = $occupancy->{stufe};
		}
	}

	if ( $ref->{occupancy_first} and $ref->{occupancy_second} ) {
		$ref->{occupancy}
		  = ( $ref->{occupancy_first} + $ref->{occupancy_second} ) / 2;
	}
	elsif ( $ref->{occupancy_first} ) {
		$ref->{occupancy} = $ref->{occupancy_first};
	}
	elsif ( $ref->{occupancy_second} ) {
		$ref->{occupancy} = $ref->{occupancy_second};
	}

	for my $stop ( @{ $json->{halte} // [] } ) {
		push(
			@{ $ref->{route} },
			Travel::Status::DE::DBRIS::Location->new(
				json         => $stop,
				strptime_obj => $strptime
			)
		);
	}

	if ( $json->{verkehrsmittel}{typ} eq 'TRANSFER' ) {
		$ref->{is_transfer} = 1;
		$ref->{transfer_notes}
		  = [ map { $_->{value} } @{ $json->{transferNotes} // [] } ];
		$ref->{distance_m} = $json->{distanz};
	}

	bless( $ref, $obj );

	return $ref;
}

sub route {
	my ($self) = @_;

	return @{ $self->{route} // [] }[ 1 .. $#{ $self->{route} } - 1 ];
}

sub transfer_notes {
	my ($self) = @_;

	return @{ $self->{transfer_notes} // [] };
}

1;