From 3986f5f6369755f0c53ad64305d72eec27a10d45 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 11 Dec 2013 19:42:36 +0100 Subject: add option to hide route before / after selected stop --- bin/aseag-m | 19 ++++++++++++------- lib/Travel/Status/DE/ASEAG.pm | 33 ++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/bin/aseag-m b/bin/aseag-m index 5c2c453..af3bd94 100755 --- a/bin/aseag-m +++ b/bin/aseag-m @@ -14,13 +14,17 @@ use List::Util qw(max); use Travel::Status::DE::ASEAG; my (@grep_lines); -my $show_full_route = 0; +my $full_route = 0; +my $hide_past = 1; GetOptions( - 'h|help' => sub { show_help(0) }, - 'f|full-route' => \$show_full_route, - 'l|line=s@' => \@grep_lines, - 'V|version' => \&show_version, + 'a|route-after' => sub { $full_route = 'after' }, + 'b|route-before' => sub { $full_route = 'before' }, + 'h|help' => sub { show_help(0) }, + 'f|full-route' => \$full_route, + 'l|line=s@' => \@grep_lines, + 'p|with-past' => sub { $hide_past = 0 }, + 'V|version' => \&show_version, ) or show_help(1); @@ -70,7 +74,7 @@ sub display_result { @{$line}[ 0 .. 2 ] ); - if ($show_full_route) { + if ($full_route) { print "\n" . $line->[3] . "\n\n\n"; } } @@ -83,8 +87,9 @@ sub show_results { for my $d ( $status->results( + full_routes => $full_route, + hide_past => $hide_past, stop => $stop_name, - full_routes => $show_full_route ) ) { diff --git a/lib/Travel/Status/DE/ASEAG.pm b/lib/Travel/Status/DE/ASEAG.pm index ccdf5fa..ada29e4 100644 --- a/lib/Travel/Status/DE/ASEAG.pm +++ b/lib/Travel/Status/DE/ASEAG.pm @@ -22,6 +22,7 @@ sub new { my $self = { full_routes => $opt{full_routes} // 0, fuzzy => $opt{fuzzy} // 1, + hide_past => $opt{hide_past} // 1, stop => $opt{stop}, post => { ReturnList => @@ -117,9 +118,11 @@ sub results { my $full_routes = $opt{full_routes} // $self->{full_routes} // 0; my $fuzzy = $opt{fuzzy} // $self->{fuzzy} // 1; + my $hide_past = $opt{hide_past} // $self->{hide_past} // 1; my $stop = $opt{stop} // $self->{stop}; my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' ); + my $ts_now = $dt_now->epoch; for my $dep ( @{ $self->{raw_list} } ) { @@ -138,10 +141,31 @@ sub results { next; } + my $dt_dep = DateTime->from_epoch( + epoch => $timestamp / 1000, + time_zone => 'Europe/Berlin' + ); + my $ts_dep = $dt_dep->epoch; + + if ( $hide_past and $dt_dep->subtract_datetime($dt_now)->is_negative ) { + next; + } + if ($full_routes) { @route = map { [ $_->[9] / 1000, $_->[1] ] } grep { $_->[8] == $tripid } @{ $self->{raw_list} }; + if ($hide_past) { + @route = grep { $_->[0] >= $ts_now } @route; + } + + if ( $full_routes eq 'before' ) { + @route = grep { $_->[0] < $ts_dep } @route; + } + elsif ( $full_routes eq 'after' ) { + @route = grep { $_->[0] > $ts_dep } @route; + } + @route = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, $_->[0] ] } @route; @@ -157,11 +181,6 @@ sub results { } @route; } - my $dt_dep = DateTime->from_epoch( - epoch => $timestamp / 1000, - time_zone => 'Europe/Berlin' - ); - push( @results, Travel::Status::DE::ASEAG::Result->new( @@ -176,8 +195,8 @@ sub results { countdown_sec => $dt_dep->subtract_datetime($dt_now)->in_units('seconds'), route_timetable => [@route], - stop => $stopname, - stop_id => $stopid, + stop => $stopname, + stop_id => $stopid, ) ); } -- cgit v1.2.3