#!perl use strict; use warnings; use 5.014; our $VERSION = '0.02'; use utf8; use DateTime; use Encode qw(decode); use JSON; use Getopt::Long qw(:config no_ignore_case bundling); use List::MoreUtils qw(uniq); use List::Util qw(first max); use Travel::Routing::DE::HAFAS; use Travel::Status::DE::HAFAS; my ( $date, $time, $language ); my $types = q{}; my $developer_mode; my $json_output; my $show_full_route; my ( $list_services, $service ); my ( @excluded_mots, @exclusive_mots ); my $verbosity = 0; my @output; my %min_verbosity = ( A => 3, C => 0, G => 2, H => 1, L => 0, M => 1, P => 0, ); binmode( STDOUT, ':encoding(utf-8)' ); for my $arg (@ARGV) { $arg = decode( 'UTF-8', $arg ); } my $output_bold = -t STDOUT ? "\033[1m" : q{}; my $output_reset = -t STDOUT ? "\033[0m" : q{}; GetOptions( 'd|date=s' => \$date, 'h|help' => sub { show_help(0) }, 'f|full-route' => \$show_full_route, 'l|language=s' => \$language, 'm|mot=s' => \$types, 's|service=s' => \$service, 't|time=s' => \$time, 'v|verbose+' => \$verbosity, 'V|version' => \&show_version, 'devmode' => \$developer_mode, 'json' => \$json_output, 'list' => \$list_services, ) or show_help(1); if ($list_services) { printf( "%-40s %-14s %s\n\n", 'operator', 'abbr. (-s)', 'languages (-l)' ); for my $service ( Travel::Routing::DE::HAFAS::get_services() ) { printf( "%-40s %-14s %s\n", @{$service}{qw(name shortname)}, join( q{ }, @{ $service->{languages} // [] } ) ); } exit 0; } parse_mot_options(); my ( $from_stop, $to_stop ) = @ARGV; if ( not $from_stop and $to_stop ) { show_help(1); } my %opt = ( excluded_mots => \@excluded_mots, exclusive_mots => \@exclusive_mots, from_stop => $from_stop, to_stop => $to_stop, developer_mode => $developer_mode, service => $service, language => $language, ); if ( $date or $time ) { my $dt = DateTime->now( time_zone => 'Europe/Berlin' ); if ($date) { if ( $date =~ m{ ^ (? \d{1,2} ) [.] (? \d{1,2} ) [.] (? \d{4})? $ }x ) { $dt->set( day => $+{day}, month => $+{month} ); if ( $+{year} ) { $dt->set( year => $+{year} ); } } else { say "--date must be specified as DD.MM.[YYYY]"; exit 1; } } if ($time) { if ( $time =~ m{ ^ (? \d{1,2} ) : (? \d{1,2} ) $ }x ) { $dt->set( hour => $+{hour}, minute => $+{minute}, second => 0, ); } else { say "--time must be specified as HH:MM"; exit 1; } } $opt{datetime} = $dt; } my $hafas = Travel::Routing::DE::HAFAS->new(%opt); sub show_help { my ($code) = @_; print 'Usage: hafas [-d ] [-m ] [-t