#!/usr/bin/env perl use strict; use warnings; use 5.018; use utf8; no if $] >= 5.018, warnings => 'experimental::smartmatch'; our $VERSION = '0.00'; use DateTime; use DateTime::Format::Strptime; use Encode qw(decode); use Getopt::Long qw(:config no_ignore_case bundling); use List::Util qw(first max); use List::MoreUtils qw(none); use Travel::Status::DE::IRIS; use Travel::Status::DE::IRIS::Stations; my ( $date, $time ); my $datetime = DateTime->now( time_zone => 'Europe/Berlin' ); my $filter_via; my ( @grep_class, @grep_type, @grep_platform ); my ( %edata, @edata_pre ); my @output; binmode( STDOUT, ':encoding(utf-8)' ); @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV; GetOptions( 'c|class=s@' => \@grep_class, 'd|date=s' => \$date, 'h|help' => sub { show_help(0) }, 'o|output=s@' => \@edata_pre, 'p|platform=s@' => \@grep_platform, 't|time=s' => \$time, 'T|type=s' => \@grep_type, 'v|via=s' => \$filter_via, 'V|version' => \&show_version, ) or show_help(1); if ( @ARGV != 1 ) { show_help(1); } # opt=foo,bar support @edata_pre = split( qr{,}, join( q{,}, @edata_pre ) ); @grep_class = split( qr{,}, join( q{,}, @grep_class ) ); @grep_platform = split( qr{,}, join( q{,}, @grep_platform ) ); @grep_type = split( qr{,}, join( q{,}, @grep_type ) ); my ($station) = @ARGV; $station = get_station($station); if ($date) { my ( $day, $month, $year ) = split( /\./, $date ); $datetime->set( day => $day, month => $month, year => $year || $datetime->year, ); } if ($time) { my ( $hour, $minute, $second ) = split( /:/, $time ); $datetime->set( hour => $hour, minute => $minute, second => $second || $datetime->second, ); } for my $efield (@edata_pre) { given ($efield) { when ('d') { $edata{delay} = 1 } when ('D') { $edata{delays} = 1 } when ('f') { $edata{fullroute} = 1 } when ('m') { $edata{messages} = 1 } when ('q') { $edata{qos} = 1 } when ('r') { $edata{route} = 1 } when ('t') { $edata{times} = 1 } default { $edata{$efield} = 1 } } } my $status = Travel::Status::DE::IRIS->new( datetime => $datetime, station => $station, ); sub get_station { my ($input_name) = @_; my @stations = Travel::Status::DE::IRIS::Stations::get_station_by_name($input_name); if ( @stations == 0 ) { say STDERR "No station matches '$input_name'"; exit(1); } elsif ( @stations == 1 ) { return $stations[0][0]; } else { say STDERR "The input '$input_name' is ambiguous. Please choose one " . 'of the following:'; say STDERR join( "\n", map { $_->[1] } @stations ); exit(1); } } sub show_help { my ($code) = @_; print 'Usage: db-iris [-f] ' . "[-v ] \n" . "See also: man db-iris\n"; exit $code; } sub show_version { say "db-iris version ${VERSION}"; exit 0; } sub display_result { my (@lines) = @_; my @line_length; if ( not @lines ) { die("Nothing to show\n"); } for my $i ( 0 .. 4 ) { $line_length[$i] = max map { length( $_->[$i] ) } @lines; } for my $line (@lines) { printf( join( q{ }, ( map { "%-${_}s" } @line_length ) ), @{$line}[ 0 .. 4 ] ); my $d = $line->[5]; if ( $edata{delay} and $d->delay and $d->delay_messages ) { printf( ' %s', ( $d->delay_messages )[-1]->[1] ); } print "\n"; if ( $edata{times} ) { if ( not defined $d->delay ) { print "\n"; } elsif ( $d->delay == 0 ) { printf( "%s+0\n", q{ } x 15 ); } else { printf( "%5s → %5s %+d\n", $d->arrival ? $d->arrival->strftime('%H:%M') : q{}, $d->departure ? $d->departure->strftime('%H:%M') : q{}, $d->delay, ); } } if ( $edata{messages} ) { for my $message ( reverse $d->messages ) { # leading spaces to align with regular output printf( " %s %s\n", $message->[0]->strftime('%d.%m. %H:%M'), $message->[1] ); } print "\n"; } if ( $edata{fullroute} ) { print "\n" . join( "\n", $d->route ) . "\n\n"; } } return; } if ( my $err = $status->errstr ) { say STDERR "Request error: ${err}"; exit 2; } for my $d ( $status->results() ) { my @via; @via = $d->route_post; if ( ( $filter_via and not( first { $_ =~ m{$filter_via}io } @via ) ) or ( @grep_class and none { $_ ~~ \@grep_class } $d->classes ) or ( @grep_platform and not( $d->platform ~~ \@grep_platform ) ) or ( @grep_type and not( $d->type ~~ \@grep_type ) ) ) { next; } my $delay = q{}; if ( $d->delay ) { $delay = ( $d->delay > 0 ? ' +' : q{ } ) . $d->delay; } if ( $d->is_cancelled ) { $delay = ' CANCELED'; } my $timestr; if ( $edata{times} ) { $timestr = sprintf( '%5s → %5s', $d->sched_arrival ? $d->sched_arrival->strftime('%H:%M') : q{}, $d->sched_departure ? $d->sched_departure->strftime('%H:%M') : q{}, ); } else { $timestr = $d->time . $delay; } push( @output, [ $timestr, $d->train, $edata{route} ? join( q{ }, $d->route_interesting ) : q{}, $d->route_end, $d->platform, $d ] ); } display_result(@output); __END__ =head1 NAME db-iris - Interface to the DeutscheBahn online departure monitor =head1 SYNOPSIS B [B<-fV>] [B<-d> I] [B<-t> I