From 1d12c3e5585411a6ff497ee274ba699579fa11b1 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 28 Jan 2014 23:02:27 +0100 Subject: move track-via to db-iris --- bin/db-iris | 37 +++++++- examples/track-from-to | 234 ------------------------------------------------- 2 files changed, 33 insertions(+), 238 deletions(-) delete mode 100755 examples/track-from-to diff --git a/bin/db-iris b/bin/db-iris index baf7627..5ce8267 100755 --- a/bin/db-iris +++ b/bin/db-iris @@ -19,9 +19,9 @@ use Travel::Status::DE::IRIS::Stations; my ( $date, $time ); my $datetime = DateTime->now( time_zone => 'Europe/Berlin' ); -my $filter_via; +my ( $filter_via, $track_via, $status_via ); my ( @grep_class, @grep_type, @grep_platform ); -my ( %edata, @edata_pre ); +my ( %edata, @edata_pre ); my @output; @@ -38,7 +38,8 @@ GetOptions( 't|time=s' => \$time, 'T|type=s' => \@grep_type, 'v|via=s' => \$filter_via, - 'V|version' => \&show_version, + 'V|track-via=s' => \$track_via, + 'version' => \&show_version, ) or show_help(1); @@ -55,6 +56,10 @@ if ( @ARGV != 1 ) { my ($station) = @ARGV; $station = get_station($station); +if ($track_via) { + $track_via = get_station($track_via); +} + if ($date) { my ( $day, $month, $year ) = split( qr{ [.] }ox, $date ); $datetime->set( @@ -89,6 +94,12 @@ my $status = Travel::Status::DE::IRIS->new( datetime => $datetime, station => $station, ); +if ($track_via) { + $status_via = Travel::Status::DE::IRIS->new( + datetime => $datetime, + station => $track_via, + ); +} sub get_station { my ($input_name) = @_; @@ -208,6 +219,12 @@ if ( my $err = $status->errstr ) { exit 2; } +if ( $status_via and $status_via->errstr ) { + my $err = $status_via->errstr; + say STDERR "Request error: ${err}"; + exit 2; +} + for my $d ( $status->results() ) { my @via; @@ -232,7 +249,19 @@ for my $d ( $status->results() ) { } my $timestr; - if ( $edata{times} ) { + if ($track_via) { + $timestr = $d->time; + my $d_via = first { $_->train_id eq $d->train_id } $status_via->results; + if ( not $d_via + or not $d_via->arrival + or $d_via->arrival < $d->departure ) + { + next; + } + my $timestr_via = $d_via->arrival->strftime('%H:%M') . $delay; + $timestr = $d->time . ' → ' . $timestr_via; + } + elsif ( $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{}, diff --git a/examples/track-from-to b/examples/track-from-to deleted file mode 100755 index 9df96e0..0000000 --- a/examples/track-from-to +++ /dev/null @@ -1,234 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; -use 5.014; -use utf8; - -no if $] >= 5.018, warnings => 'experimental::smartmatch'; - -our $VERSION = '0.01'; - -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 ( @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, - 'h|help' => sub { show_help(0) }, - 'o|output=s@' => \@edata_pre, - 'p|platform=s@' => \@grep_platform, - 'T|type=s' => \@grep_type, - 'V|version' => \&show_version, - -) or show_help(1); - -if ( @ARGV < 2 ) { - 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 ($from, $filter_via, $to) = @ARGV; -$to //= $filter_via; -$from = get_station($from); -$to = get_station($to); - -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_f = Travel::Status::DE::IRIS->new( - station => $from, -); - -my $status_t = Travel::Status::DE::IRIS->new( - station => $to, -); - -sub get_station { - my ($input_name) = @_; - - my @stations = Travel::Status::DE::IRIS::Stations::get_station($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 [-V] [-c ] [-d ] ' - . '[-o ] [-p ] [-t