From fd1ff5ad10e530661f59e3218dbf8ea97cfe4112 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Tue, 31 Oct 2023 22:39:22 +0100 Subject: initial commit --- bin/hafas | 357 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100755 bin/hafas (limited to 'bin') diff --git a/bin/hafas b/bin/hafas new file mode 100755 index 0000000..f77ddd6 --- /dev/null +++ b/bin/hafas @@ -0,0 +1,357 @@ +#!perl +use strict; +use warnings; +use 5.014; + +our $VERSION = '0.00'; + +use utf8; +use DateTime; +use Encode qw(decode); +use JSON; +use Getopt::Long qw(:config no_ignore_case); +use List::MoreUtils qw(uniq); +use List::Util qw(first max); +use Travel::Routing::DE::HAFAS; + +my ( $date, $time, $language ); +my $types = q{}; +my $developer_mode; +my $json_output; +my ( $list_services, $service ); +my ( @excluded_mots, @exclusive_mots ); + +my @output; + +binmode( STDOUT, ':encoding(utf-8)' ); +for my $arg (@ARGV) { + $arg = decode( 'UTF-8', $arg ); +} + +GetOptions( + 'd|date=s' => \$date, + 'h|help' => sub { show_help(0) }, + 'l|language=s' => \$language, + 'm|mot=s' => \$types, + 's|service=s' => \$service, + 't|time=s' => \$time, + '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