Travel-Status-MOTIS

 view release on metacpan or  search on metacpan

bin/motis-m  view on Meta::CPAN

	print
	  "Usage: motis [-d dd.mm.yyy] [-t hh:mm] [-i] <stopId|tripId|lat:lon>\n"
	  . "See also: man motis\n";

	exit $code;
}

sub show_version {
	say "motis version ${VERSION}";

	exit 0;
}

sub spacer {
	my ($len) = @_;
	return ( $len % 2 ? q { } : q{} ) . ( q{ ·} x ( $len / 2 ) );
}

sub format_delay {
	my ( $delay, $len ) = @_;
	if ( $delay and $len ) {
		return sprintf( "(%+${len}d)", $delay );
	}
	return q{};
}

my $status = Travel::Status::MOTIS->new(%opt);

if ( my $err = $status->errstr ) {
	say STDERR "Request error: ${err}";
	exit 2;
}

if ($raw_json_output) {
	say JSON->new->convert_blessed->encode( $status->{raw_json} );
	exit 0;
}

if ($json_output) {
	if ( $opt{trip_id} ) {
		say JSON->new->convert_blessed->encode( $status->result );
	}
	elsif ( $opt{stop_id} ) {
		say JSON->new->convert_blessed->encode(
			{
				result  => $status->result,
				results => [ $status->results ]
			}
		);
	}
	else {
		say JSON->new->convert_blessed->encode( [ $status->results ] );
	}

	exit 0;
}

if ( $opt{stop_id} ) {
	my $max_display_name
	  = max map { length( $_->display_name ) } $status->results;
	my $max_headsign
	  = max map { length( $_->headsign // q{} ) } $status->results;
	my $max_delay = max map { length( $_->stopover->departure_delay // q{} ) }
	  $status->results;
	my $max_track = max map {
		length( $_->stopover->track // $_->stopover->scheduled_track // q{} )
	} $status->results;

	$max_delay += 1;

	my @results = map { $_->[1] }
	  sort { $a->[0] <=> $b->[0] }
	  map {
		[ ( $_->stopover->departure // $_->stopover->arrival )->epoch, $_ ]
	  } $status->results;

	printf( "%s\n\n", $results[0]->stopover->stop->name );

	for my $result (@results) {
		printf(
"%s  %s  %${max_display_name}s  %${max_headsign}s  %${max_track}s\n",
			$result->is_cancelled ? '--:--'
			: $result->stopover->departure->strftime('%H:%M'),
			$result->stopover->departure_delay ? sprintf(
				"(%+${max_delay}d)", $result->stopover->departure_delay
			  )
			: q{ } x ( $max_delay + 2 ),
			$result->display_name,
			$result->headsign // q{???},
			$result->stopover->track // q{}
		);

		if ($show_trip_ids) {
			say $result->id;
		}
	}
}
elsif ( $opt{trip_id} ) {
	my $trip = $status->result;

	my $max_name  = max map { length( $_->stop->name ) } $trip->stopovers;
	my $max_track = max map { length( $_->track // q{} ) } $trip->stopovers;
	my $max_delay
	  = max map { $_->delay ? length( $_->delay ) + 3 : 0 } $trip->stopovers;

	my $mark_stop = 0;
	my $now       = DateTime->now;

	for my $i ( reverse 1 .. ( scalar $trip->stopovers // 0 ) ) {
		my $stop = ( $trip->stopovers )[ $i - 1 ];

		if (
			not $stop->is_cancelled
			and (  $stop->departure and $now <= $stop->departure
				or $stop->arrival and $now <= $stop->arrival )
		  )
		{
			$mark_stop = $stop;
		}
	}

	printf( "%s am %s\n\n",
		$trip->display_name, $trip->scheduled_arrival->strftime('%d.%m.%Y') );

	for my $stop ( $trip->stopovers ) {
		if ( $stop == $mark_stop ) {
			print($output_bold);
		}

		if ( $stop->is_cancelled ) {
			print('    --:--    ');
		}
		elsif ( $stop->arrival and $stop->departure ) {
			printf( '%s → %s',
				$stop->arrival->strftime('%H:%M'),
				$stop->departure->strftime('%H:%M'),
			);
		}
		elsif ( $stop->departure ) {
			printf( '        %s', $stop->departure->strftime('%H:%M') );
		}
		elsif ( $stop->arrival ) {
			printf( '%s        ', $stop->arrival->strftime('%H:%M') );
		}
		else {
			print('             ');
		}

		printf( " %${max_delay}s",

bin/motis-m  view on Meta::CPAN

		if ( $stop == $mark_stop ) {
			print($output_reset);
		}
	}
}
elsif ( $opt{stops_by_coordinate} ) {
	for my $result ( $status->results ) {
		if ( defined $result->id ) {
			printf( "%8d  %s\n", $result->id, $result->name );
		}
	}
}
elsif ( $opt{stops_by_query} ) {
	for my $result ( $status->results ) {
		if ( defined $result->id ) {
			printf( "%8d  %s\n", $result->id, $result->name );
		}
	}
}

__END__

=head1 NAME

motis-m - Interface to MOTIS public transit services

=head1 SYNOPSIS

B<motis-m> [B<-s> I<service>] [B<-d> I<DD.MM.>] [B<-t> I<HH:MM>] [B<-i>] [I<opt>] I<station>

B<motis-m> [B<-s> I<service>] [I<opt>] I<station>

B<motis-m> [B<-s> I<service>] I<trip_id>

B<motis-m> [B<-s> I<service>] B<?>I<query>|I<lat>B<:>I<lon>

=head1 VERSION

version 0.04

=head1 DESCRIPTION

B<motis-m> is an interface to MOTIS routing services. It can serve as an
arrival/departure board, request details about a specific trip, and
look up public transport stops by name or geolocation. The operating
mode depends on the contents of its non-option argument.

=head2 Departure Board (I<stop>)

Show departures at I<stop>. I<stop> may be given as a stop name or
stop id. For each departure, B<motis-m> shows

=over

=item * estimated departure time,

=item * delay, if known,

=item * trip route name,

=item * headsign / destination if known, and

=item * track, if known.

=back

=head2 Trip details (I<trip_id>)

List intermediate stops of I<trip_id> (as given by the departure board when
invoked with B<-i> / B<--show-trip-ids>) with arrival/departure time, delay (if
available), track (if available), and stop name. Also includes some generic
trip information.

=head2 Stop Search (B<?>I<query>|I<lat>B<:>I<lon>)

List stop that match I<query> or that are located in the vicinity of
I<lat>B<:>I<lon> geocoordinates with stop id and name.

=head1 OPTIONS

Values in brackets indicate options that only apply to the corresponding
operating mode(s).

=over

=item B<-d>, B<--date> I<DD.MM.[YYYY]> (departure board)

Request departures on the specified date.
Default: today.

=item B<-t>, B<--time> I<HH:MM> (departure board)

Request departures on the specified time.
Default: now.

=item B<-i>, B<--show-trip-ids> (departure board)

Show trip id for each listed arrival/departure.
These can be used to obtain details on individual trips with subsequent
B<motis-m> invocations.

=item B<-m>, B<--modes-of-transit> I<mot1>[,I<mot2>,...] (departure board)

Only return results for the specified modes of transit.
Use C<<-m help>> to get a list of supported modes of transit.

=item B<--json>

Print result(s) as JSON and exit. This is a dump of internal data structures
and not guaranteed to remain stable between minor versions. Please use the
Travel::Status::MOTIS(3pm) module if you need a proper API.

=item B<--no-cache>

By default, if the Cache::File module is available, server replies are cached
for 90 seconds in F<~/.cache/Travel-Status-MOTIS> (or a path relative to
C<$XDG_CACHE_HOME>, if set). Use this option to disable caching. You can use
B<--cache> to re-enable it.

=item B<--raw-json>



( run in 1.974 second using v1.01-cache-2.11-cpan-7f9471e7e0a )