diff options
-rwxr-xr-x | bin/efa-m | 4 | ||||
-rw-r--r-- | lib/Travel/Status/DE/VRR.pm | 58 | ||||
-rw-r--r-- | lib/Travel/Status/DE/VRR/Result.pm | 3 | ||||
-rw-r--r-- | t/20-vrr.t | 19 |
4 files changed, 59 insertions, 25 deletions
@@ -5,6 +5,8 @@ use 5.010; our $VERSION = '0.02'; +binmode( STDOUT, ':encoding(utf-8)' ); + use Getopt::Long qw(:config no_ignore_case); use List::Util qw(max); use Travel::Status::DE::VRR; @@ -13,8 +15,6 @@ my ( $date, $time, $input_type ); my ( @grep_lines, @grep_platforms ); my @output; -binmode( STDOUT, ':encoding(utf-8)' ); - GetOptions( 'd|date=s' => \$date, 'h|help' => sub { show_help(0) }, diff --git a/lib/Travel/Status/DE/VRR.pm b/lib/Travel/Status/DE/VRR.pm index 156e815..4f8ff77 100644 --- a/lib/Travel/Status/DE/VRR.pm +++ b/lib/Travel/Status/DE/VRR.pm @@ -7,6 +7,7 @@ use 5.010; our $VERSION = '0.02'; use Carp qw(confess); +use Encode qw(encode decode); use Travel::Status::DE::VRR::Result; use LWP::UserAgent; use XML::LibXML; @@ -126,6 +127,25 @@ sub errstr { return $self->{errstr}; } +sub sprintf_date { + my ($e) = @_; + + return sprintf( '%02d.%02d.%d', + $e->getAttribute('day'), + $e->getAttribute('month'), + $e->getAttribute('year'), + ); +} + +sub sprintf_time { + my ($e) = @_; + + return sprintf( '%02d:%02d', + $e->getAttribute('hour'), + $e->getAttribute('minute'), + ); +} + sub results { my ($self) = @_; my @results; @@ -134,44 +154,52 @@ sub results { my $xp_date = XML::LibXML::XPathExpression->new('./itdDateTime/itdDate'); my $xp_time = XML::LibXML::XPathExpression->new('./itdDateTime/itdTime'); + my $xp_rdate = XML::LibXML::XPathExpression->new('./itdRTDateTime/itdDate'); + my $xp_rtime = XML::LibXML::XPathExpression->new('./itdRTDateTime/itdTime'); my $xp_line = XML::LibXML::XPathExpression->new('./itdServingLine'); - my $xp_extra = XML::LibXML::XPathExpression->new('./motDivaParams'); + my $xp_info + = XML::LibXML::XPathExpression->new('./itdServingLine/itdNoTrain'); for my $e ( $self->{tree}->findnodes($xp_element) ) { my $e_date = ( $e->findnodes($xp_date) )[0]; my $e_time = ( $e->findnodes($xp_time) )[0]; my $e_line = ( $e->findnodes($xp_line) )[0]; + my $e_info = ( $e->findnodes($xp_info) )[0]; + + my $e_rdate = ( $e->findnodes($xp_rdate) )[0]; + my $e_rtime = ( $e->findnodes($xp_rtime) )[0]; if ( not( $e_date and $e_time and $e_line ) ) { next; } - my $date = sprintf( '%d.%d.%d', - $e_date->getAttribute('day'), - $e_date->getAttribute('month'), - $e_date->getAttribute('year'), - ); - my $time = sprintf( '%02d:%02d', - $e_time->getAttribute('hour'), - $e_time->getAttribute('minute'), - ); + my $date = sprintf_date($e_date); + my $time = sprintf_time($e_time); + + my $rdate = $e_rdate ? sprintf_date($e_rdate) : $date; + my $rtime = $e_rtime ? sprintf_time($e_rtime) : $time; + my $platform = $e->getAttribute('platform'); my $line = $e_line->getAttribute('number'); my $dest = $e_line->getAttribute('direction'); - my $info = undef; + my $info = $e_info->textContent; my $countdown = $e->getAttribute('countdown'); + my $delay = $e_info->getAttribute('delay') // 0; push( @results, Travel::Status::DE::VRR::Result->new( - date => $date, - time => $time, + date => $rdate, + time => $rtime, platform => $platform, line => $line, - destination => $dest, + destination => decode( 'UTF-8', $dest ), countdown => $countdown, - info => $info, + info => decode( 'UTF-8', $info ), + delay => $delay, + sched_date => $date, + sched_time => $time, ) ); } diff --git a/lib/Travel/Status/DE/VRR/Result.pm b/lib/Travel/Status/DE/VRR/Result.pm index 93df048..93e651f 100644 --- a/lib/Travel/Status/DE/VRR/Result.pm +++ b/lib/Travel/Status/DE/VRR/Result.pm @@ -9,8 +9,7 @@ use parent 'Class::Accessor'; our $VERSION = '0.02'; Travel::Status::DE::VRR::Result->mk_ro_accessors( - qw(destination date time platform line info - countdown type) + qw(countdown date delay destination info line platform sched_date sched_time time type) ); sub new { @@ -4,8 +4,9 @@ use warnings; use 5.010; use utf8; +use Encode qw(decode); use File::Slurp qw(slurp); -use Test::More tests => 96; +use Test::More tests => 102; BEGIN { use_ok('Travel::Status::DE::VRR'); @@ -26,16 +27,22 @@ for my $result (@results) { can_ok($result, qw(date destination info line time type platform)); } -is($results[0]->destination, 'Düsseldorf Hbf', 'first result: destination ok'); -is($results[0]->info, undef, 'first result: no info'); +is($results[0]->destination, decode('UTF-8', 'Düsseldorf Hbf'), 'first result: destination ok'); +is($results[0]->info, 'Bordrestaurant', 'first result: no info'); is($results[0]->line, 'ICE 946 Intercity-Express', 'first result: line ok'); -is($results[0]->date, '16.11.2011', 'first result: date ok'); -is($results[0]->time, '09:36', 'first result: time ok'); +is($results[0]->date, '16.11.2011', 'first result: real date ok'); +is($results[0]->time, '09:40', 'first result: real time ok'); +is($results[0]->delay, 4, 'first result: delay 4'); +is($results[0]->sched_date, '16.11.2011', 'first result: scheduled date ok'); +is($results[0]->sched_time, '09:36', 'first result: scheduled time ok'); is($results[0]->platform, '#1', 'first result: platform ok'); is($results[-1]->destination, 'Essen Dellwig Bahnhof', 'last result: destination ok'); -is($results[-1]->info, undef, 'last result: no info'); +is($results[-1]->info, q{}, 'last result: no info'); +is($results[-1]->delay, 0, 'lest result: no delay'); is($results[-1]->line, '166', 'last result: line ok'); is($results[-1]->date, '16.11.2011', 'last result: date ok'); is($results[-1]->time, '09:54', 'last result: time ok'); +is($results[-1]->sched_date, '16.11.2011', 'first result: scheduled date ok'); +is($results[-1]->sched_time, '09:54', 'first result: scheduled time ok'); is($results[-1]->platform, '9', 'last result: platform ok'); |